Luke,
Interesting problem. I had to brush up on my high school math to try
to figure it out. This may not be exactly right, but it should push
you in the right direction. I have NOT tested this, but I think it
will work. Here's a basic algorithm.
Use the basic equation of a line: y = mx + b.
Use the known points on the line to determine the slope and y intercept.
Once you know the equation of the line, test the points at the left,
top, right, and bottom of the rectangle and see if the line
intersects any of the four lines which make up the side of the
rectangle.
There certainly could be errors in the stuff below, but at least it
give you a way to approach the problem.
First, pull out the points that you need to consider into simple
local variables:
on LineIntersectsRect point1, point2, theRect -- returns TRUE or FALSE
h1 = point1.locH
v1 = point1.locV
h2 = point2.locH
v2 = point2.locV
hMin = min(h1, h2)
hMax = max(h1, h2)
vMin = min(v1, v2)
vMax = max(v1, v2)
rectLeft = theRect.left
rectTop = theRect.top
rectRight = theRect.right
rectBottom = theRect.bottom
Now, you need to figure out the equation for the line based on the
two points that you have. The equation for a line is: y = mx + b
where m is the "slope" of the line, and b is the y intercept (the
place where the line intersects the y axis. The slope is defined as
the rise over the run, or in lingo terms:
theSlope = float(vMax - vMin) / float(hMax - hMin)
-- now that we know the slope, calculate the y intercept by turning
the equation:
-- y = mx + b, around to
-- b = y - mx, while plugging in one of the points
theYIntercept = v1 - (theSlope * h1)
Now that we have the equation of the line, we need to see if the line
now intersects any of the top bottom, left, or right of our rect. We
do this by trying all four parts in our line equation - first the
left and right:
-- for the Left and Right, we know the x (or h) value
theV = (theSlope * rectLeft) + theYintercept
if (theV >= rectBottom) and (theV <= rectTop) then
return TRUE
end if
theV = (theSlope * rectRight) + theYIntercept
if (theV >= rectBottom) and (theV <= rectTop) then
return TRUE
end if
-- Now turn the y = mx + b equation around for the top and the bottom
-- mx = y - b
-- x = (y - b) / x
theH = (rectBottom - theYIntercept) / theSlope
if (theH >= rectLeft) and (theH <= rectRight) then
return TRUE
end if
theH = (rectBottom - theYIntercept) / theSlope
if (theH >= rectLeft) and (theH <= rectRight) then
return TRUE
end if
theH = (rectTop - theYIntercept) / theSlope
if (theH >= rectLeft) and (theH <= rectRight) then
return TRUE
end if
-- otherwise, it fell through all test, does not intersect
return FALSE
end
Good luck,
Irv
At 3:23 PM +1100 12/3/01, Luke Wigley wrote:
>Hi Listers,
>
>I can't seem to solve this simple problem properly - I need to test whether
>a line ([pointA, pointB]) intersects a rect or quad ([point1, point2,
>point3, point4]). Can someone point me in the right direction?
>
>Cheers,
>
>Luke
--
Lingo / Director / Shockwave development for all occasions.
(Home-made Lingo cooked up fresh every day just for you.)
[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/lingo-l.cgi To post messages to the list,
email [EMAIL PROTECTED] (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo. Thanks!]