2D Vector rotation
Assume a so-called (2D) 'vector' —for the purpose of this page/exercise, consider a vector just
to be an arrow— that has its 'origin' at coordinate (0, 0) and has its pointy end at coordinate (x, y).
We want to rotate the arrow around the origin; i.e., the origin remains where it is,
through an angle θ. We can liken this to moving the pointy end of the vector along an arc of
a circle with a radius that is equal to the length of the vector over an angle θ.
Since the origin and length of the vector do not change, 'all' we need to do is to find the new coordinates
(x', y') of the pointy end of the vector. For that we can use the following two rotation formulas:
- x' = x (cos(θ)) - y (sin(θ))
- y' = x (sin(θ)) + y (cos(θ))
!!Note!! These formulas describe counterclockwise rotation. To rotate clockwise just
use -θ or 360 - θ.
For example, if the end of our vector is at (x = 8, y = 6) (see plot below) and we want to rotate
90° counterclockwise:
- x' = 8 (cos(90°)) - 6 (sin(90°)) = 8 (0) - 6 (1) = -6
- y' = 8 (sin(90°)) + 6 (cos(90°)) = 8 (1) + 6 (0) = 8
Play with this below.
|