Rectangle's dimensions, given only the top left and bottom right coordinates, and rotation angle

StackOverflow https://stackoverflow.com/questions/22797512

  •  25-06-2023
  •  | 
  •  

Frage

I have a rectangle that has had a rotation applied. I know the rotation angle, and the top left and bottom right coordinates.

My question is, how do I calculate the dimensions of the rectangle, given these three pieces of information?

War es hilfreich?

Lösung

If your rotation is in terms of a center point on the rectangle, first you have to get the TL and BR relative to that origin.

Let A,B,C,D be the four vertices TL,TR,BR,BL.

You are trying to find TR and BL which would be B and D.

B_x = C_x*Cos(a) - A_y*Sin(a)
B_y = C_x*Sin(a) + A_y*Cos(a)

Likewise

D_x = A_x*Cos(a) - C_y*Sin(a)
D_y = A_x*Sin(a) + C_y*Cos(a)

Think about what they would be before the rotation. You would just use components of A and C to define B and D. The above just comes from multiplying that by the rotation matrix. Where a is the angle.

If the rotation has already been applied to A and B then get the original points before the rotation which is simply the transpose of the rotation matrix:

A'_x = A_x*Cos(a) + A_y*Sin(a)
A'_y = -A_x*Sin(a) + A_y*Cos(a)

Likewise for C

Then use A' and C' in the first set of equations to find the resulting points with rotation.

If you are just interested in length and width, A' and C' will suffice:

width = C'_x - A'_x
Height = A'_y - C'_y
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top