Trigonometry Basics for Developers and Designers
Trigonometric functions power animations, game physics, audio visualization, and SVG path generation. This guide covers sin, cos, and tan with practical code examples.
Key Takeaways
- Trigonometry is the math of angles and distances.
- For a right triangle with angle θ:
- Most programming languages use radians.
- Place points on a circle of radius r centered at (cx, cy):
Color Palette Generator
Why Developers Need Trigonometry
Trigonometry is the math of angles and distances. It underlies circular motion, wave animations, audio waveforms, radar charts, analog clocks, and collision detection. Understanding sine and cosine unlocks an enormous range of visual and interactive effects.
The Big Three Functions
For a right triangle with angle θ:
- sin(θ) = opposite / hypotenuse
- cos(θ) = adjacent / hypotenuse
- tan(θ) = opposite / adjacent = sin / cos
Radians vs Degrees
Most programming languages use radians. Convert with:
- Degrees to radians:
rad = deg * (π / 180) - Radians to degrees:
deg = rad * (180 / π)
Full circle = 360° = 2π radians.
Practical Applications
Circular Motion
Place points on a circle of radius r centered at (cx, cy):
x = cx + r * cos(angle)
y = cy + r * sin(angle)
Distribute 12 items evenly: increment angle by 2π/12 = 30° between each.
Wave Animation
A sine wave oscillates between -1 and 1. Control amplitude (height), frequency (speed), and phase (offset):
y = amplitude * sin(frequency * x + phase)
Rotation
Rotate point (x, y) by angle θ around the origin:
x' = x * cos(θ) - y * sin(θ)
y' = x * sin(θ) + y * cos(θ)
Distance and Angle Between Points
Distance: d = sqrt((x2-x1)² + (y2-y1)²)
Angle: θ = atan2(y2-y1, x2-x1)
Use atan2 instead of atan because it handles all four quadrants correctly.
Quick Reference
| Angle | sin | cos | tan |
|---|---|---|---|
| 0° | 0 | 1 | 0 |
| 30° | 0.5 | 0.866 | 0.577 |
| 45° | 0.707 | 0.707 | 1 |
| 90° | 1 | 0 | undefined |
| 180° | 0 | -1 | 0 |