Rotate 2D Points

~12 mincode completion

Implement rotate_points(points, theta).

  • points has shape (N, 2).
  • theta is in radians, counterclockwise.
  • Return an array of shape (N, 2).

Examples

theta = 0 leaves the points unchanged

Input
rotate_points([[1, 0], [0, 1]], 0)
Output
[[1, 0], [0, 1]]

90 degrees counterclockwise: (1,0) -> (0,1), (0,1) -> (-1,0)

Input
rotate_points([[1, 0], [0, 1], [1, 1]], 1.5707963267948966)
Output
[[0, 1], [-1, 0], [-1, 1]]

45 degrees sends (1,0) onto the diagonal

Input
rotate_points([[1, 0]], 0.7853981633974483)
Output
[[0.70711, 0.70711]]

Hints

Hint 1

Use a matrix product rather than nested loops, and check which operand transposes.

Hint 2

Reach for counterclockwise rather than clockwise.

Requirements

  • points: (N, 2) array of (x, y) rows

  • theta: rotation angle in radians

  • Return (N, 2) array of rotated points

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 93.

Python
import numpy as np


def rotate_points(points, theta):
    """
    Rotate 2D points counterclockwise by theta radians.

    Args:
        points: (N, 2) array of (x, y) rows
        theta:  rotation angle in radians

    Returns:
        (N, 2) array of rotated points
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.