Computing General Form Line Coefficients From Two Points

The general form of a line equation is given as ax + by + c=0. Given two points (x_1, y_1) and (x_2, y_2), we can compute coefficients a, b, and c as follows. First, compute the slope as rise over run m = (y_2 - y_1)/(x_2 - x_1). Second, plug this value m and one of the points into the point-slope form, given by y - y_1 = m(x - x_1), and gather all terms on the lefthand side:

    \begin{align*}y - y_1 &= \frac{y_2 - y_1}{x_2 - x_1}(x - x_1)\\ (y - y_1)(x_2 - x_1) &= (y_2 - y_1)(x - x_1)\\ yx_2 - yx_1 - y_1x_2 + x_1y_1 &= xy_2 - y_2x_1 - xy_1 + x_1y_1\\ yx_2 - yx_1 - y_1x_2 - xy_2 + y_2x_1 + xy_1 &= 0\\ xy_1 - xy_2 + yx_2 - yx_1 - y_1x_2 + y_2x_1 &= 0\\ (y_1 - y_2)x + (x_2 - x_1)y + y_2x_1 - y_1x_2 &= 0.\end{align*}

Finally, we can see that a = y_1 - y_2, b = x_2 - x_1, and c = y_2x_1 - y_1x_2.

Equivalence to Orient2D

It is useful to observe that the general form of a line equation can be used as an orientation predicate. Namely, given a third point (x_3, y_3), plugging the point into the line equation and observing the sign of the result will tell you if the point is to the left (negative sign), on (zero), or to the right (positive sign) of the oriented line through (x_1, y_1) and (x_2, y_2).

This implies an algebraic equivalence to the usual Orient2D predicate:

    \begin{align*}\begin{vmatrix}x_1 & y_1 & 1 \\ x_2 & y_2 & 1 \\ x_3 & y_3 & 1 \end{vmatrix} \\ \begin{vmatrix}x_1-x_3 & y_1-y_3 \\ x_2-x_3 & y_2-y_3\end{vmatrix} \\ (x_1-x_3)(y_2-y_3)-(y_1-y_3)(x_2-x_3) \\ x_1y_2-x_1y_3-y_2x_3+x_3y_3-(y_1x_2-y_1x_3-x_2y_3+x_3y_3) \\ x_1y_2-x_1y_3-y_2x_3-y_1x_2+y_1x_3+x_2y_3 \\ x_1y_2 - x_1y_3 - y_1x_2 + y_1x_3 + x_2y_3 - y_2x_3.\end{align*}

We can see this by plugging the third point into ax+by+c, using the values for a, b, and c we computed above:

    \begin{align*}(y_1-y_2)x_3+(x_2-x_1)y_3+(y_2x_1-y_1x_2) \\ y_1x_3-y_2x_3+x_2y_3-x_1y_3+x_1y_2-y_1x_2 \\ x_1y_2 - x_1y_3 - y_1x_2 + y_1x_3 + x_2y_3 - y_2x_3.\end{align*}

The following demo shows three points and highlights the third point in red when it is to the left, black when it is on, and green when it is to the right of the line through the other two points.





2 thoughts on “Computing General Form Line Coefficients From Two Points

  1. Pingback: Computing the Intersection of Two Lines | Clinton Freeman

  2. Pingback: Finding the largest area axis-parallel square with a known center in a polygon | Clinton Freeman

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.