Skip to main content
December 25, 2007
Question

[Math] Finding the intersection of two lines

  • December 25, 2007
  • 7 replies
  • 761 views
Does anyone here happen to know how to find the intersection of two lines? kglad, perhaps?

My situation is one or both of two possible scenarios. The difference in these two scenarios is how the lines are defined, and I am able to accommodate either:

Scenario 1: To define the two lines I have one point each, thus two known points, and with those points a known angle ranging from -180 to 180 -- this is one point and one angle per line. Illustrated here:
http://abeall.com/files/temp/vector.intersect.gif
(p3 in blue is the unknown point to be solved)
The angles face in a direction such that the lines will intersect, ie the intersection won't happen in the opposite direction of the angle, if that makes sense.

Scenario 2: I have two pairs of points to make the lines, thus four points total -- two points per line. The intersection may be on the line segments, or it may be extended beyond the line segments in either direction.

The lines will never be parallel.

I've spent a few hours researching online and found some possible solutions, by not being very mathematically inclined I get quite lost in all the notation and have had no luck creating an ActionScript translation.

One example that might be what I need:
http://www.faqs.org/faqs/graphics/algorithms-faq/
^ search for "Subject 1.03: How do I find intersections of 2 2D line segments?"

Any help would be greatly appreciate. Thanks!
This topic has been closed for replies.

7 replies

kglad
Community Expert
Community Expert
December 31, 2007
you're welcome.
December 31, 2007
Excellent, that works. Thanks again!
kglad
Community Expert
Community Expert
December 30, 2007
the slope of a vertical line is infinite. so, in the situation where you're trying to find the intersection of a vertical line with any other (non-vertical) line, use the x coordinate of the vertical line and solve for y on your non-vertical line.

eg, if your vertical line is defined by x=3 and your non-vertical line is defined by y=mx+b, the intersection is:

(3,m*3+b);
December 30, 2007
Hey kglad, I'm encountering some behavior I don't understand, wondering if you could shed some light.

I'm using the vector method(pointPointF) and it appears that in the event that a vector is exactly vertical, the intercept returns Infinity or -Infinity. The vectors do intersect, they are not parallel. Horizontal seems to work fine.

Any thoughts would be appreciated. Thanks!
kglad
Community Expert
Community Expert
December 25, 2007
you're welcome.
kglad
Community Expert
Community Expert
December 25, 2007
your x and y need to be switched in atan2.
December 25, 2007
Perfect! Thanks!
kglad
Community Expert
Community Expert
December 25, 2007
:

December 25, 2007
Thanks, kglad!

The method using two points per line is working perfectly. Awesome!

However, the angle version is not working for me. Here is the function I'm using to determine angle:
function angle(p1,p2){
return -Math.atan2((p1.x-p2.x), (p1.y-p2.y))/(Math.PI/180);
}

Does that need to be adjusted?