Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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.

share|improve this question
5  
If you're stuck on it, it's not "trivial" :) –  badatmath Oct 24 '11 at 19:36
    
I was thinking, because it is high-school math it would be seen as trivial :) –  orlp Oct 24 '11 at 19:41
    
Who you calling "You people" - ?!?! $$$$ :) –  The Chaz 2.0 Oct 24 '11 at 20:13
    
Well, you choose: you dudes, you guys, some of you, etc. –  orlp Oct 24 '11 at 20:24
1  
possible duplicate of Rotating a rectangle –  J. M. Oct 25 '11 at 1:29

3 Answers 3

up vote 7 down vote accepted

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

share|improve this answer
    
What did you use to draw that picture? –  orlp Oct 24 '11 at 21:40
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. –  Jiri Oct 24 '11 at 21:48
    
After trying it out it seems to work only for $\alpha < 90$. How would I edit the formulae for $\alpha \geq 90$? –  orlp Oct 24 '11 at 23:18
    
Thank you so much, you helped immensely. +1 and accepted –  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)$

share|improve this answer
    
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. –  orlp Oct 24 '11 at 20:10
    
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}$. –  Thomas Andrews Oct 24 '11 at 20:40
    
I think these formulas only work for $\alpha < \pi/2$. There are similar formulas for other intervals. –  lhf Oct 24 '11 at 21:13
    
Nightcracker- sorry for not including the explanation, I was jut sort-off reading it from your picture... –  Tomislav Petričević Oct 24 '11 at 21:48
    
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)$. –  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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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