How to find the center of an after-rotated rectangle in 2d-space?

848 Views Asked by At

I'm trying to write an algorithm that solves the following problem. And although there is a lot of rectangle geometry questions here on math.stackexchange.com, I have not yet found one that answers the following dilemma.

I have the top-left coordinate of a rectangle, along with the height and width of the rectangle. I also have an angle that the rectangle has been rotated by (emphasis on past tense). The point of rotation is the center of the after-rotated rectangle.

Given the above information, I would like to find the center of the after-rotated rectangle.

Right now my approach is to first find the other three coordinates, and then take the average of the x and y components giving the center. The other co-ordinates calculated by first finding which quadrant I am in, and then using simple trigonometry to find new point.

For example, if the angle $0<a\leq 90$, I would find the top right point by first calculating the y component with $(-1 * sin(a)*width)$ and the x component with $(cos(a) * width)$.

Doing it this way I end up with a LOT of if statements, and alot of different calculations, some multiplied by $-1$ depending on which quadrant.

If there is a more elegant way to solve this problem, please help guide me through it.

Update:

I have included a diagram to illustrate.

enter image description here