Rosetta Code

Angle difference between two bearings

Compute the smallest signed turn needed to move from one bearing to another.

Intro View source
Source rosettacode/popular/angle_difference_between_two_bearings.vibe
# title: Angle difference between two bearings
# source: https://rosettacode.org/wiki/Angle_difference_between_two_bearings
# category: Rosetta Code
# difficulty: Intro
# summary: Compute the smallest signed turn needed to move from one bearing to another.
# tags: popular, math, geometry, normalization
# vibe: 0.2

def normalize(angle)
  while angle < 0.0
    angle = angle + 360.0
  end

  while angle >= 360.0
    angle = angle - 360.0
  end

  angle
end

def angle_difference(from_bearing, to_bearing)
  difference = normalize(to_bearing) - normalize(from_bearing)

  while difference <= -180.0
    difference = difference + 360.0
  end

  while difference > 180.0
    difference = difference - 360.0
  end

  difference
end

def run
  {
    north_to_east: angle_difference(0.0, 90.0),
    east_to_north: angle_difference(90.0, 0.0),
    wrap_clockwise: angle_difference(350.0, 10.0),
    wrap_counterclockwise: angle_difference(10.0, 350.0),
    same_heading: angle_difference(720.0, 0.0)
  }
end
Output
Press run to execute run from this example.
rosetta-code popular math geometry normalization browser-runner