import math


def compute_heading_angle(source_coordinates,
                          target_coordinates,
                          radians=True,
                          apply_r2r_correction=True):
  """Computes heading angle in the xy plane given two xyz coordinates.

  Args:
    source_coordinates: Source triplet with xyz coordinates.
    target_coordinates: Target triplet with xyz coordinates.
    radians: Whether to output in radians or degrees.
    apply_r2r_correction: Whether to use the R2R dataset heading definition.
        If true, heading is computed with respect to the y-axis, increasing
        clockwise. See
        https://github.com/peteanderson80/Matterport3DSimulator/blob/master/README.md#simulator-api.

  Returns:
    Heading angle in the xy plane. Angle is computed with respect to the y-axis,
    increasing clockwise, unless apply_r2r_correction is false.
  """
  source_x, source_y, _ = source_coordinates
  target_x, target_y, _ = target_coordinates
  angle = math.atan2(target_y - source_y, target_x - source_x)

  if apply_r2r_correction:
    angle = 0.5 * math.pi - angle

  if radians:
    return angle % (2 * math.pi)
  return math.degrees(angle) % 360