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.

  • 5
    If you're stuck on it, it's not "trivial" :)2011-10-24
  • 0
    I was thinking, because it is high-school math it would be seen as trivial :)2011-10-24
  • 0
    Who you calling "You people" - ?!?! $$$$ :)2011-10-24
  • 1
    possible duplicate of [Rotating a rectangle](http://math.stackexchange.com/questions/49020/rotating-a-rectangle)2011-10-25
  • 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

7

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
    What did you use to draw that picture?2011-10-24
  • 1
    (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.2011-10-24
  • 0
    After trying it out it seems to work only for $\alpha < 90$. How would I edit the formulae for $\alpha \geq 90$?2011-10-24
  • 0
    Thank you so much, you helped immensely. +1 and accepted2011-10-25
2

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
    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.2011-10-24
  • 0
    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}$.2011-10-24
  • 0
    I think these formulas only work for $\alpha < \pi/2$. There are similar formulas for other intervals.2011-10-24
  • 0
    Nightcracker- sorry for not including the explanation, I was jut sort-off reading it from your picture...2011-10-24
  • 0
    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)$.2011-10-24
  • 0
    @Tomislav: my formula seems correct to me.2011-10-24
  • 0
    For a point $F$ $(0,h)$ your formula will give coordinates $(0,h\cdot cos \alpha+w\cdot sin\alpha)$, whereas my corrections lead to correct answer $(0,h\cdot cos \alpha)$ (I chose the specific point, i.e. $F$. which we can visually check from your graphs).2011-10-24
  • 0
    @Tomislav: you are right. I corrected my answer, extended it to other angles and gave you +1.2011-10-25
1

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.