Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the Bounding Rectangle at an Angle of a Polygon

I have the need to determine the bounding rectangle for a polygon at an arbitrary angle. This picture illustrates what I need to do:

alt text http://kevlar.net/RotatedBoundingRectangle.png

The pink rectangle is what I need to determine at various angles for simple 2d polygons.

Any solutions are much appreciated!

Edit:

Thanks for the answers, I got it working once I got the center points correct. You guys are awesome!

like image 675
kevin42 Avatar asked May 20 '09 18:05

kevin42


People also ask

What is meant by bounding rectangle?

The bounding box of an element is the smallest possible rectangle (aligned with the axes of that element's user coordinate system) that entirely encloses it and its descendants.

What are bounding box extents?

In computational geometry, the minimum bounding rectangle (MBR), also known as bounding box (BBOX) or envelope, is an expression of the maximum extents of a two-dimensional object (e.g. point, line, polygon) or set of objects within its x-y coordinate system; in other words min(x), max(x), min(y), max(y).

How do you rotate bounding boxes?

Rotating bounding boxes are represented using coordinates of the center point (x, y), width w, height h, and θ. Rotation Angle (θ) is the horizontal axis (the X-axis) counterclockwise to deal with the rectangle on the edge of the first Angle. (b) Normalization strategy for the representation of rotating bounding box.


2 Answers

To get a bounding box with a certain angle, rotate the polygon the other way round by that angle. Then you can use the min/max x/y coordinates to get a simple bounding box and rotate that by the angle to get your final result.

From your comment it seems you have problems with getting the center point of the polygon. The center of a polygon should be the average of the coordinate sums of each point. So for points P1,...,PN, calculate:

xsum = p1.x + ... + pn.x;
ysum = p1.y + ... + pn.y;
xcenter = xsum / n;
ycenter = ysum / n;

To make this complete, I also add some formulas for the rotation involved. To rotate a point (x,y) around a center point (cx, cy), do the following:

// Translate center to (0,0)
xt = x - cx;
yt = y - cy;
// Rotate by angle alpha (make sure to convert alpha to radians if needed)
xr = xt * cos(alpha) - yt * sin(alpha);
yr = xt * sin(alpha) + yt * cos(alpha);
// Translate back to (cx, cy)
result.x = xr + cx;
result.y = yr + cx;
like image 200
schnaader Avatar answered Sep 20 '22 00:09

schnaader


To get the smallest rectangle you should get the right angle. This can acomplished by an algorithm used in collision detection: oriented bounding boxes. The basic steps:

Get all vertices cordinates
Build a covariance matrix
Find the eigenvalues
Project all the vertices in the eigenvalue space
Find max and min in every eigenvalue space.

For more information just google OBB "colision detection"

Ps: If you just project all vertices and find maximum and minimum you're making AABB (axis aligned bounding box). Its easier and requires less computational effort, but doesn't guarantee the minimum box.

like image 23
RMAAlmeida Avatar answered Sep 23 '22 00:09

RMAAlmeida