Calculate Angle from point - CSharp System

CSharp examples for System:Math Geometry

Description

Calculate Angle from point

Demo Code


using System.Windows.Media.Media3D;
using System;/*from w  w  w. j  a v  a2  s  .c  o m*/

public class Main{
        internal static double CalculateAngle(double px1, double py1, double px2, double py2)
        {
            //// Negate X and Y values
            double resX = px2 - px1;
            double resY = py2 - py1;
            double angle = 0.0;
            //// Calculate the angle
            if (resX == 0.0)
            {
                if (resY == 0.0)
                    // was resY. i think 0,0 is seen as same as up... There is an error in http://www.carlosfemmer.com/post/2006/02/Calculate-Angle-between-2-points-using-C.aspx  :-)
                {
                    angle = 0.0;
                }
                else if (resY > 0.0)
                {
                    angle = Math.PI/2.0;
                }
                else
                {
                    angle = Math.PI*3.0/2.0;
                }
            }
            else if (resY == 0.0)
            {
                if (resX > 0.0)
                {
                    angle = 0.0;
                }
                else
                {
                    angle = Math.PI;
                }
            }
            else
            {
                if (resX < 0.0)
                {
                    angle = Math.Atan(resY/resX) + Math.PI;
                }
                else if (resY < 0.0)
                {
                    angle = Math.Atan(resY/resX) + (2*Math.PI);
                }
                else
                {
                    angle = Math.Atan(resY/resX);
                }
            }

            angle = angle*180/Math.PI; //// Convert to degrees
            return angle;
        }
}

Related Tutorials