# Calculating Bearing

Theory

Bearing is the direction in which an object is pointing or traveling. This is also commonly known as a heading. In Virtual Earth polygons and polylines consist of an array of coordinates that represent endpoints of line segments. It’s useful to think of these line segments as vectors. Each vector has a length and a bearing. Taking this into consideration will allow us to come up with some very useful algorithms.

Virtual Earth uses bearings in the 3D map mode in the form of the heading which is the direction in which the camera is pointing. This heading uses 0 degrees as true North, 90 degrees as East, 180 degrees as South, and 270 degrees as West. These same bearings can be used in 2D map mode when representing the line segments of a polygon or polyline as vectors. This calculation requires us to use complex variable calculations to calculate the arctangent of a point in space. This calculation is often done using the atan2 function. The reason for this is that it can handle situations when the denominator is equal to 0. This complex function has the following complex argument:

For

For y = 0

Where φ is an angle in the range such that:

The sgn function, known as the sign function and signum function, is another complex function that extracts the sign of a real number. This function has the following characteristics:

The following formula can be used to calculate the bearing between two coordinates. Note that this formula takes in latitude and longitudes as radian values and returns the bearing as a radian value in the following range:

We can convert this bearing to degrees in the range [0,360] by using the following formula:

The reason for adding 360 to the previous formula, then calculate the remainder when divided by 360, is to ensure that the bearing is a positive angle between 0 and 360.

Application

The function we are going to create will take in two a VELatLong objects and return the bearing in degrees. We will have to take the latitudes and longitudes of each VELatLong and convert them to their radian value. This will be done using the DegtoRad function we created earlier. We will then calculate the two points we need to put into the arctangent function. We will then calculate the bearing in degrees.

```function calculateBearing(latlong1,latlong2){
var lon1 = latlong1.Longitude;
var lon2 = latlong2.Longitude;
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 = (RadtoDeg(Math.atan2(y, x))+360)%360;
return brng;
}```

## 3 thoughts on “Calculating Bearing”

1. Unknown says:

When I take a start lat long of 26.67,39.150 and an end lat/long 49.01,2.55 I get a bearing of about 37.65. My understanding is that this represents the angle between the two but that would mean that the two points have a north easterly correlation when they actually have a north westerly relationship. My ignorance may be showing but have to ask for enlightenment. Thanks.

2. Ricky says:

I\’ve ran your coordinates through these formulas and recieved a value of 318.249…. degrees which would indicate a North Westerly direction which represents the path from 26.67,39.150 to 49.01,2.55

3. Unknown says:

Richard, thank you. You are absolutely correct. I am using the VEToolkit which credits you with the formula. It appears that the VE Toolkit forumla is at minimum doing something "different". I will follow up with the VEToolkt author. Best regards.