Run Rev's within() function--Was: Problem with mask
James Hurley
jhurley0305 at sbcglobal.net
Mon Oct 27 21:33:42 EDT 2008
Wilhelm et. al.,
It is possible to write a within() function which, unlike the RR
function, includes all point on the perimeter as within the ellipse,
not just those on the top and left sides.
If one defines the ellipse by its two foci and its width, you know
that a point for which the sum of the distances to the two foci is
less than or equal to its width will be inside, otherwise outside.
This is one definition of an ellipse.
The following script is a demonstration, or to see it in action run
the following in the message box:
go url "http://home.infostations.net/jhurley/WithinEllipse.rev"
Jim Hurley
local p1,p2, myName,w
on mouseDown
put the name of me into myName
put the width of grc "oval"/2 into w -- the half width
put the height of grc "oval" / 2 into h -- the half height
-- Given two points p1 and p2, an ellipse is the locus of points
--such that the sum of the distance to each focus is a constant.
-- Calculate the distance (2s) between the two foci of the ellipse
put sqrt(abs(w*w - h*h)) into s -- A little geometry here.
put the loc of grc "oval" into tLoc
--Get coordinates of the two foci
put item 1 of tLoc - s & comma & item 2 of tLoc into p1--focus 1
put item 1 of tLoc + s & comma & item 2 of tLoc into p2--focus 2
end mouseDown
on mousemove u,v
if myName is empty then exit mouseMove
put u,v into tPt
--Calculate the sum of the distances to the two foci
put distanceBetween(p1, tPt) + distanceBetween (p2,tPt) into tDist
--If the sum of the distances to each focus is less
--or equal to 2*w then the point is within the ellipse. Otherwise
not.
if abs(tDist) <= 2*w then
put u,v & cr & true into msg box
else
put u,v & cr & false into msg box
end if
end mousemove
on mouseUP
put empty into myName
end mouseUP
function distanceBetween a,b
put item 1 of a into x1
put item 1 of b into x2
put item 2 of a into y1
put item 2 of b into y2
return sqrt((x1-x2)^2 + (y1-y2)^2)
end distanceBetween
More information about the use-livecode
mailing list