20.1. Converting Between Degrees and Radians
Problem
When using the trigonometric functions of the Math class, all units are in radians. You have some angles measured in degrees and want to convert these to radians in order to use them with the members of the Math class, and some angles measured in radians that need to be in degrees.
Solution
To convert a value in degrees to radians, multiply it by math.PI/180:
using System;
public static double ConvertDegreesToRadians (double degrees)
{
return ((Math.PI / 180) * degrees);
}To convert a value in radians to degrees, multiply it by 180/mathPI:
using System;
public static double ConvertRadiansToDegrees(double radians)
{
return ((180 / Math.PI) * radians);
}Discussion
All of the static trigonometric methods in the Math class use radians as their unit of measure for angles. It is very handy to have conversion routines to convert between radians and degrees, especially when a user is required to enter data in degrees rather than radians. After all, humans understand degrees better than radians.
The static field Math.PI contains the constant 3.14151265358979323846.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access