geometry-challenged
Trevor DeVore
lists at mangomultimedia.com
Mon Jan 26 13:47:19 EST 2004
On Jan 26, 2004, at 10:51 AM, Dar Scott wrote:
>
> On Monday, January 26, 2004, at 10:47 AM, Jim Hurley wrote:
>
>> One practical way to deal with the parallel lines case is to make
>> them converge to a point at a very great distance--see below. Or, of
>> course, you could return a "Lines parallel" message.
>
> In the original problem, testing whether two line _segments_ intersect
> would be handy. Getting the intersection point along the way could be
> a byproduct of such a function.
I played around with this a bit yesterday after Dar mentioned that part
of the first handler I sent looked fishy and seeing Jim's example. It
was since the intersect points were not correct with some button
shapes. This function will check if two lines intersect and return the
x,y coordinates if they do. If not it returns empty. I *think* it
covers all divide by zero errors which could occur. The possible
errors I thought of were 1) both lines are vertical, 2) both lines are
horizontal or 3) one line is vertical/horizontal. I used an if
statement to catch cases 1 and 2 and the try/catch approach to catch
for the 3rd case. Just another version for people to play with.
--
Trevor DeVore
Blue Mango Multimedia
trevor at mangomultimedia.com
function libPlot_IntersectAtPoint p1Start, p1End, p2Start, p2End
local x1, y1, x2, y2
local xx1, yy1, xx2, yy2
local m1, m2, b1, b2
local intersectX, intersectY
local theIntersect
put item 1 of p1Start into x1
put item 2 of p1Start into y1
put item 1 of p1End into x2
put item 2 of p1End into y2
put item 1 of p2Start into xx1
put item 2 of p2Start into yy1
put item 1 of p2End into xx2
put item 2 of p2End into yy2
## Check for two parallel or two vertical lines
if ((x1= x2 AND xx1 = xx2) OR (y1 = y2 AND yy1 = yy2)) then return
empty
## Calculate slope for line 1
try
put (y2-y1) / (x2-x1) into m1
## solve for intercept b = y - mx
put y1 - (m1*x1) into b1
catch tError
## Vertical line
return x1, round( yy2 + (x1-xx2) *(yy2-yy1)/(xx2-xx1) )
end try
## Calculate slope for line 2
try
put (yy2-yy1) / (xx2-xx1) into m2
put yy1 - (m2*xx1) into b2
catch tError
return xx2, round( y2 + (xx1-x2) *(y2-y1)/(x2-x1) )
end try
## If slopes match then lines are parallel
if (m1 = m2) then return empty
## solve for y = (b1 * m2 - b2 * m1) / (m2 - m1)
put round( ((b1 * m2) - (b2 * m1)) / (m2 - m1) ) into intersectY
## solve for x
put round( (intersectY - b2) / m2 ) into intersectX
return intersectX, intersectY
end libPlot_IntersectAtPoint
More information about the use-livecode
mailing list