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.

  • 5
    $\begingroup$ If you're stuck on it, it's not "trivial" :) $\endgroup$ – badatmath Oct 24 '11 at 19:36
  • $\begingroup$ I was thinking, because it is high-school math it would be seen as trivial :) $\endgroup$ – orlp Oct 24 '11 at 19:41
  • $\begingroup$ Who you calling "You people" - ?!?! $$$$ :) $\endgroup$ – The Chaz 2.0 Oct 24 '11 at 20:13
  • 1
    $\begingroup$ possible duplicate of Rotating a rectangle $\endgroup$ – J. M. is not a mathematician Oct 25 '11 at 1:29
  • $\begingroup$ @J.M.: Not a duplicate. I already know the sides, I need to know the new coordinates of a point within the smaller square. $\endgroup$ – orlp Oct 25 '11 at 10:52

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$$

  • $\begingroup$ What did you use to draw that picture? $\endgroup$ – orlp Oct 24 '11 at 21:40
  • 1
    $\begingroup$ (1) MS PowerPoint. (2) Print Screen. (3) Paste to Paint. (4) Select the relevant area and "Edit/Copy to.." 24-bit BMP. (5) Open BMP in Paint and Save As PNG. $\endgroup$ – Jiri Kriz Oct 24 '11 at 21:48
  • $\begingroup$ After trying it out it seems to work only for $\alpha < 90$. How would I edit the formulae for $\alpha \geq 90$? $\endgroup$ – orlp Oct 24 '11 at 23:18
  • $\begingroup$ Thank you so much, you helped immensely. +1 and accepted $\endgroup$ – orlp Oct 25 '11 at 11:17

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

  • $\begingroup$ A few lines explaining how you got these formula's would help me immensely next time, it would be great if you could add those. $\endgroup$ – orlp Oct 24 '11 at 20:10
  • $\begingroup$ Complex multiplication. Let $P=0$ and then rotating clockwise by angle $A$ is the same as multiplication by $\cos A - i \sin A$. Apply this to your top left corner, $-x+yi$, and take the imaginary component to get $y_{new}$. Apply this to yor lower left corner, $-x-(h-y)i$, and take the real part to get $-x_{new}$. $\endgroup$ – Thomas Andrews Oct 24 '11 at 20:40
  • $\begingroup$ I think these formulas only work for $\alpha < \pi/2$. There are similar formulas for other intervals. $\endgroup$ – lhf Oct 24 '11 at 21:13
  • $\begingroup$ Nightcracker- sorry for not including the explanation, I was jut sort-off reading it from your picture... $\endgroup$ – Tomislav Petričević Oct 24 '11 at 21:48
  • $\begingroup$ Jiri- I think you made a slight error when calculating GK component in the last expression- $GK=x\cdot sin(\alpha)$ and hence $y´=y\cdot cos(\alpha)+x\cdot sin(\alpha)$. $\endgroup$ – Tomislav Petričević Oct 24 '11 at 21:51

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.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.