Determine the direction of the compass from one lat / bosom to another

Does anyone have an algorithm to determine the direction from one lat / bosom to another (pseudo-code):

CalculateHeading( lat1, lon1, lat2, long2 ) returns string heading 

Where is the title, for example. NW, SW, E, etc.

Basically, I have two points on the map, and I want to get a general idea of ​​the direction, given that 50 miles to the east and one mile of the North is just the East, not the northeast.

0
source share
3 answers

This site has a basic algorithm:

 // in javascript, not hard to translate... var y = Math.sin(dLon) * Math.cos(lat2); var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon); var brng = Math.atan2(y, x).toDeg(); 

UPDATED: see full math to javascript matching algorithm

This will give you a number from 0 to 360, and this is just a simple search question:

 var bearings = ["NE", "E", "SE", "S", "SW", "W", "NW", "N"]; var index = brng - 22.5; if (index < 0) index += 360; index = parseInt(index / 45); return(bearings[index]); 

It is important to note that your load changes when you move on the ground. The algorithm above shows the original bearing, but if you travel long distances, your bearing will differ significantly when you reach your destination (if you travel a short distance [<several hundred kilometers], then this probably will not change enough to be a problem).

+15
source

Do you remember your trigger functions? That is, SOHCAHTOA:

  • SOH : Sin (θ) = opposite over hypotenuse
  • CAH : Cos (θ) = adjacent over hypotenuse
  • TOA : Tan (θ) = opposite each other

In pseudo code:

 function getDir(lat1, long1, lat2, long2) { margin = π/90; // 2 degree tolerance for cardinal directions o = lat1 - lat2; a = long1 - long2; angle = atan2(o,a); if (angle > -margin && angle < margin): return "E"; elseif (angle > π/2 - margin && angle < π/2 + margin): return "N"; elseif (angle > π - margin && angle < -π + margin): return "W"; elseif (angle > -π/2 - margin && angle < -π/2 + margin): return "S"; } if (angle > 0 && angle < π/2) { return "NE"; } elseif (angle > π/2 && angle < π) { return "NW"; } elseif (angle > -π/2 && angle < 0) { return "SE"; } else { return "SW"; } } 

Edit 1: As Pete and Dean pointed out, this does not take into account the curvature of the Earth. For more accurate calculations for points from the equator, you need to use the formulas of the spherical triangle , which are used in Dean's answer.

Edit 2: Another correction; as Pete noted, arctan() does not give the correct angles, since -1 / -1 and 1/1 are the same (equal to -1/1 and 1 / -1). arctan2(y, x) is a variant with two arguments arctan() , which is designed to compensate for this. arctan() has a range (-π, π], positive for y >= 0 and negative for y < 0 .

+2
source

Convert to a numeric angle and use the result to search for text. For example, -22.5 .. + 22.5 = N. + 22.5..67.5 = NE, 67.5..112.5 = E, etc. Of course, it is assumed that you use only N, NE, E, SE, S, SW, W, NW - if you decide (for example) to go with the old “32 compass points”, each text line obviously represents a smaller range.

0
source

Source: https://habr.com/ru/post/922109/


All Articles