Cast a Lidar Fan

~20 mincode completion

Implement lidar_scan(origin, angles, obstacles, max_range). origin is [x, y], angles are ray headings in radians (direction ), and obstacles is an array of rows . Return one distance per angle.

Examples

Straight at a circle: the reading is the near surface, 4 not 5

Input
lidar_scan([0, 0], [0], [[5, 0, 1]], 10)
Output
[4]

Nothing that way: the ray reads max_range

Input
lidar_scan([0, 0], [1.5707963267948966], [[5, 0, 1]], 10)
Output
[10]

A four-ray fan around one obstacle

Input
lidar_scan([0, 0], [0, 1.5707963267948966, 3.141592653589793, 4.71238898038469], [[5, 0, 1]], 10)
Output
[4, 10, 10, 10]

Hints

Hint 1

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

Hint 2

A common slip here: returned the distance to the circle centre instead of to its surface.

Requirements

  • origin: [x, y] start of every ray

  • angles: (K,) ray headings in radians

  • obstacles: (N, 3) rows [cx, cy, radius]

  • max_range: reading returned when a ray hits nothing

  • Return (K,) distances, each in (0, max_range]

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Python
import numpy as np


def lidar_scan(origin, angles, obstacles, max_range):
    """
    Distance to the nearest circular obstacle along each ray.

    Args:
        origin:    [x, y] start of every ray
        angles:    (K,) ray headings in radians
        obstacles: (N, 3) rows [cx, cy, radius]
        max_range: reading returned when a ray hits nothing

    Returns:
        (K,) distances, each in (0, max_range]
    """
    # 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.