Why This Matters
Sine, cosine, and tangent are the three core functions of trigonometry. They connect angles to ratios of sides in a right triangle. If you know one angle and one side length, these functions let you calculate everything else about the triangle. This is the foundation for measuring distances, heights, and positions that you cannot reach directly.
In computing, sin and cos are everywhere. They generate circular motion in animations, produce sound waves in audio synthesis, calculate lighting angles in 3D rendering, and determine projectile paths in physics simulations. When a game character moves in a direction defined by an angle, the code uses cos for the horizontal component and sin for the vertical component. Without these three functions, modern graphics, audio, and physics engines simply would not work.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
SOH-CAH-TOA: the three trig ratios connect an angle to the sides of a right triangle.
Code Example
// Computing sin, cos, tan from triangle sides
function trigRatios(opposite, adjacent) {
const hypotenuse = Math.sqrt(opposite ** 2 + adjacent ** 2);
return {
sin: opposite / hypotenuse,
cos: adjacent / hypotenuse,
tan: opposite / adjacent
};
}
// Classic 3-4-5 triangle
const ratios = trigRatios(3, 4);
console.log("sin:", ratios.sin); // 0.6
console.log("cos:", ratios.cos); // 0.8
console.log("tan:", ratios.tan); // 0.75
// Verify with built-in functions
// First find the angle using arctan
const angleRad = Math.atan2(3, 4);
console.log("angle:", (angleRad * 180 / Math.PI).toFixed(2), "degrees"); // 36.87
console.log("Math.sin:", Math.sin(angleRad).toFixed(4)); // 0.6000
console.log("Math.cos:", Math.cos(angleRad).toFixed(4)); // 0.8000
console.log("Math.tan:", Math.tan(angleRad).toFixed(4)); // 0.7500
// Common angles
const angles = [0, 30, 45, 60, 90];
for (const deg of angles) {
const rad = deg * Math.PI / 180;
console.log(`${deg} deg: sin=${Math.sin(rad).toFixed(4)} cos=${Math.cos(rad).toFixed(4)}`);
}
// Using sin and cos for circular motion
function pointOnCircle(centerX, centerY, radius, angleDeg) {
const rad = angleDeg * Math.PI / 180;
return {
x: centerX + radius * Math.cos(rad),
y: centerY + radius * Math.sin(rad)
};
}
console.log("Point at 45 deg:", pointOnCircle(0, 0, 10, 45));Interactive Experiment
Try these exercises:
- Compute sin, cos, and tan for a 5-12-13 right triangle. Verify that sin squared plus cos squared equals 1.
- What is sin(0), sin(90 degrees), cos(0), and cos(90 degrees)? Do the values match what you expect from a right triangle?
- Calculate tan(89 degrees) and tan(90 degrees). What happens at exactly 90 degrees and why?
- Use sin and cos to compute the x and y coordinates of 8 evenly spaced points around a circle of radius 100.
- Verify that tan(A) equals sin(A) divided by cos(A) for several different angles.
Quick Quiz
Coding Challenge
Write a function called `trigFromSides(opposite, adjacent)` that takes the lengths of the opposite and adjacent sides of a right triangle and returns the sine, cosine, and tangent of the angle. First compute the hypotenuse using the Pythagorean theorem: hyp = sqrt(opp^2 + adj^2). Then compute sin = opp/hyp, cos = adj/hyp, tan = opp/adj. Print each value rounded to 4 decimal places on a separate line in the format: sin: 0.6000, cos: 0.8000, tan: 0.7500.
Real-World Usage
Sine, cosine, and tangent are used constantly in software:
- Game physics: Decomposing velocity into x and y components uses cos and sin. A projectile fired at angle A with speed v has horizontal velocity v * cos(A) and vertical velocity v * sin(A).
- 3D graphics: Lighting calculations use the dot product, which involves cosine. The brightness of a surface depends on the cosine of the angle between the light direction and the surface normal.
- Audio synthesis: Sound waves are sine waves. Combining sin functions at different frequencies and amplitudes creates complex sounds. This is the basis of FM synthesis and Fourier analysis.
- CSS transforms: The rotate() transform uses sin and cos internally to compute the new positions of every pixel in the rotated element.
- Machine learning: Transformer models use sinusoidal positional encoding. Each position in the input sequence gets a unique pattern of sin and cos values to encode its location.