Affine Transform with Homogeneous Coordinates

~15 mincode completion

Implement apply_affine(points, A).

  • points has shape (N, 2).
  • A has shape (2, 3).
  • Return an array of shape (N, 2).

Examples

Pure translation: origin moves, (1,1) shifts by (2,3)

Input
apply_affine([[1, 1], [0, 0]], [[1, 0, 2], [0, 1, 3]])
Output
[[3, 4], [2, 3]]

90 degree rotation plus translation (1, 2)

Input
apply_affine([[1, 0], [0, 1]], [[0, -1, 1], [1, 0, 2]])
Output
[[1, 3], [0, 2]]

Scale by (2, 3) and translate by (1, -1)

Input
apply_affine([[2, 3]], [[2, 0, 1], [0, 3, -1]])
Output
[[5, 8]]

Hints

Hint 1

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

Hint 2

Watch for this: applied only the 2x2 linear part.

Requirements

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

  • A: (2, 3) affine matrix [linear | translation]

  • Return (N, 2) array of transformed points

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 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 apply_affine(points, A):
    """
    Apply a 2x3 affine map to 2D points using homogeneous coordinates.

    Args:
        points: (N, 2) array of (x, y) rows
        A:      (2, 3) affine matrix [linear | translation]

    Returns:
        (N, 2) array of transformed 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.