mapbox / mapbox-gl-native

Interactive, thoroughly customizable maps in native Android, iOS, macOS, Node.js, and Qt applications, powered by vector tiles and OpenGL
https://mapbox.com/mobile
Other
4.37k stars 1.33k forks source link

NaN return value when finding the distance to the same point #3556

Closed bleege closed 8 years ago

bleege commented 8 years ago

With certain LatLng values (e.g. LatLng(40.71199035644531, -74.0081)) we are getting a NaN return value when finding the distance to the same point.

LatLng ll1 = new LatLng(40.71199035644531, -74.0081);
LatLng ll2 = new LatLng(40.71199035644531, -74.0081);
double distance = ll1.distanceTo(ll2); // distance: NaN

It appears that in this particular case the sum of t1, t2 & t3 equals 1.0000000000000002 instead of 1 which then causes Math.acos() to return NaN since its > 1.

    public double distanceTo(LatLng other) {

        final double a1 = MathConstants.DEG2RAD * this.latitude;
        final double a2 = MathConstants.DEG2RAD * this.longitude;
        final double b1 = MathConstants.DEG2RAD * other.getLatitude();
        final double b2 = MathConstants.DEG2RAD * other.getLongitude();

        final double cosa1 = Math.cos(a1);
        final double cosb1 = Math.cos(b1);

        final double t1 = cosa1 * Math.cos(a2) * cosb1 * Math.cos(b2);
        final double t2 = cosa1 * Math.sin(a2) * cosb1 * Math.sin(b2);
        final double t3 = Math.sin(a1) * Math.sin(b1);
        final double tt = Math.acos(t1 + t2 + t3);

        return GeoConstants.RADIUS_EARTH_METERS * tt;
    }
tobrun commented 8 years ago

I started with writing a testcase for this issue: screen shot 2016-01-18 at 12 30 29

This resulted in following output:

screen shot 2016-01-18 at 12 30 34

tobrun commented 8 years ago

Updated code of distanceTo:

screen shot 2016-01-18 at 12 41 59

tobrun commented 8 years ago

This landed with 78bedff in release branch of 3.1.0 in #3537