The Vacuum's Laser in a Bare Room

range sensor · ray · max range · trigonometry

A robot vacuum works in an empty rectangular room. The room's corners are (0, 0) and (width, height) in metres (x to the right, y up). The robot's pose is (x, y, theta): its position, strictly inside the room, and its heading theta in radians, counter-clockwise from the +x axis.

A laser range finder on the robot points straight ahead. Its beam starts at the robot's position and travels along the heading, in the direction (\cos\theta, \sin\theta), until it meets one of the four walls (the lines x = 0, x = \text{width}, y = 0 and y = \text{height}). The sensor reports the distance travelled. It cannot see further than max_range metres: if the wall is further away, it reports max_range itself.

Write room_range(width, height, pose, max_range) that returns the reported distance as a float.

Examples

Input:  width = 4, height = 3, pose = (1, 1, 0), max_range = 10
Output: 3.0
Explanation: facing +x, the beam meets the wall x = 4 after 4 - 1 = 3 m.

Input:  width = 4, height = 3, pose = (1, 1, math.pi / 4), max_range = 10
Output: 2.8284271247461903
Explanation: diagonally up and to the right. The top wall is 2 m higher, reached after
2 / sin(pi/4) = 2.828 m; the right wall would take 3 / cos(pi/4) = 4.243 m.

Input:  width = 4, height = 3, pose = (2, 1.5, math.pi), max_range = 1.5
Output: 1.5
Explanation: the left wall is 2 m away, beyond the sensor's 1.5 m.

Constraints

  • answers are compared with a tolerance of 1e-6; do not round

Goals

  • Find how far a beam travels from a pose before it meets a wall
  • Treat each wall separately and keep the nearest one the beam reaches
  • Report the sensor's maximum range when the wall is out of reach
Starting Python…