13

Possible Duplicate:
Calculate Bounding box coordinates from a rotated rectangle, Picture inside.

I have a rotated rectangle, So how do i calculate the size of axis-aligned bounding box for the rotated rectangle in 2D Coordinates?

Attach Image http://img88.imageshack.us/img88/503/rotp.png

i know x, y, o (angle) but how do i get a, b

Thank you

Community
  • 1
  • 1
Northern
  • 189
  • 1
  • 1
  • 8

7 Answers7

52
a = abs(x * sin(o)) + abs(y * cos(o))
b = abs(x * cos(o)) + abs(y * sin(o))
Jpsy
  • 20,077
  • 7
  • 118
  • 115
Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
  • 1
    This isn't working for me... Given: `O=75, Y=39, X=105`, the results are `A=-4.76877, B=81.66039` which are obviously incorrect! – Campbeln May 02 '12 at 04:02
  • It is in radians.. so convert the degrees to radians. (Google's calculator does this) – Souleiman Dec 24 '12 at 01:49
  • 3
    Sorry, not correct - even in radians. I have added abs() terms to your answer now - with that change it should really be correct. (Original version was ``a = x * sin(o) + y * cos(o)`` and ``b = x * cos(o) + y * sin(o)``, which leads to the miscalculation that Campbeln describes above.) – Jpsy Oct 22 '13 at 23:02
  • 7
    Since the picture is missing...`x` is the original width, `y` is the original height, `a` is the new bounding-box's height, and `b` is the new bounding-box's width. With those definitions, the formula works fine for me. – ulatekh Jul 26 '16 at 22:55
  • 2
    out of all the answers on here for calculating the bounding box of rotated rectangle, this one gave me the correct results. FYI: degrees = o * (Math.PI/180); – slappy-x Apr 02 '17 at 01:08
  • @slappy-x Thanks a lot, your comment take out me from the hell. Put your comment in the answer, I'll vote up it – Neeraj Rathod Mar 30 '18 at 12:24
  • This works perfectly, however keep it in mind that a would be y and b would be x. Also, if you want it vice versa (from rotated sizes to sizes of the object): y = (x'/cos(o) + y'/sin(o)) / (tan(o)-cot(o)) x = (y'/sin(o) - (x'/cos(o)-y'/sin(o)) / (tan2(o)-1) – Navid Naderi Jul 02 '22 at 09:05
10

To construct an axis-aligned bounding box, one must find the extreme points of the rotated box. i.e.,

given a rectangle 'P', given by points P1=(0,0), P2=(x,0), P3(x,y), P4(0,y), rotated 'R' degrees; find minX, maxX, minY, maxY, such that the box [(minX,minY),(maxX,maxY)] completely bounds the rotated 'P'.

                          +-------P3'----+maxY
                          |     /    \   |
  P4------P3              |   /        \ |
   |      |    rotate     | /            P2'
   |      | => by 'R' =>  P4'           /|
   |      |    degrees    | \         /  |
  P1------P2              |   \     /    |
                          |     \ /      |
                          +-----P1'------+minY
                         minX           maxX

The values for the bounding box are the minimum/maximum of the components of the rotated points P1'..P4'; thus,

minX=min(P1'[x],P2'[x],P3'[x],P4'[x])
maxX=max(P1'[x],P2'[x],P3'[x],P4'[x])
minY=min(P1'[y],P2'[y],P3'[y],P4'[y])
maxY=max(P1'[y],P2'[y],P3'[y],P4'[y])

For a discussion of 2D rotations, see http://en.wikipedia.org/wiki/Transformation_matrix#Rotation

Cédric
  • 334
  • 2
  • 8
eric
  • 111
  • 2
2

Well you didn't give a whole lot of detail. I'm assuming you know that the height and width of the rectangle will give you the area no matter the rotation. If you only have the x,y data points then you use the sqrt((x1-x1)^2 + (y1-y2)^2). To get the length of a side.

You clarified your question so if you have a rectangle and you know the angle from the top left corner is rotated away from the top so the left side looks like this.
  /
/
a = sine(alpha)*width
b = cosine(alpha)*width
c = sine(alpha)*height
d = cosine(alpha)*height

width = a + d
height = b + c
Be sure you get the angle right it is kind of hard to clarify it on here. If you get the other angle then it will come out to
width = b + c
height = a + d

qw3n
  • 6,236
  • 6
  • 33
  • 62
0

For the axis aligned box of the rotated rectangle, you find the minimum and maximum of each of the 4 rotated coordintates. The minX and minY becomes 1 corner and the maxX and maxY becomes the other corner.

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
-1

Use [Heron's Formula Triangle Area Calculator] s = (a + b + c) / 2 or 1/2 of the perimeter of the triangle

A = SquareRoot(s * (s - a) * (s - b) * (s - c))

Where

a=SquareRoot((X1-X2)^2+(Y1-Y2)^2)  [Side 1 Length]
b=SquareRoot((X1-X3)^2+(Y1-Y3)^2)  [Side 2 Length]
c=SquareRoot((X2-X3)^2+(Y2-Y3)^2)  [Side 3 Length]

X1,Y1,X2,Y2,X3,Y3 are the coordinations of any three points (Corners)

RectangleArea=2*A

Or Direct without [Heron's Formula Triangle Area Calculator], sequence of points are important here.

P1----P2
|     |
P3----P4

 a=SquareRoot((X1-X2)^2+(Y1-Y2)^2)  [Side 1 Length]
 b=SquareRoot((X1-X3)^2+(Y1-Y3)^2)  [Side 2 Length]
 RectangleArea=a*b
Waleed A.K.
  • 1,596
  • 13
  • 13
-3

Calculate the area of the original rectangle. Area doesn't change under rotation.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
-4

It's a bit complicated, but for a rectangle, Area = b * h = length * width.

tpdi
  • 34,554
  • 11
  • 80
  • 120