Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

geometry

Given the input:

double x1,y1,x2,y2; 

How can I find the general form equation (double a,b,c where ax + by + c = 0) ?

Note: I want to be able to do this computationally. So the equivalent for slope-intercept form would be something like:

double dx, dy; double m, b;  dx = x2 - x1; dy = y2 - y1; m = dy/dx; b = y1; 

Obviously, this is very simple, but I haven't been able to find the solution for the general equation form (which is more useful since it can do vertical lines). I already looked in my linear algebra book and two books on computational geometry (both too advanced to explain this).

like image 262
mimicocotopus Avatar asked Nov 06 '12 00:11

mimicocotopus


People also ask

What is the general form of a line?

General form of a line The general form ax+by+c=0 is one of the many different forms you can write linear functions in. Other ones include the slope intercept form y=mx+b or slope-point form. We can convert the linear function among different forms.

How do you find the general equation of a line?

The general equation of a straight line is y = mx + c, where m is the gradient, and y = c is the value where the line cuts the y-axis. This number c is called the intercept on the y-axis. The equation of a straight line with gradient m and intercept c on the y-axis is y = mx + c.


2 Answers

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.
like image 63
plesiv Avatar answered Sep 22 '22 22:09

plesiv


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.

like image 38
Antimony Avatar answered Sep 23 '22 22:09

Antimony