How to get size of a rotated rectangle [duplicate]
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
a = abs(x * sin(o)) + abs(y * cos(o))
b = abs(x * cos(o)) + abs(y * sin(o))
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
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
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.
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
Calculate the area of the original rectangle. Area doesn't change under rotation.
It's a bit complicated, but for a rectangle, Area = b * h = length * width
.
精彩评论