13

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.

mish
  • 1,055
  • 10
  • 29
  • Check out [this explanation](http://cgm.cs.mcgill.ca/~orm/maer.html). – mbeckish Dec 07 '12 at 14:43
  • 2
    Did you look at [Toussaint's original paper](http://cgm.cs.mcgill.ca/~godfried/publications/calipers.pdf)? Pages 1 and 2 explain the algorithm quite clearly (IMO). Note that the page numbering is reversed... – Bart Kiers Dec 07 '12 at 14:44
  • See the GIF here: http://www-cgrl.cs.mcgill.ca/~godfried/research/calipers.html – Thomas Ahle Oct 13 '22 at 18:32

1 Answers1

11

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.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • The only problem with this method is that it is O(n^2), which may not be suitable when there are many vertices in the convex hull. – mbeckish Dec 07 '12 at 14:46
  • @mbeckish: true -- I've extended my answer with a couple of optimizations. Maybe thinking of it as a simple algorithm with optimizations will make it easier to understand. – Vaughn Cato Dec 07 '12 at 14:59
  • It's not true that there are only four points around the convex hull that are touching the bounding box at any one time. Assuming collinear points do not occur in the convex hull, the bounding box can touch up to 8 points. Consider the convex hull `(2,0), (3,0), (4,1), (4,2), (3,3), (2,3), (1,2), (1,1)` with an axis aligned bounding box (or one tilted 45 degrees) touching all points. – Bart Kiers Dec 07 '12 at 15:58
  • @BartKiers: That's true. It's a simplification that makes it easier to understand the algorithm. I'll try to make that more clear. – Vaughn Cato Dec 07 '12 at 16:07
  • Yeah, sorry to be a bit pedantic :). It's just that when one starts simplifying computational geometry related algorithms, and certain corner cases are ignored, the implementation is likely to break [when the implemented algorithm encounters these corner cases]. – Bart Kiers Dec 07 '12 at 16:20
  • 3
    @BartKiers: Yep, computational geometry has a lot of corners and edges... :) – Vaughn Cato Dec 07 '12 at 17:06