Finding the Intersection of Two Lines

For lines that aren’t vertical.

1
2
3
4
5
function findIntersect(a1, b1, a2, b2):Point{
    var p:Point = new Point((b1-b2)/(a2-a1), 0);
    p.y = a1*p.x+b1;
    return p;
}

For any linear line:

1
2
3
4
5
6
7
function findIntersect(a1, b1, a2, b2):Point{
    var p:Point = new Point((b1-b2)/(a2-a1), 0);
   
    if(isNaN(a1)||isNaN(a2))p.x = (isNaN(a1))?b2:b1;
    p.y = (isNaN(a2))?a1*p.x+b1:a2*p.x+b2;
    return p;
}

Say you have 2 lines 5x+2 and -x-1.
Using slope-intercept form(ax+b), we can fill in the parameters.
5x+2:
a = 5
b = 2

-x-1:
a = -1
b = -1

So to find the point at which these 2 lines meet, you would call the function like so:

1
findIntersect(5, 2, -1, -1)

It would trace:

(x=-0.5, y=-0.5)

If you want to have a vertical line, put the slope as NaN and the y-intercept as whatever you want y to equal to.
So y = 5 would be:
a = NaN
b = 5

Comments

One Response to “Finding the Intersection of Two Lines”

  1. Martin Ristov on February 27th, 2010 6:20 PM

    Hi,you don’t know me,but I’m working as same as you guys,all that AS3 and AS2 coding. I’d like to get in touch with you. I have things to share such as AS3 and AS2 variable scanner (not the one you made),but a different one,with ability to browse the variables and many more things that me and my friend are working on .
    As I said,I’d like to get in touch to share things. Probably you got my e-mail but here goes again: martin.ristov7@gmail.com .
    Thank you,
    Martin

Got something to say?