8
$\begingroup$

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:

enter image description here

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).

enter image description here

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.

  • 0
    @J.M.: Not a duplicate. I already __know__ the sides, I need to know the new coordinates of a point within the smaller square.2011-10-25

3 Answers 3

8

I am refering to the picture below, where the rotation angle is denoted by $\alpha$.

enter image description here

We have for the new $x', y'$ if $0 \le \alpha \le 90$:: $x' = BD = BC + CD = FE \cos(90-\alpha) + CD = (h - y) \cos(90-\alpha) + x \cos \alpha$ $y' = DK = DG + GK = DG + ML \cos (90 - \alpha) = y \cos \alpha + x \cos (90 - \alpha)$

Using $\cos(90 - \alpha) = \sin \alpha$ we get $x' = BD = (h - y) \sin \alpha + x \cos \alpha$ $y' = DK = y \cos \alpha + x \sin \alpha$

ADDED. Let us denote this computation as $(x', y') = F(x, y, h, \alpha), 0 \le \alpha \le 90$ The meaning and the order of the parameters is relevant.

For other angles use the picture below enter image description here

CORRECTION: The legend in the figures should be respectively: $\beta=\alpha-90,\beta=\alpha-180,\beta=\alpha-270$.

to deduce: $(x', y') = F(h-y, x, w, \alpha-90), 90 \le \alpha \le 180$ $(x', y') = F(w-x, h-y, h, \alpha-180), 180 \le \alpha \le 270$ $(x', y') = F(y, w-x, w, \alpha-270), 270 \le \alpha \le 360$

  • 0
    Thank you so much, you helped immensely. +1 and accepted2011-10-25
3

If I'm not mistaken the formulas should be:

$x_{new}=(h-y)\cdot sin(A)+x\cdot cos(A)$

$y_{new}=x\cdot sin(A)+y\cdot cos(A)$

  • 0
    @Tomislav: you are right. I corrected my answer, extended it to other angles and gave you +1.2011-10-25
2

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.