Problem 148018 · easy · Robotics · Level 01 Poses and Frames

North Is Zero on the Boat

compass heading · radians · conventions · angle wrapping

A survey boat's autopilot and its navigation code disagree about angles.

  • The compass convention, used by the crew: degrees, 0 is north, angles grow clockwise, so 90 is east, 180 south and 270 west. A compass heading is always in [0, 360).
  • The maths convention, used by the code: radians, 0 is east (the world's +x axis; +y is north), angles grow counter-clockwise, so north is \pi/2. A maths heading is always wrapped to (-\pi, \pi]; west is written as +\pi, never -\pi.

Write convert_heading(value, to):

  • to == "math": value is a compass heading in degrees; return the same direction as a maths heading in radians, in (-\pi, \pi].
  • to == "compass": value is a maths heading in radians; return the compass heading in degrees, in [0, 360).

Examples

Input:  value = 135, to = "math"
Output: -0.7853981633974483
Explanation: 135 on the compass is south-east, 45 degrees clockwise of east: -pi/4.

Input:  value = 270, to = "math"
Output: 3.141592653589793
Explanation: due west is pi (not -pi).

Input:  value = 3.141592653589793, to = "compass"
Output: 270.0

Constraints

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

Goals

  • Translate between a compass heading and the maths angle convention
  • Convert between degrees and radians in the right place
  • Wrap the result into the range each convention uses
Starting Python…