# Finding the Best Fitting Plane Given a Set of 3D Points

Suppose that we have $n$ points in 3D.

I want to find a plane $ax + by + cz + d$ such that sum of all the orthogonal distances to the plane is minimum.

So far, this answer from SO was the simplest one. However, this solution projects the points onto $z=0$ plane all the time.

Could you help me to write the algorithm? I'll code it in Java.

• I don't know what you mean by "However, I need an algorithmic solution." Least squares is an algorithmic solution. It does have the problem that it doesn't solve your problem -- it solves a related optimization problem, but not your problem -- but it's certainly an algorithmic solution, as there are standard algorithms to compute the least squares fit plane. – D.W. Jan 13 '15 at 0:30

[Least squares fitting is almost a solution to this problem, except that it doesn't minimize the orthogonal distance -- it minimizes the distance along the $y$ axis, or any single axis you might pick.]