interpolate static method

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

Returns the Point which lies the given fraction of the way between the origin Point and the destination Point.

from The Point from which to start.

to The Point toward which to travel.

fraction A fraction of the distance to travel.

return The interpolated Point.

Implementation

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

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

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

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

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