I'm trying to write out the steps in code for deriving the 2D coordinate rotation formula so I can understand it.

x = radius * cos(angle)
y = radius * sin(angle)
x1 = radius * cos(angle + -rotation)
y1 = radius * sin(angle +  rotation)


x1 = radius * cos(angle) * cos(rotation) – radius * sin(angle) * sin(rotation)
y1 = radius * sin(angle) * cos(rotation) + radius * cos(angle) * sin(rotation)


x1 = cos(rotation) * x – sin(rotation) * y
y1 = cos(rotation) * y + sin(rotation) * x

I hate to post a question just asking "Is this right?", but is this right? In particular, I'm unsure if I'm correctly representing the w (as listed in the link) in the first x1 and y1 assignment, and it's expansion. And no, this isn't homework. Thanks.


migrated from stackoverflow.com Sep 3 '11 at 20:24

This question came from our site for professional and enthusiast programmers.

  • $\begingroup$ Thanks. Though that site seems a bit intimidating, especially to ask them to check my plus and minus signs. $\endgroup$ – Marcus Booster Sep 3 '11 at 19:34

Why don't you look at the rotation this way. What is the coordinates of the rotated $\hat{x}=(1,0)$ axis? From trigonometry you will find it as $X=(\cos\theta,\sin\theta)$. Now what is the coordinates of the rotated $\hat{y}=(0,1)$ axis? Similarly from trig you get $Y=(-\sin\theta,\cos\theta)$

A general vector $\vec{v}=(A,B)=A \hat{x}+B \hat{y}$ is rotated by rotating the unit vectors $V = A*(\cos\theta,\sin\theta) + B(-\sin\theta,\cos\theta)$ or re-arranged

$$ V = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \begin{pmatrix} A \\ B \end{pmatrix} $$

So the rotation matrix is $${\rm Rot}(\theta)=\begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix}$$


It's okay, just a minor detail in the initial formulation (the first four lines):

x1 = radius * cos(angle + -rotation)

Should be:

x1 = radius * cos(angle + rotation)

(No minus sign, the angle is added in both cases)

By the way, I can understand your doubts. The link you posted first names the rotation angle w and then f. They should have stuck with w or given it a significant name like you did

  • $\begingroup$ Thanks. But without the minus sign, wouldn't the first expansion for x1 also lack one--or is that just a consequence of finding the x value? $\endgroup$ – Marcus Booster Sep 3 '11 at 18:36
  • $\begingroup$ All other equations are ok, it's as if the minus were just a typo and was not considered in the expansions $\endgroup$ – dario_ramos Sep 3 '11 at 18:38
  • 1
    $\begingroup$ @Marcus: The minus sign in your first equation for $x1$ comes from the property of the cosine. The cosine of the sum of two angles is given by the formula $$\cos(\alpha+\beta)=\cos\alpha\cos\beta-\sin\alpha\sin\beta.$$ For sine the corresponding formula reads $$\sin(\alpha+\beta)=\sin\alpha\cos\beta+\cos\alpha\sin\beta.$$ This explains the difference in sign. $\endgroup$ – Jyrki Lahtonen Sep 3 '11 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy