lukas121213
lukas121213

Reputation: 11

Correct way of computing angle between geopoints

I have a problem with calculation angle between lines created by connecting three geopoints.

p1
    \    p2
     \  /
     p3

I implemented many solutions but not gave me expected result. For example I used law of cosinuses Direct way of computing clockwise angle between 2 vectors . I also used equation

angle = atan2(vector2.y, vector2.x) - atan2(vector1.y, vector1.x).

Each method returned the same incorect result.

For example I have three points:

p3 latitude=52.66346360584388, 19.056108732273625

p1 latitude=52.66321959338828, 19.056379848488714

p2 latitude=52.66348185383115, 19.05648061759354

Law of cosinuses and atan2 returned angle 44.797 degrees. When I marker this points in google maps and measure the angle in gimp program I have about 57 degrees. In other sets of points the differences are the same or greater. What am I doing wrong?

gimp angle

Upvotes: 1

Views: 97

Answers (1)

MBo
MBo

Reputation: 80287

For spherical geometry you need to use special formulas from here

Look at bearing section, calculate bearings from p3 to p2 and from p3 to p1 and find their difference

Formula:    
  θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ )
where   φ1,λ1 is the start point, 
   φ2,λ2 the end point (Δλ is the difference in longitude)

JavaScript:     (all angles     in radians)

var y = Math.sin(λ2-λ1) * Math.cos(φ2);
var x = Math.cos(φ1)*Math.sin(φ2) -
        Math.sin(φ1)*Math.cos(φ2)*Math.cos(λ2-λ1);
var brng = Math.atan2(y, x).toDegrees();

Upvotes: 1

Related Questions