[Math] Intersection of 2 lines in 2D via General Form Linear Equations

algebraic-geometrygeometry

). 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 equation of line of the form $ax + by + 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).

Best 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.

Related Question