geometry – How can I find the general form equation of a line from two points?

geometry – How can I find the general form equation of a line from two points?

If you start from the equation y-y1 = (y2-y1)/(x2-x1) * (x-x1) (which is the equation of the line defined by two points), through some manipulation you can get (y1-y2) * x + (x2-x1) * y + (x1-x2)*y1 + (y2-y1)*x1 = 0, and you can recognize that:

  • a = y1-y2,
  • b = x2-x1,
  • c = (x1-x2)*y1 + (y2-y1)*x1.

Get the tangent by subtracting the two points (x2-x1, y2-y1). Normalize it and rotate by 90 degrees to get the normal vector (a,b). Take the dot product with one of the points to get the constant, c.

geometry – How can I find the general form equation of a line from two points?

If you start from the equation of defining line from 2 points

(x - x1)/(x2 - x1) = (y - y1)/(y2 - y1)

you can end up with the next equation

x(y2 - y1) - y(x2 - x1) - x1*y2 + y1*x2 = 0

so the coefficients will be:

  • a = y2 – y1
  • b = -(x2 – x1) = x1 – x2
  • c = y1*x2 – x1*y2

My implementation of the algorithm in C

inline v3 LineEquationFrom2Points(v2 P1, v2 P2) {
    v3 Result;

    Result.A = P2.y - P1.y;
    Result.B = -(P2.x - P1.x);
    Result.C = P1.y * P2.x - P1.x * P2.y;

    return(Result);
}

Leave a Reply

Your email address will not be published.