2D Rotation Matrix
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 θ (counterclockwise) can be represented using trigonometric functions. To rotate a point, we can follow these steps:
- Convert the given angle to a suitable format for calculation.
- Create the rotation matrix using the angle.
- Apply the rotation matrix to the point.
This technique is widely used in image processing.
Example:
rotate_point([1, 0], 90)
[0.0, 1.0]
Rotating (1, 0) by 90° counterclockwise gives (0, 1)
Constraints:
- Angle is given in degrees
- Point coordinates are floating-point numbers
- Return the rotated point [x', y'] rounded to 4 decimal places
2D Rotation Matrix: Background & Strategy
Background Knowledge
A 2D rotation matrix is a fundamental transformation in linear algebra that rotates points in a 2D plane around the origin. The key insight is that rotation is a linear transformation—it can be represented as matrix multiplication. When you multiply a rotation matrix by a position vector, you get a new vector that represents the rotated point.
The rotation matrix works because of trigonometric relationships. For a counterclockwise rotation by angle θ, the new coordinates depend on both the original position and the angle: the x-component of the rotated point involves both cosθ and sinθ applied to the original coordinates, and similarly for the y-component. This elegant structure preserves distances and angles, making it an orthogonal transformation—a property that ensures rotations don't distort shapes.
Understanding this concept is crucial because rotation matrices extend to 3D graphics, computer vision, robotics, and physics simulations. The 2D case is the foundation for more complex transformations. Additionally, rotation matrices have special properties: their inverse equals their transpose, and their determinant is always 1, which you can verify mathematically.
Algorithm/Approach
The solution follows a straightforward two-phase approach:
- Matrix Construction: Build the rotation matrix using the given angle θ, computing cosθ and sinθ values
- Matrix-Vector Multiplication: Apply the rotation matrix to the point by performing standard matrix multiplication
This is a direct application of linear algebra: treat the point as a column vector and multiply it by the rotation matrix from the left.
Step-by-Step Strategy
-
Parse the Input: Extract the angle θ (typically in radians) and the point coordinates (x,y)
-
Compute Trigonometric Values: Calculate cosθ and sinθ once (don't recompute them multiple times)
-
Construct the Rotation Matrix: Populate a 2×2 matrix with the formula:
- Top-left: cosθ
- Top-right: −sinθ
- Bottom-left: sinθ
- Bottom-right: cosθ
- Perform Matrix-Vector Multiplication: Multiply the rotation matrix by the point vector:
- x′=cos\theta⋅x−sin\theta⋅y
- y′=sin\theta⋅x+cos\theta⋅y
- Return the Result: Return the rotated point (x′,y′)
Common Pitfalls
-
Angle Units: Verify whether the angle is in radians or degrees. Most programming languages' trigonometric functions expect radians. If given degrees, convert using: radians=\text{degrees} \times \frac{\pi}{180}
-
Sign Errors: The top-right element is −sinθ (negative), not +sinθ. This is easy to mix up.
-
Matrix Multiplication Order: The rotation matrix multiplies the point vector from the left: R(\theta)⋅p, not p⋅R(\theta)
-
Floating-Point Precision: Trigonometric functions return approximate values. For angles like 90°, cos(90°) might be 6.123×10−17 instead of exactly 0. Consider rounding very small values to zero if needed.
-
Coordinate System Assumptions: Ensure you understand whether your coordinate system has y-axis pointing up (standard math) or down (some graphics systems). The formula assumes standard mathematical coordinates.
Time & Space Complexity
-
Time Complexity: O(1) — The solution involves a fixed number of trigonometric function calls and arithmetic operations, regardless of input size
-
Space Complexity: O(1) — You only need to store the rotation matrix (4 values) and the resulting point (2 values), both constant space