I read the Calculate Bounding box coordinates from a rotated rectangle to know how to calculate bounding box coordinates from a rotated rectangle. But in a special case as follow image:
How to get the rotated rectangle size if had get the bounding box size, coordinates and rotate degree?
I try write code in javascript
//assume w=123,h=98,deg=35 and get calculate box size
var deg = 35;
var bw = 156.9661922099485;
var bh = 150.82680201149986;
//calculate w and h
var xMax = bw / 2;
var yMax = bh / 2;
var radian = (deg / 180) * Math.PI;
var cosine = Math.cos(radian);
var sine = Math.sin(radian);
var cx = (xMax * cosine) + (yMax * sine) / (cosine * cosine + sine * sine);
var cy = -(-(xMax * sine) - (yMax * cosine) / (cosine * cosine + sine * sine));
var w = (cx * 2 - bw)*2;
var h = (cy * 2 - bh)*2;
But...the answer is not match w and h
You'll probably need something like affine transformation to discover point coordinates. And then using standard geometry formulas calculate the size.
Solution
Given bounding box dimensions
bx
byby
andt
being the anticlockwise rotation of rectangle sizedx
byy
:Derivation
Why is this?
First, consider that the length
bx
is cut in two pieces,a
andb
, by the corner of the rectangle. Use trigonometry to expressbx
in terms ofx
,y
, andtheta
:and similarly for
by
:1 and 2 can be expressed in matrix form as:
Note that the matrix is nearly a rotation matrix (but not quite - it's off by a minus sign.)
Left-divide the matrix on both sides, giving:
The matrix inverse is easy to evaluate for a 2x2 matrix and expands to:
[5] gives the two formulas:
Easy as pie!