I'm struggling with calculating the smallest enclosing rectangle (arbitrarily aligned) of a set of points.
I was able to calculate the convex hull using graham's algorithm.
Where I'm stuck is the next step. I thought about using the rotating calipers method, but I can't seem to find an adequate explanation of the algorithm.
If you have the convex hull, then a basic algorithm easy. You just go through each edge of the convex hull and rotate your points so that edge is along a major axis, let's say the X-axis. Then you find an axis-aligned bounding box of those points. Choose whichever bounding box is smallest.
An axis-aligned bounding box can be found by getting the minimum and maximum in each dimension.
If you rotate your bounding box by the opposite amount, it will now enclose your original points.
To make this more efficient, note that the only points that can affect the bounding box are on the convex hull.
To make it really efficient, note that there are only four points around the convex hull that are touching the bounding box at any one time (this isn't strictly true, but ignore that for now). If you rotate the points just enough that the next edge is aligned with the bounding box, then three of those points are the same and one of the points is replaced with the next point around the convex hull. This lets you create an algorithm that is linear in the number of points on the convex hull.
Now there are special cases where two edges are parallel or perpendicular. This will cause more than four points to be touching the bounding box at any one time, but it actually doesn't matter. If you have a choice between which of two parallel edges to use next, you can just pick one arbitrarily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With