TITLE: "Trivial" trigonometry problem
QUESTION [8 upvotes]: I am really stuck on this problem, and I figured you people could probably help me with it. I have a rectangle with width w and height h with a certain point p inside it. This point is given by the offsets x and y from the topleft of the square. In a picture:

Now, I rotate this rectangle by angle a. I draw the surrounding box around it. I want to know the new (x, y) coordinates of point p from the topleft of the surrounding box (or, the lenghts of the purple lines).

Notes: this is not homework, this is a programming problem I'm stuck on. In the images the angle a < 90, this is not necessarily the case. Also, the lengths of the surrounding box are already known, they need not be calculated.

REPLY [2 votes]: Choose the point $p$ as origin of an $(x,y)$-coordinate system, the $x$-axis pointing to the left. Now rotate the rectangle around $p$ clockwise by an angle $\phi$. What you want to know is the $x$-coordinate of the leftmost vertex and the $y$-coordinate of the topmost vertex as a function of $\phi$.
Let $A=(x,y)$, $B=(x,h-y)$, $C=(w-x,h-y)$, $D=(w-x,y)$ be the four vertices in the starting position. Then for $0<\phi<{\pi\over2}$ the leftmost vertex is $B$ and the topmost vertex is $A$. For ${\pi\over2}<\phi<\pi$ the leftmost vertex is $C$ and the topmost vertex is $B$, and so on.
Now use the formulae for the rotation of points in a coordinate system, taking care of the chosen orientations of axes and angles. The procedure will be more or less mechanic.