Click here to Skip to main content
15,888,803 members
Articles / Programming Languages / C#
Tip/Trick

Angle between two points and x-axis

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
23 May 2011CPOL 63.7K   5   2
Calculate angle between two points and the x-axis using C#
Using Math.Atan2 method:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCalculate angle between two points Pin
Ravi.Kumar02112-Jan-14 6:55
professionalRavi.Kumar02112-Jan-14 6:55 
GeneralUm ... two points define a line. You need three to define an... Pin
Chris Trelawny-Ross23-May-11 9:14
Chris Trelawny-Ross23-May-11 9:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.