# Intersection of 2 lines in 2D via General Form Linear Equations

). Recently, I asked you how to find the "Intersection of 2 Lines in 2D" and the answers revolved around the Determinants ( http://en.wikipedia.org/wiki/Line-line_intersection ) or Systems ( Intersection Of Two Lines In 2D ).

Now, based on what I learned here from Isaac about how to get the General Form Linear Equation from 2 points ( How to obtain Line Equation of the form ax + bx + c = 0 ), isn't it simpler to find the intersection like this?

Where x1 to x4 and y1 to y4 are the 4 points defining the 2 lines.

  //First line's Equation.
var a: Number = y1 - y2;
var b: Number = x2 - x1;
var c: Number = (y2 - y1) * x1 - (x2 - x1) * y1;

//Second line's Equation.
var d: Number = y3 - y4;
var e: Number = x4 - x3;
var f: Number = (y4 - y3) * x3 - (x4 - x3) * y3;

//Below formulas obtained via System.
intersection.y = (c*d - f*a) / (-d*b + a*e);
intersection.x = (-b * intersection.y - c) / a;


It seems to me to be less mathematical operations here compared to the determinants solution. Does this method seem ok to you, mathematically speaking? I like that it's slope-independent (vertical lines result in useless checks in those cases).

-

## 1 Answer

Assuming your ret.y is intersection.y, your results look good to me. If you want to make sure that you are safe for all lines, you probably should change the intersection.x formula so that it doesn't depend on a being nonzero: $$x=\frac{bf-ce}{-db+ae}$$ (If $-db+ae=0$, which both $x$ and $y$ have in the denominator, the two lines do not intersect.)

These formulae for $x$ and $y$ in terms of $a$, $b$, $c$, $d$, $e$, and $f$ are equivalent to applying Cramer's rule (beware that the use of $a$, $b$, $c$, $d$, $e$, and $f$ there are not quite the same), which uses determinants.

-
Hi Isaac! ::- D. Ouch, I forgot to modify that 'ret' to 'intersection'. You are correct (I just found out the hard way), my solution depends on a and b being non-zero so I think I'm going to use the determinants after all. Thank you for bringing this case up ::- D. – Axonn Mar 16 '11 at 13:08
Third time you're helping me out. Thank you VERY much ::- D. I learned a lot from you and the others here. 2 weeks ago I wouldn't have been able to come up even with such a broken solution like I did now ::- D. – Axonn Mar 16 '11 at 13:38
@Axonn: You're welcome. And I wouldn't have called this a broken solution at all. Other than the case where $a=0$, it works just fine, and it's a relatively minor change to correct for that. – Isaac Mar 16 '11 at 13:42