interpolate method
Returns the Coordinate at a certain fraction of the distance between the Coordinate and different Coordinate, to
.
Uses the Haversine formula to calculate the new coordinate.
The fraction
is a number between 0 and 1.
If the fraction
is 0, the Coordinate will be the same as the Coordinate.
If the fraction
is 1, the Coordinate will be the same as the to
.
Example:
Coordinate(1, 2).interpolate(Coordinate(3, 4), 0.5); // Coordinate(2, 3)
Implementation
Coordinate interpolate(Coordinate to, double fraction) {
final lat1 = latitude * (pi / 180);
final lon1 = longitude * (pi / 180);
final lat2 = to.latitude * (pi / 180);
final lon2 = to.longitude * (pi / 180);
final dLon = lon2 - lon1;
final bx = cos(lat2) * cos(dLon);
final by = cos(lat2) * sin(dLon);
final lat3 = atan2(sin(lat1) + sin(lat2),
sqrt((cos(lat1) + bx) * (cos(lat1) + bx) + by * by));
final lon3 = lon1 + atan2(by, cos(lat1) + bx);
return Coordinate(lat3 * (180 / pi), lon3 * (180 / pi));
}