Ora

How to Rotate a Vector by Some Angle?

Published in Vector Rotation Mathematics 4 mins read

Rotating a vector by a specific angle involves transforming its components using trigonometric functions or a rotation matrix. This process reorients the vector in a coordinate space without altering its magnitude. The core idea, as highlighted by mathematical principles, is to construct new coordinates for the rotated vector by leveraging sine and cosine functions relative to the desired angle.

2D Vector Rotation

The most common scenario involves rotating a 2D vector around the origin. This can be achieved using direct trigonometric formulas or a rotation matrix.

Using Trigonometric Formulas

To rotate a vector v = (x, y) by an angle θ (theta) counter-clockwise around the origin, the new coordinates (x', y') can be calculated as follows:

  • x' = x ⋅ cos(θ) - y ⋅ sin(θ)
  • y' = x ⋅ sin(θ) + y ⋅ cos(θ)

These formulas essentially define a new position for the vector's tip by projecting its original components onto new axes that are rotated by the angle θ. The sine and cosine functions play a crucial role in constructing this new coordinate space, determining how much of the original x and y components contribute to the new x' and y' positions.

Example:
Let's rotate the vector v = (3, 2) by an angle of 90 degrees (or π/2 radians) counter-clockwise.

  • cos(90°) = 0
  • sin(90°) = 1

Applying the formulas:

  • x' = 3 ⋅ 0 - 2 ⋅ 1 = -2
  • y' = 3 ⋅ 1 + 2 ⋅ 0 = 3

The rotated vector v' is (-2, 3).

Using a Rotation Matrix

A more generalized and often preferred method, especially in computer graphics and linear algebra, is to use a rotation matrix. The 2D rotation matrix for an angle θ (counter-clockwise) is:

$$
R(\theta) = \begin{pmatrix}
\cos(\theta) & -\sin(\theta) \
\sin(\theta) & \cos(\theta)
\end{pmatrix}
$$

To find the new vector v' = (x', y'), you multiply the rotation matrix by the original vector v:

$$
\begin{pmatrix}
x' \
y'
\end{pmatrix} = \begin{pmatrix}
\cos(\theta) & -\sin(\theta) \
\sin(\theta) & \cos(\theta)
\end{pmatrix}
\begin{pmatrix}
x \
y
\end{pmatrix}
$$

This matrix multiplication yields the same trigonometric formulas as above.

Example (using the same vector (3, 2) and angle 90°):

$$
\begin{pmatrix}
x' \
y'
\end{pmatrix} = \begin{pmatrix}
\cos(90^\circ) & -\sin(90^\circ) \
\sin(90^\circ) & \cos(90^\circ)
\end{pmatrix}
\begin{pmatrix}
3 \
2
\end{pmatrix} = \begin{pmatrix}
0 & -1 \
1 & 0
\end{pmatrix}
\begin{pmatrix}
3 \
2
\end{pmatrix} = \begin{pmatrix}
0 \cdot 3 + (-1) \cdot 2 \
1 \cdot 3 + 0 \cdot 2
\end{pmatrix} = \begin{pmatrix}
-2 \
3
\end{pmatrix}
$$

Again, the rotated vector v' is (-2, 3).

Key Considerations for Vector Rotation

When rotating vectors, several factors are important to keep in mind:

  • Angle Direction:
    • Positive Angle (θ): Typically denotes a counter-clockwise rotation.
    • Negative Angle (-θ): Represents a clockwise rotation.
    • Alternatively, for clockwise rotation with a positive angle θ, you can use:
      • x' = x ⋅ cos(θ) + y ⋅ sin(θ)
      • y' = -x ⋅ sin(θ) + y ⋅ cos(θ)
      • Or use a rotation matrix with -θ: $R(-\theta) = \begin{pmatrix} \cos(\theta) & \sin(\theta) \ -\sin(\theta) & \cos(\theta) \end{pmatrix}$
  • Units of Angle: Ensure consistency in angle units. Most mathematical functions (like those in programming languages) expect angles in radians. If working with degrees, convert them to radians before applying the formulas: radians = degrees * (π / 180).
  • Origin of Rotation: The formulas above assume rotation around the origin (0,0). If you need to rotate around a different point (Px, Py), you must:
    1. Translate the vector so that the rotation point becomes the origin (subtract Px, Py from the vector's components).
    2. Perform the rotation.
    3. Translate the vector back (add Px, Py to the rotated components).

3D Vector Rotation

Rotating vectors in 3D space is more complex as it involves specifying an axis of rotation. Common methods include:

  • Rotation Matrices for Specific Axes: You can rotate a 3D vector around the X, Y, or Z axis using dedicated 3x3 rotation matrices.
    • Rotation around X-axis:
      $$
      R_x(\theta) = \begin{pmatrix}
      1 & 0 & 0 \
      0 & \cos(\theta) & -\sin(\theta) \
      0 & \sin(\theta) & \cos(\theta)
      \end{pmatrix}
      $$
    • Rotation around Y-axis:
      $$
      R_y(\theta) = \begin{pmatrix}
      \cos(\theta) & 0 & \sin(\theta) \
      0 & 1 & 0 \
      -\sin(\theta) & 0 & \cos(\theta)
      \end{pmatrix}
      $$
    • Rotation around Z-axis:
      $$
      R_z(\theta) = \begin{pmatrix}
      \cos(\theta) & -\sin(\theta) & 0 \
      \sin(\theta) & \cos(\theta) & 0 \
      0 & 0 & 1
      \end{pmatrix}
      $$
  • General Axis-Angle Rotation: For rotation around an arbitrary axis, more advanced techniques like Rodrigues' Rotation Formula or using Quaternions are employed. Quaternions are particularly efficient for combining multiple rotations and avoiding gimbal lock issues.

Summary of 2D Rotation Formulas

Aspect Formula (Counter-Clockwise Rotation) Notes
Original Vector v = (x, y) Components of the vector to be rotated.
Angle of Rotation θ (theta) Positive θ for counter-clockwise. Convert degrees to radians.
New X-component (x') x' = x ⋅ cos(θ) - y ⋅ sin(θ) Utilizes cosine and sine to project components onto new axes.
New Y-component (y') y' = x ⋅ sin(θ) + y ⋅ cos(θ)
Rotation Matrix $$
    R(\theta) = \begin{pmatrix}
    \cos(\theta) & -\sin(\theta) \\
    \sin(\theta) & \cos(\theta)
    \end{pmatrix}
    $$ | Allows for matrix multiplication: **v'** = R(θ) ⋅ **v**                             |

Understanding these principles allows for precise manipulation of vectors in various applications, from physics simulations to computer graphics.