I am toying around with a shape problem and I am looking for a more clever solution than what I have been able to come up with.
Here is the problem:
I have a set of points that form an enclosed shape on a Cartesian grid, say A(-1,0),B(1,0), and C(0,4) which forms an acute triangle.
I have reworded this in a slightly less confusing way. Take the shape above and imagine that you can freely rotate it. I am looking to discover the rotation where we only take into account the x-axis and the distance between the western most and eastern most point is at its smallest.
When taking into account the shape above that distance would be the distance between A and B. While for more interesting shapes there may be shorter distances between points, I believe there is no way to rotate the shape above such that the western and eastern most points is less that the distance between A and B.
My only solution thus far has been to plot the points, rotate 1 degree, store the maximum distance keyed by the rotation. Rinse repeat and take the smallest one. This seems a bit kludgy and I know there has to be a more mathematically sound way to approach this.
Any ideas?
Do the principal component analysis and align the principle component to the y axis. This will optimize the “mean” square distance from every point to the y axis (i.e. width on x axis). Perhaps this is also optimal or close to the optimal by your criteria.
See: http://en.wikipedia.org/wiki/Principal_component_analysis
Alternative Solution (Optimal solution):
First calculate the convex hull of the points. Note that the two points with maximum x width are always going to in the convex hull.
Now, for every line segment in the convex hull find the vertex that is farthest and note down the distance. Find the (line segment, farthest vertex) pair with minimum distance. Optimal rotation is the one that aligns that line segment in vertical direction.
Complexity:
O(nlogn)for the convex hull part andO(m^2)for the second part where m is the number of points on the convex hull.