Math wizardry
Jim Hurley
jhurley at infostations.com
Wed Mar 2 12:25:55 EST 2005
>
>Message: 1
>Date: Wed, 2 Mar 2005 10:41:27 -0500
>From: Richard Miller <wow at together.net>
>Subject: Math wizardry
>To: How to use Revolution <use-revolution at lists.runrev.com>
>Message-ID: <2eadbdcb635a7d516daedb0bcc373e3c at together.net>
>Content-Type: text/plain; charset=US-ASCII; format=flowed
>
>I've got two line graphics drawn on the screen. I need to find out if
>they intersect and, if so, what the angle is that is formed by their
>intersection. Any simple way to do this?
>
>Thanks.
>Richard Miller
>Imprinter Technologies
>
Richard,
Here are a couple of functions which will should be helpful:
function theLineAngle p1,p2
--Angle of line defined by the two points p1 and p2
put item 1 of p2 - item 1 of p1 into dx
put item 2 of p2 - item 2 of p1 into dy
put atan2(dy,dx) into tAngle
return tAngle
end theLineAngle
And
function intersection line1,line2
--Intersection point of two lines defined by line1 and line2
--Where line1 is defined by its two end points x1,y1,x2,y2
put item 1 to 2 of line1 into p1
put item 3 to 4 of line1 into p2
put item 1 to 2 of line2 into pp1
put item 3 to 4 of line2 into pp2
put item 1 of p1 into x1
put item 2 of p1 into y1
put item 1 of p2 into x2
put item 2 of p2 into y2
put item 1 of pp1 into xp1
put item 2 of pp1 into yp1
put item 1 of pp2 into xp2
put item 2 of pp2 into yp2
if x1 = x2 and xp1= xp2 then add .0001 to xp2
if x1 = x2 or xp1 = xp2 then
if x1 = x2 then
return x1&comma&yp2 + (x1-xp2)*(yp2-yp1)/(xp2-xp1)
else
return xp2&comma& y2 + (xp1-x2)*(y2-y1)/(x2-x1)
end if
end if
if (y2-y1)/(x2-x1) = (yp2-yp1)/(xp2-xp1) then add .0001 to y2--if
lines are parallel
put yp2 - y2 + x2*(y2-y1)/(x2 - x1) - xp2*(yp2 - yp1)/(xp2 - xp1)
into numerator
put ((y2 - y1)/(x2 - x1) -( yp2 - yp1)/(xp2-xp1)) into denom
put numerator / denom into x
put y2 + (x-x2) *(y2-y1)/(x2-x1) into y
return x & comma & y
end intersection
You may wish to handle the special cases were the lines intersect at
infinity differently. In the above function I have chosen to slightly
alter the given points so that the lines intersect at a great
distance (close to infinity, so to speak.)
The above intersection function assumes that these are effectively
the infinite lines of Euclidean geometry. The end points are simply
two points which define the infinite lines. To find out whether the
intersection point lies between the end points of each line you will
need to test whether the distance between the intersection point and
ALL the end points is less than the length of each line
respectively. (I can't imagine that sentence is clear.)
Other functions which might be helpful are the perpDist function (the
perpendicular distance between a point and a given line, and
thePerpProj function (the perpendicular projection of a given point
onto a line.) You can see how these work in my recent Bouncing Ball
post which can be retrieved by putting this into the msg box:
go url http://home.infostations.net/jhurley/BouncingBallTools.rev
Let me know if you have any problems.
Jim
P.S. I've currently lost my mind entirely and am working on a
simulation of pool. So far I've got the balls (just two balls)
colliding and rebounding so that they satisfy the physical laws of
collision dynamics. What fun.
More information about the use-livecode
mailing list