Given the input:
double x1,y1,x2,y2;
How can I find the general form equation (double a,b,c where ax + b
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
.
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
.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:
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);
}
Shortcut steps: "Problem : (4,5) (3,-7)" Solve: m=-12/1 then 12x-y= 48 "NOTE:m is a slope" COPY THE NUMERATOR, AFFIX "X" Positive fraction Negative sign on between. (tip: simmilar sign = add + copy the sign) 1.Change the second set into opposite signs, 2.ADD y1 to y2 (means add or subtract them depending of the sign), 3.ADD x1 to x2 (also means add or subtract them depending of the sign), 4.Then Multiply 12 and 1 to any of the problem set. After that "BOOM" Tada!, you have your answer
#include <stdio.h>
main()
{
int a,b,c;
char x,y;
a=5;
b=10;
c=15;
x=2;
y=3;
printf("the equation of line is %dx+%dy=%d" ,a,b,c);
}