interpolate static method

Point<num> interpolate(
  1. Point<num> from,
  2. Point<num> to,
  3. num 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, num fraction) {
  // http://en.wikipedia.org/wiki/Slerp
  num fromLat = toRadians(from.x);
  num fromLng = toRadians(from.y);
  num toLat = toRadians(to.x);
  num toLng = toRadians(to.y);
  num cosFromLat = cos(fromLat);
  num cosToLat = cos(toLat);

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

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

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

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