Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Bounding box coordinates from a rotated rectangle

I have the coordinates of the top left point of a rectangle as well as its width, height and rotation from 0 to 180 and -0 to -180.

I am trying to get the bounding coordinates of the actual box around the rectangle.

What is a simple way of calculating the coordinates of the bounding box

  • Min y, max y, min x, max x?

The A point is not always on the min y bound, it can be anywhere.

I can use matrix the transform toolkit in as3 if needed.

like image 803
coulix Avatar asked Mar 07 '09 17:03

coulix


People also ask

How do you find the bounding box of a rectangle?

The bottom of the rectangle is determined by the y-coordinate of the lowest point - point D. The left side of the rectangle is determined by the x-coordinate of the leftmost point - point A. The right of the rectangle is determined by the x-coordinate of the rightmost point - point C.

How do you find the points of rotation of a rectangle?

The essence is this: (1) If c is the center point, then the corners are c + (L/2,W/2), +/- etc., where L and W are the length & width of the rectangle. (2) Translate the rectangle so that center c is at the origin, by subtracting c from all four corners. (3) Rotate the rectangle by 40 deg via the trig formulas cited.

Can bounding boxes be rotated?

Figure 7. Bounding boxes are constructed by first creating an axis-aligned box (left), and then rotating by theta (right). Many datasets (for example, COCO and ISPRS) come with segmentation masks. These masks can be converted into rotated bounding boxes by using a geometry package.

How do you calculate rotation coordinates?

Coordinates of Rotation: The point (x,y) rotated an angle of θ counter-clockwise about the origin will land at point (x′,y′) where x′=xcos(θ)−ysin(θ) x ′ = x cos ⁡ ( θ ) − y sin ⁡ and y′=ycos(θ)+xsin(θ) y ′ = y cos ⁡ ( θ ) + x sin ⁡ .


2 Answers

  • Transform the coordinates of all four corners
  • Find the smallest of all four x's as min_x
  • Find the largest of all four x's and call it max_x
  • Ditto with the y's
  • Your bounding box is (min_x,min_y), (min_x,max_y), (max_x,max_y), (max_x,min_y)

AFAIK, there isn't any royal road that will get you there much faster.

If you are wondering how to transform the coordinates, try:

x2 = x0+(x-x0)*cos(theta)+(y-y0)*sin(theta) y2 = y0-(x-x0)*sin(theta)+(y-y0)*cos(theta) 

where (x0,y0) is the center around which you are rotating. You may need to tinker with this depending on your trig functions (do they expect degrees or radians) the sense / sign of your coordinate system vs. how you are specifying angles, etc.

like image 85
MarkusQ Avatar answered Oct 02 '22 06:10

MarkusQ


I realize that you're asking for ActionScript but, just in case anyone gets here looking for the iOS or OS-X answer, it is this:

+ (CGRect) boundingRectAfterRotatingRect: (CGRect) rect toAngle: (float) radians {     CGAffineTransform xfrm = CGAffineTransformMakeRotation(radians);     CGRect result = CGRectApplyAffineTransform (rect, xfrm);      return result; } 

If your OS offers to do all the hard work for you, let it! :)

Swift:

func boundingRectAfterRotatingRect(rect: CGRect, toAngle radians: CGFloat) -> CGRect {     let xfrm = CGAffineTransformMakeRotation(radians)     return CGRectApplyAffineTransform (rect, xfrm) } 
like image 21
Olie Avatar answered Oct 02 '22 06:10

Olie