Changeset 4206 in josm


Ignore:
Timestamp:
2011-07-07T10:06:51+02:00 (13 years ago)
Author:
bastiK
Message:

change heading formula to something sane

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r4126 r4206  
    66import static java.lang.Math.PI;
    77import static java.lang.Math.asin;
     8import static java.lang.Math.atan2;
    89import static java.lang.Math.cos;
    910import static java.lang.Math.sin;
     
    181182     * this lat/lon to another.
    182183     *
     184     * (I don't know the original source of this formula, but see
     185     * http://math.stackexchange.com/questions/720/how-to-calculate-a-heading-on-the-earths-surface
     186     * for some hints how it is derived.)
     187     *
    183188     * @param other the "destination" position
    184      * @return heading
     189     * @return heading in the range 0 <= hd < 2*PI
    185190     */
    186191    public double heading(LatLon other) {
    187         double rv;
    188         if (other.lat() == lat()) {
    189             rv = (other.lon()>lon() ? Math.PI / 2 : Math.PI * 3 / 2);
    190         } else {
    191             rv = Math.atan((other.lon()-lon())/(other.lat()-lat()));
    192             if (rv < 0) {
    193                 rv += Math.PI;
    194             }
    195             if (other.lon() < lon()) {
    196                 rv += Math.PI;
    197             }
    198         }
    199         return rv;
    200     }
     192        double hd = atan2(sin(toRadians(this.lon() - other.lon())) * cos(toRadians(other.lat())),
     193                cos(toRadians(this.lat())) * sin(toRadians(other.lat())) -
     194                sin(toRadians(this.lat())) * cos(toRadians(other.lat())) * cos(toRadians(this.lon() - other.lon())));
     195        hd %= 2 * PI;
     196        if (hd < 0) {
     197            hd += 2 * PI;
     198        }
     199        return hd;
     200     }
    201201
    202202    /**
Note: See TracChangeset for help on using the changeset viewer.