Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Angle between two points and x-axis

, 23 May 2011
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Tefik Becirovic
Systems Engineer
Germany Germany
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 23 May 2011
Article Copyright 2011 by Tefik Becirovic
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid