How to compute the coefficients of the general form of a line $ax + by + d = 0$ in a way that helps to find the plane equation $ax+by+cz+d=0$

55 Views Asked by At

I have a bunch of points $(x,y,z)$. I want to compute the best fit line equation going through those points in 2D (I want the line in 2D so I just ignored the z axis for the line). I have done that using the line equation in the form $y=mx+n$ and the least squares:

I have many points, thus many equations: $$ \begin{bmatrix} y_1 \\ y_2 \\ . \\ . \\ . \\ y_n \end{bmatrix} = \begin{bmatrix} x_1 & 1 \\ x_2 & 1 \\ . \\ . \\ . \\ x_n & 1 \end{bmatrix} \begin{bmatrix} m \\ n \\ \end{bmatrix} $$ To simplify, the y vector is "$B$", $x_1$ is "$A$" and coefficients "$X$": $B=AX \Leftrightarrow X=(A^TA)^{-1}A^TB$

Using $y=mx+n$ with the calculated coefficients, I now calculated the plane going through the 3D points. I needed 3 points so I calculated 2 points in the line by arbitrarily choosing a "$x$" value to find "$y$" and took a random other point in my data set that doesn't belong to the line. I now have 3 points $A$,$B$ and $C$, I do the cross product of $(A-B*A-B)$ and get $a,b,c$ from the plane equation $ax+by+cz+d=0$, then I can get d using those values and a point.

$$$$

I now have a line equation $y=mx+n$ and plane equation $ax+by+cz+d=0$

However I would like to calculate the best fit line equation using this line equation form: $ax+by+d=0$ in a way that would allow me to extend/slightly adjust to also calculate the plane equation.

I know that with 2 known points and by using the point-slope form of the line equation $${y-y_B}={\textstyle y_B-y_A\over \textstyle \strut x_B-x_A}(x-x_B)$$

I can then have this: $$(y_A-y_B)x-(x_A-x_B)y+x_Ay_B-x_By_A=0$$

Meaning I'd have: $$a=(y_A-y_B)\\b=(x_A-x_B)\\d=(x_Ay_B-x_By_A)$$

However that's for 2 points only, not a best fit line.

I also know that with this general form $ax+by+d=0$ I get $y=-{\textstyle a\over \textstyle \strut b}x-{\textstyle d\over \textstyle \strut b}$ but I don't know how to get $b$ and how to use that in a way that would allow me to "extend/adjust" to find the plane equation.