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 programming a 2d game, and I have noticed I am having trouble getting the position (X,Y) of a rectangle's corner, when such rectangle is rotated. The position I am seeking is absolute in the 2d space.

enter image description here

As you can see, I need help finding the correct formula to find the bottom-right corner of my rectangle when it is rotated. Given the rectangle's dimensions are 100x50, and it has been rotated by degrees.

Also, the Y position in this language is increased the lower the point is. Basically, the top of the screen is Y=0 and the bottom is Y=600

Thank you.

share|improve this question
    
Rotating with respect to what? Where is the origin of your coordinate system? –  J. M. May 7 '11 at 0:31
    
The origin? Do you mean, the top-left of the rectangle? Sorry I forgot to mention that. –  Zol Tun Kul May 7 '11 at 0:35
    
The origin: you're rotating with respect to some point. What's that point? Note that if your (horizontal) rectangle were laying on the horizontal axis and the lower left corner is $(0,0)$, the coordinate of your lower right corner is $(100,0)$. –  J. M. May 7 '11 at 0:37
    
(0,50) is the origin. Sorry, my mistake twice :( –  Zol Tun Kul May 7 '11 at 0:41

1 Answer 1

up vote 4 down vote accepted

The idea is to first translate your coordinates to a new coordinate system such that the origin is at $(0,0)$ (subtract $(0,50)$ from all your original coordinates), perform the rotation, and then translate back.

So, in this new coordinate system, the bottom of your rectangle has endpoints at $(0,0)$ and $(100,0)$. You can then use a(n anticlockwise) rotation matrix to transform the point $(100,0)$ to its rotated image:

$$\begin{align*}100\cos45^\circ-0\sin45^\circ&=50\sqrt{2}\\100\sin45^\circ+0\cos45^\circ&=50\sqrt{2}\end{align*}$$

and then undo the translation you made before rotating, which yields the point $(50\sqrt{2},50+50\sqrt{2})$ as the coordinates for the upper left corner in its new position.

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.