Search code examples
mathpolygonbounding-box

Get smallest bounding box for a polygon that is large enough despite orientation


I am currently getting the bounding box for my polygon by getting the min/max x and min/max y of the points, but when rotating the polygon the bounding box is too small to fit the rotated polygon. See the illustration for clarification:

This:

Polygon unrotated

Turns into this:

Polygon rotated

How would I get the bounding box that is big enough to contain any rotated state?


Solution

  • If I understand the problem correctly, this is really trivial.

    The point furthest away from the center will always be a vertex. So find the vertex with the maximum distance from the center, and make the box large enough to fit the polygon when that vertex is facing straight up, down, left and right:

    1. Find the vertex furthest away from the center, and let d denote its distance from the center.
    2. The polygon will always fit in the box 2d × 2d.