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
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.
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.
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.
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.
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 .
min_x
max_x
(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.
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) }
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