65.9K
CodeProject is changing. Read more.
Home

Angle between two points and x-axis

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.40/5 (5 votes)

May 23, 2011

CPOL
viewsIcon

64768

Calculate angle between two points and the x-axis using C#

Using Math.Atan2 method:
const double Rad2Deg = 180.0 / Math.PI;
const double Deg2Rad = Math.PI / 180.0;

/// <summary>
/// Calculates angle in radians between two points and x-axis.
/// </summary>
private double Angle(Point start, Point end)
{
    return Math.Atan2(start.Y - end.Y, end.X - start.X) * Rad2Deg;
}
Pay attention: changing the sign of Y-coordinates causes a transformation of the fourth to the first quadrant because .NET coordinate system is not the same as mathematical ones.