Search code examples
c#mathangle

Calculating the angle and converting it to degrees


I am having trouble getting an accurate angle measurement and converting it to degrees.

I first calculated the distance with:

double distance = Math.Sqrt(deltax + deltay);
Console.WriteLine("Distance :" + Math.Round(distance, 3));
Console.WriteLine("");

Then tried to calculate the angle:

double angle = Math.Atan2(deltay, deltax);
double RadiantoDegree = (angle * 180 / Math.PI);
Math.Round((decimal)angle, 3);
Console.WriteLine("Angle :" + angle)

With inputs such as x1=2, y1=2, x2=1, y2=1, the angle should measure -135.000 degrees.


Solution

  • The problem is that you are not using your converted value of angle.

    You could do:

    angle = (angle * 180 / Math.PI);
    

    instead of defining RadiantToDegree but never using it. So what happens is right now you are basically just printing your angle in radians.