angle function

double angle(
  1. List<double> a,
  2. List<double> b
)

Get the angle between two 2D vectors @param {ReadonlyVec2} a The first operand @param {ReadonlyVec2} b The second operand @returns {Number} The angle in radians

Implementation

double angle(List<double> a, List<double> b) {
  final x1 = a[0],
      y1 = a[1],
      x2 = b[0],
      y2 = b[1],
      // mag is the product of the magnitudes of a and b
      mag = math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2));
  // mag &&.. short circuits if mag == 0
  final cosine = mag == 0 ? 0 : (x1 * x2 + y1 * y2) / mag;
  // math.min(math.max(cosine, -1), 1) clamps the cosine between -1 and 1
  return math.acos(math.min(math.max(cosine, -1), 1));
}