I have a certain number of points/vectors that approximately lie on a line in 3D space. Are there any existing functions in unity that can fit a line through a cluster of 3D points/vectors?
The problem can be solved mathematically with a orthogonal linear regression (amongst others), that minimizes the squared error. However, it’s been ages since I’ve worked with stuff like principal component analysis, eigenvectors, eigenvalues and so on, so implementing it myself in C# would be an enourmous hustle. Unfortunately I haven’t found any C# implementation either.
A solution I’m looking for doesn’t have to be as mathematically optimal as the mentioned regression, I simply want to get a rough mean direction vector from my points.
I thought about fitting a cylinder or a quad around the point cluster and using the line that goes through the centers of the capping circles/squares. Does anyone have an idea how to go about doing that script wise?
A very simple way to get an extremely rough plot is to take the average of all points and plot a line from the average of a few points form the beginning to the average of all points. This of course requires you to have sorted your points somehow, e.g. first by x, then y and finally z. Radix sort is good for this purpose. If you believe in outliers, be warned that this method is very sensitive to them in your data set, as they will strongly skew the regression.
now for 3D you can basically just do the 2D Algorithm two times… first by only using x/y and then by looking from above and using x/z instead, so you get all components to build your 3D line