Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Angle between two points and x-axis

0.00/5 (No votes)
23 May 2011 1  
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here