22

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:

https://i.stack.imgur.com/3UNfD.png

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Liao San Kai
  • 857
  • 1
  • 11
  • 17
  • what do you mean by "rectangle size"? if you rotate an object, you will obtain, well, the same object rotated... same lengths, same area ... if you have the result of the rotation and not the original object, just use the coordinates (you say you have them), computing distances between the "corner points" to get the length of each side. – ShinTakezou Apr 02 '12 at 05:26
  • I'm assuming, with reference to your "Case Image", that you have `bh`, `bw` and `theta`, and you want `w` and `h`? – Li-aung Yip Apr 02 '12 at 05:31
  • I think your Javascript code is nearly correct, but you're off by a few plus/minus signs. See my answer. – Li-aung Yip Apr 02 '12 at 08:51
  • see: http://stackoverflow.com/a/30157405/133327 – abernier Dec 09 '15 at 16:50

2 Answers2

53

enter image description here

Solution

Given bounding box dimensions bx by by and t being the anticlockwise rotation of rectangle sized x by y:

x = (1/(cos(t)^2-sin(t)^2)) * (  bx * cos(t) - by * sin(t))
y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t))

Derivation

Why is this?

First, consider that the length bx is cut in two pieces, a and b, by the corner of the rectangle. Use trigonometry to express bx in terms of x, y, and theta:

bx = b          + a
bx = x * cos(t) + y * sin(t)            [1]

and similarly for by:

by = c          + d
by = x * sin(t) + y * cos(t)            [2]

1 and 2 can be expressed in matrix form as:

[ bx ] = [ cos(t)  sin(t) ] * [ x ]     [3]
[ by ]   [ sin(t)  cos(t) ]   [ y ]

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:

[ x ] = inverse ( [ cos(t)  sin(t) ]    * [ bx ]                        [4]
[ y ]             [ sin(t)  cos(t) ] )    [ by ]

The matrix inverse is easy to evaluate for a 2x2 matrix and expands to:

[ x ] = (1/(cos(t)^2-sin(t)^2)) * [ cos(t) -sin(t) ] * [ bx ]           [5]
[ y ]                             [-sin(t)  cos(t) ]   [ by ]

[5] gives the two formulas:

x = (1/(cos(t)^2-sin(t)^2)) * (  bx * cos(t) - by * sin(t))             [6]
y = (1/(cos(t)^2-sin(t)^2)) * (- bx * sin(t) + by * cos(t))

Easy as pie!

Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
  • And this is what five years of university-level mathematics teaches you how to do. :P – Li-aung Yip Apr 02 '12 at 07:58
  • +1: Li-aung Yip: you put me to shame with the amount of time and effort you have put into this answer. – High Performance Mark Apr 02 '12 at 10:34
  • 3
    @HighPerformanceMark: Time and effort spent answering questions is proportional to how interesting the question is. This one [nerd sniped](http://xkcd.com/356/) me. ;) – Li-aung Yip Apr 02 '12 at 11:56
  • @Li-aungYip: I want do upvote,but my reputation is not enough.I will redo when I get 15 reputation. :) – Liao San Kai Apr 03 '12 at 02:31
  • Actually you can accept answers even when you're at 1 rep. Just click the "tick mark" under the voting buttons to accept the answer. Accepting answers gives you 2 reputation points! – Li-aung Yip Apr 03 '12 at 13:35
  • Stupid Question: Does this work with a non-negative T? EG: Will this work with a rotation of 35 degrees? Or 325 degrees (aka -35 degrees)? Thanks! – Campbeln May 01 '12 at 01:17
  • @Campbeln: I didn't specify any constraints on `theta` and I've no reason to believe that the sign of `theta` matters. Have you tried it? – Li-aung Yip May 01 '12 at 01:42
  • I'm working with it now, but I just realized my problem differs a bit... http://stackoverflow.com/questions/10392658/calculate-the-bounding-boxs-x-y-height-and-width-of-a-rotated-element-via-jav I have the X, Y and T (angle of rotation), and I'd like to find BX, BY and the X/Y coords for the top-left corner. The question at the link above is written in JavaScript, but I've referenced the sources for the calcs (including yours here ;) so even if you do not know JS, it should be parse-able for you. Thanks! – Campbeln May 01 '12 at 02:01
  • Hi, when theta is 45, we get really wrong results due to the division by 0 1/(cos(t)^2-sin(t)^2) what can be done for this? – anna.mi Dec 17 '13 at 17:11
  • 1
    @anna.mi for 45º, there is no inverse matrix. See http://www.mathsisfun.com/algebra/matrix-inverse.html - The Inverse May Not Exist. I would assume that you'd have to use the affine transformation method mentioned below. – Marius Aug 19 '15 at 06:15
  • Got wrong results in C# until I converted the degrees to radians. Then everything was fine. – Humppakäräjät Nov 09 '16 at 10:58
  • 1
    To make your answer even more awesome, would you please care to state the formulas the other way round? you see, I need to rotate and crop an image (around a face I detect there) and I have the (x,y) in your terminology, and I need to calculate (bx,by.)... – Motti Shneor Mar 19 '19 at 13:22
0

You'll probably need something like affine transformation to discover point coordinates. And then using standard geometry formulas calculate the size.

Dmitry Reznik
  • 6,812
  • 2
  • 32
  • 27