# Get heading from 2 IPoints

I am moving an object from one point to another and would like to update the heading of the dynamic display properties. I am using this code:

``````m_dynamicSymbolProperties.set_Heading(esriDynamicSymbolType.esriDSymbolMarker, heading);
``````

But I am having trouble getting my heading right. Does anyone know how to calculate a heading based on two IPoints which have standard esriSRGeoCS_WGS1984 spatial references?

-

``````float getHeadingFromTwoPoints(IPoint A, IPoint B)
{

double dLon = lon2 - lon1;
double tmp0 = Math.Sin(dLon) * Math.Cos(lat2);
double tmp1 = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(dLon);
double tmp = Math.Atan2(tmp0, tmp1);
double tc1 = tmp % (2 * Math.PI);
double theta = (float)(tc1 * (180 / Math.PI));
}

{
return (input * (Math.PI / 180));
}
``````
-

The heading of a 2D vector is simply the unit vector fed into the Atan2 function.

``````        //calculate the 2D vector components dX, dY
double dX = x2 - x1;
double dY = y2 - y1;

//determine the magnitude of the vector
double length = Math.Sqrt(dX * dX + dY * dY);

//normalize the vector to a unit vector
double nX = dX / length;
double nY = dY / length;

//get the angle making use of Atan2, multiply by 180/pi to get degrees
double angle = Math.Atan2(nX, nY) * (180 / Math.PI);
``````

In any solution, you'll have to determine what 'quadrant' the result ends up in, and adjust the angle from there. Depending of course what system of angles you want the result in (quadrant bearings, or 0 degrees = grid north, or 0 degrees = the positive X axis, etc).

Googling 'determine quadrant' or something will get you somewhere with this, and you'll see better diagrams than I could draw here as my Ascii art sucks :-) Or the ArcObjects API might have a function to do it all for you.

-