interpolate static method

Point<double> interpolate(
  1. Point<double> from,
  2. Point<double> to,
  3. double fraction
)

Returns the point which lies the given fraction of the way between from and to.

Implementation

static Point<double> interpolate(
    Point<double> from, Point<double> to, double fraction) {
  // http://en.wikipedia.org/wiki/Slerp
  final fromLat = toRadians(from.x);
  final fromLng = toRadians(from.y);
  final toLat = toRadians(to.x);
  final toLng = toRadians(to.y);
  final cosFromLat = cos(fromLat);
  final cosToLat = cos(toLat);

  // Computes Spherical interpolation coefficients.
  final angle = computeAngleBetween(from, to);
  final sinAngle = sin(angle);
  if (sinAngle < 1E-6) {
    return Point(from.x + fraction * (to.x - from.x),
        from.y + fraction * (to.y - from.y));
  }

  final a = sin((1 - fraction) * angle) / sinAngle;
  final b = sin(fraction * angle) / sinAngle;

  // Converts from polar to vector and interpolate.
  final x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng);
  final y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng);
  final z = a * sin(fromLat) + b * sin(toLat);

  // Converts interpolated vector back to polar.
  final lat = atan2(z, sqrt(x * x + y * y));
  final lng = atan2(y, x);
  return Point(toDegrees(lat), toDegrees(lng));
}