การแปลงระหว่าง Degrees (องศา) และ Radians (เรเดียน) เป็นพื้นฐานสำคัญในคณิตศาสตร์ วิศวกรรม ฟิสิกส์ และ Computer Graphics Python มีฟังก์ชันใน math และ numpy สำหรับการแปลงนี้
ความสัมพันธ์พื้นฐาน
180° = π ระดียน
- 1° = π/180 ≈ 0.0174533 ระดียน
- 1 ระดียน = 180/π ≈ 57.29578°
โมดูล math (Standard Library)
math.degrees(rad) - ระดียน → องศา
import math
print(f"1 rad = {math.degrees(1):.6f}°") # 57.295780°
print(f"π rad = {math.degrees(math.pi):.6f}°") # 180.000000°
print(f"2π rad = {math.degrees(2*math.pi):.6f}°") # 360.000000°
print(f"π/2 rad = {math.degrees(math.pi/2):.6f}°") # 90.000000°
math.radians(deg) - องศา → ระดียน
import math
print(f"1° = {math.radians(1):.6f} rad") # 0.017453 rad
print(f"90° = {math.radians(90):.6f} rad") # 1.570796 rad (π/2)
print(f"180° = {math.radians(180):.6f} rad") # 3.141593 rad (π)
print(f"360° = {math.radians(360):.6f} rad") # 6.283185 rad (2π)
โมดูล numpy (Array Operations)
NumPy มีฟังก์ชันที่ทำงานกับ Array ได้โดยตรง (Vectorized)
np.deg2rad() / np.radians() - องศา → ระดียน
np.rad2deg() / np.degrees() - ระดียน → องศา
import numpy as np
# Single Values
print(f"np.deg2rad(90) = {np.deg2rad(90):.6f}") # 1.570796
print(f"np.rad2deg(np.pi) = {np.rad2deg(np.pi):.6f}") # 180.000000
# Arrays (Vectorized!)
angles_deg = np.array([0, 30, 45, 60, 90, 180, 270, 360])
angles_rad = np.deg2rad(angles_deg)
print("Degrees:", angles_deg)
print("Radians:", np.round(angles_rad, 6))
# [0. 0.523599 0.785398 1.047198 1.570796 3.141593 4.712389 6.283185]
# กลับกัน
back_to_deg = np.rad2deg(angles_rad)
print("Back to degrees:", back_to_deg.astype(int))
# [ 0 30 45 60 90 180 270 360]
การประยุกต์ใช้งานจริง
1. Trigonometric Functions (ต้องการ Radians)
import math
import numpy as np
# math functions ต้องการ Radians
angle_deg = 45
angle_rad = math.radians(angle_deg)
sin_val = math.sin(angle_rad)
cos_val = math.cos(angle_rad)
tan_val = math.tan(angle_rad)
print(f"sin(45°) = {sin_val:.6f}") # 0.707107
print(f"cos(45°) = {cos_val:.6f}") # 0.707107
print(f"tan(45°) = {tan_val:.6f}") # 1.000000
# NumPy Vectorized
angles = np.array([0, 30, 45, 60, 90])
rad = np.deg2rad(angles)
print("sin:", np.round(np.sin(rad), 6))
print("cos:", np.round(np.cos(rad), 6))
2. Computer Graphics: Rotation Matrix
import numpy as np
def rotation_matrix_2d(angle_deg):
"""2D Rotation Matrix จากองศา"""
theta = np.deg2rad(angle_deg)
c, s = np.cos(theta), np.sin(theta)
return np.array([[c, -s], [s, c]])
# หมุน 90 องศา
R90 = rotation_matrix_2d(90)
point = np.array([1, 0])
rotated = R90 @ point
print(f"Rotate (1,0) by 90°: {rotated}") # [0. 1.]
3. Physics: Angular Velocity
# Angular Velocity: ω (rad/s) = 2π * f (Hz)
# Frequency f = 60 Hz (AC Power)
f = 60
omega = 2 * np.pi * f
print(f"Angular velocity at 60 Hz: {omega:.2f} rad/s") # 376.99 rad/s
# Convert to RPM (Revolutions Per Minute)
rpm = omega * 60 / (2 * np.pi)
print(f"RPM: {rpm:.0f}") # 3600
ตารางค่าสำคัญ (Reference)
| Degrees | Radians (Exact) | Radians (Decimal) | sin | cos |
|---|---|---|---|---|
| 0° | 0 | 0.000000 | 0 | 1 |
| 30° | π/6 | 0.523599 | 1/2 | √3/2 |
| 45° | π/4 | 0.785398 | √2/2 | √2/2 |
| 60° | π/3 | 1.047198 | √3/2 | 1/2 |
| 90° | π/2 | 1.570796 | 1 | 0 |
| 180° | π | 3.141593 | 0 | -1 |
| 270° | 3π/2 | 4.712389 | -1 | 0 |
| 360° | 2π | 6.283185 | 0 | 1 |
สรุป
- math.degrees()/radians(): สำหรับ Scalar Values (Single Numbers)
- np.deg2rad()/rad2deg(): สำหรับ Arrays/Vectorized Operations
- Trigonometric Functions (
sin, cos, tan) ใน Python ต้องการ Radians เสมอ - ใช้
math.piหรือnp.piสำหรับค่า π ที่แม่นยำ