I am writing a GeoInfosystems library for the FEZ, but I think I have run into an issue.

The first method I wanted to include was to calculate the bearing between two sets of coordinates, but the calculations are coming up wrong.

For examples:

```
Location pointA = new Location(40.7486, -73.9864, 0);
Location pointB = new Location(40.9486, -72.9866, 0);
Debug.Print(GPSTools.BearingToLocation(pointA, pointB).ToString());
```

returns 47.5. The correct heading for those coordinates should be 74 degrees.

Here is my code:

```
/*
* GPS Tools
* Coded by Chris Seto 2010
*
* This sofware is released under the Apache 2.0 license, copyright Chris Seto 2010
* */
using System;
using ElzeKool;
using ChrisSeto.GeoInfoSystems.System;
namespace ChrisSeto.GeoInfoSystems
{
/// <summary>
/// Methods for working with waypoints
/// </summary>
public static class GPSTools
{
/// <summary>
/// Units of distance
/// </summary>
public enum Distance
{
Miles,
Feet,
Kilometers,
Meters,
}
/// <summary>
/// Calculate the distance between two Locations
/// </summary>
/// <param name="pointA"></param>
/// <param name="pointB"></param>
/// <param name="distanceUnits"></param>
/// <returns></returns>
public static double DistanceBetweenLocations(Location pointA, Location pointB, Distance distanceUnits)
{
return 0;
}
/// <summary>
/// Calculate the inital bearing between two Locations
/// </summary>
/// <param name="pointA"></param>
/// <param name="pointB"></param>
/// <param name="headingType"></param>
/// <returns></returns>
public static double BearingToLocation(Location pointA, Location pointB)
{
// Convert both locations from degrees to radians
pointA = LocationToRad(pointA);
pointB = LocationToRad(pointB);
double partOne = exMath.Sin(pointB.Longitude - pointA.Longitude) * exMath.Cos(pointB.Latitude);
double partTwo = exMath.Cos(pointA.Latitude) * exMath.Sin(pointB.Latitude) - exMath.Sin(pointA.Latitude) * exMath.Cos(pointB.Latitude) * exMath.Cos(pointB.Longitude - pointA.Longitude);
double heading = AdditionalMath.ToDeg(exMath.Atan2(partOne, partTwo) % 2 * exMath.PI);
// Solve for compass wrap around
if (heading < 0)
heading += 360;
return heading;
}
/// <summary>
/// Return a new location in radians
/// </summary>
/// <param name="pointA"></param>
/// <returns></returns>
public static Location LocationToRad(Location pointA)
{
return new Location(AdditionalMath.ToRad(pointA.Latitude), AdditionalMath.ToRad(pointA.Longitude), pointA.Altitude);
}
}
/// <summary>
/// Misc. math functions not available in Elze Kool's lib
/// </summary>
public static class AdditionalMath
{
/// <summary>
/// Degrees to radians
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static double ToRad(double x)
{
return exMath.PI * x / 180.00F;
}
/// <summary>
/// Radians to degrees
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static double ToDeg(double x)
{
return x * 180.00F / exMath.PI;
}
}
}
```

Anybody have any idea what I screwed up?

This is a helpful webform to check values: Calculate distance and bearing between two Latitude/Longitude points using haversine formula in JavaScript