Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Converting degree to radian in JavaScript
Converting degrees to radians is a common mathematical operation in JavaScript, especially when working with trigonometric functions or graphics programming.
Understanding Radians
The radian is the standard unit for measuring angles in mathematics. One complete circle equals 2? radians, which is equivalent to 360 degrees. This means ? radians equals 180 degrees.
The Conversion Formula
To convert degrees to radians, multiply the degree value by ?/180:
radians = degrees × (? / 180)
Method 1: Using the Standard Formula
const degreeToRadian = (degree) => {
const factor = Math.PI / 180;
const rad = degree * factor;
return rad;
};
// Test with common angles
console.log("90° =", degreeToRadian(90), "radians");
console.log("180° =", degreeToRadian(180), "radians");
console.log("360° =", degreeToRadian(360), "radians");
90° = 1.5707963267948966 radians 180° = 3.141592653589793 radians 360° = 6.283185307179586 radians
Method 2: Simplified One-liner
const toRadians = (degrees) => degrees * (Math.PI / 180);
console.log("45° =", toRadians(45));
console.log("270° =", toRadians(270));
45° = 0.7853981633974483 270° = 4.71238898038469
Common Degree to Radian Conversions
| Degrees | Radians (Exact) | Radians (Decimal) |
|---|---|---|
| 0° | 0 | 0 |
| 30° | ?/6 | 0.524 |
| 45° | ?/4 | 0.785 |
| 90° | ?/2 | 1.571 |
| 180° | ? | 3.142 |
| 360° | 2? | 6.283 |
Practical Example: Circle Animation
function calculateCirclePoints(radius, numPoints) {
const points = [];
const angleStep = 360 / numPoints; // degrees between each point
for (let i = 0; i {
console.log(`${point.degrees}° (${point.radians} rad): (${point.x}, ${point.y})`);
});
Points on circle: 0° (0.000 rad): (10.00, 0.00) 90° (1.571 rad): (0.00, 10.00) 180° (3.142 rad): (-10.00, 0.00) 270° (4.712 rad): (-0.00, -10.00)
Conclusion
Converting degrees to radians in JavaScript is straightforward using the formula `degrees × (? / 180)`. This conversion is essential for trigonometric calculations and graphics programming where precise angle measurements are required.
Advertisements
