v9.1.1
Menu

2D Rotation Matrix

mediumFREEMatrices & TransformationsLinear Algebra, GeometryCV: Mathematical Foundations

Implement a 2D rotation matrix for a given angle and apply it to rotate a point. This task involves understanding linear transformations and matrix multiplication to achieve the desired rotation.

The concept of rotation in 2D space is crucial in computer vision and graphics, where it is used to change the orientation of objects. A 2D rotation by angle θ\theta (counterclockwise) can be represented using trigonometric functions. To rotate a point, we can follow these steps:

  1. Convert the given angle to a suitable format for calculation.
  2. Create the rotation matrix using the angle.
  3. Apply the rotation matrix to the point.
(x′y′)=(cos⁡θ−sin⁡θsin⁡θcos⁡θ)(xy)\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 technique is widely used in image processing.

Example

Input
rotate_point([1, 0], 90)
Output
[0.0, 1.0]
Reasoning
  • Input: point is the vector (x,y)=(1,0)(x, y) = (1, 0), one unit along the positive xx-axis; angle_degrees is 9090, a quarter turn counterclockwise.
  • The trigonometric functions in the matrix work in radians, so the angle first becomes θ=90⋅π180=π2\theta = 90 \cdot \frac{\pi}{180} = \frac{\pi}{2}. That gives cos⁡θ=0\cos\theta = 0 and sin⁡θ=1\sin\theta = 1.
  • Math: plug into the matrix: x′=cos⁡θ⋅x−sin⁡θ⋅y=0⋅1−1⋅0=0x' = \cos\theta \cdot x - \sin\theta \cdot y = 0 \cdot 1 - 1 \cdot 0 = 0 and y′=sin⁡θ⋅x+cos⁡θ⋅y=1⋅1+0⋅0=1y' = \sin\theta \cdot x + \cos\theta \cdot y = 1 \cdot 1 + 0 \cdot 0 = 1.
  • Output: [0.0, 1.0], the point now sits one unit up the yy-axis. Its length is still 11: rotations never stretch vectors.
  • Notice that in floating point the cosine of π/2\pi/2 comes out as roughly 6×10−176 \times 10^{-17}, not exactly 00. Rounding to 4 decimal places turns that residue into a clean 0.0.
  • For (1,1)(1, 1) at 45∘45^\circ the same formula gives x′=0x' = 0 and y′=2≈1.4142y' = \sqrt{2} \approx 1.4142: the diagonal point swings onto the yy-axis.

Constraints

  • Angle is given in degrees
  • Point coordinates are floating-point numbers
  • Return the rotated point [x', y'] rounded to 4 decimal places

Learn this in

More from CV: Mathematical Foundations

solution.py

Test results

Sign in to run your code against the test cases.