Skip to content

Commit

Permalink
Fall back to linear interpolation when interpolating in short distanc…
Browse files Browse the repository at this point in the history
…es (#388).

This also adds a couple of unit tests for interpolating to the beginning
or end of a segment.
  • Loading branch information
Guilherme Gonçalves authored and eggpi committed Jul 7, 2018
1 parent 3b8520e commit d0368a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion library/src/com/google/maps/android/SphericalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ public static LatLng interpolate(LatLng from, LatLng to, double fraction) {
double angle = computeAngleBetween(from, to);
double sinAngle = sin(angle);
if (sinAngle < 1E-6) {
return from;
return new LatLng(
from.latitude + fraction * (to.latitude - from.latitude),
from.longitude + fraction * (to.longitude - from.longitude));
}
double a = sin((1 - fraction) * angle) / sinAngle;
double b = sin(fraction * angle) / sinAngle;
Expand Down
14 changes: 14 additions & 0 deletions library/tests/src/com/google/maps/android/SphericalUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ public void testInterpolate() {
expectLatLngApproxEquals(
down,
SphericalUtil.interpolate(new LatLng(-45, 0), new LatLng(-45, 180), 1 / 2.0));

// boundary values for fraction, between left and back
expectLatLngApproxEquals(
left, SphericalUtil.interpolate(left, back, 0));
expectLatLngApproxEquals(
back, SphericalUtil.interpolate(left, back, 1.0));

// two nearby points, separated by ~4m, for which the Slerp algorithm is not stable and we
// have to fall back to linear interpolation.
expectLatLngApproxEquals(
new LatLng(-37.756872, 175.325252),
SphericalUtil.interpolate(
new LatLng(-37.756891,175.325262),
new LatLng(-37.756853,175.325242), 0.5));
}

public void testComputeLength() {
Expand Down

0 comments on commit d0368a4

Please sign in to comment.