# trig

> sine, cosine and tangent as a point going around the unit circle.

source: https://learning.anthnydas.com/math/trig/

## tl;dr

a point walks around the unit circle, the circle of radius 1 centered at the origin. the angle is how far it has walked, its height is the sine, its x is the cosine, and the slope of the line to it is the tangent. everything else follows from that picture.

- **radians:** θ = arc / radius (a full turn is 2π ≈ 6.28, so 1 rad ≈ 57.3°)
- **the point:** (cos θ, sin θ) (x first, then y)
- **tan θ:** sin θ / cos θ (no value where cos θ = 0)
- **pythagoras:** sin²θ + cos²θ = 1 (the radius is 1)
- **period:** 2π (a full turn changes nothing)
- **arcsin:** [−π/2, π/2] (one angle of the two in each turn)
- **reach for it:** an angle to a direction, and a direction back to an angle, anything that turns or repeats: rotation, waves, orbits, a right triangle with one side and one angle known

## the idea

put a point on the unit circle, the circle of radius 1 centered at the origin, and let it walk counter-clockwise from (1, 0). every question in trig is a question about that point. how far has it walked? that's the angle. how high is it? the sine. how far to the right? the cosine. how steep is the line from the center to it? the tangent. the graphs are what you get by writing those down as it goes around.

> **interactive figure:** an animation in four laps of the unit circle, with a graph beside it. first a point walks around from (1, 0): the angle θ is the arc it has walked, π/2 ≈ 1.57 at the top and 2π ≈ 6.28 for the full turn, and the same length is laid along the axis of the graph. second lap: the point's height, sin θ, is plotted against θ and draws the sine wave: 0, then 1 at π/2, 0 at π, −1 at 3π/2, 0 at 2π. third lap: its x, cos θ, draws the same wave a quarter turn ahead: 1 at 0, 0 at π/2, −1 at π. fourth lap: tan θ, the slope of the radius, read off where the extended radius meets the line x = 1: 0 at 0, 1 at π/4, off the top near π/2 and back from the bottom past it, with no value at π/2 and 3π/2.

a point walks around the unit circle, starting at (1, 0). the angle $\theta$ is how far it has walked along the arc: $\pi/2 \approx 1.57$ at the top, $\pi$ at the far left, $2\pi \approx 6.28$ for the full turn. the same length is laid along the axis of the graph beside it.

the height of the point is $\sin\theta$. plot it against $\theta$ and the circle unrolls into the sine wave: 0 at the start, 1 at $\pi/2$, 0 at $\pi$, $-1$ at $3\pi/2$, and 0 again after the full turn.

its $x$ is $\cos\theta$: the same wave, a quarter turn ahead. it starts at 1, is 0 when the point is straight up, and $-1$ at the far left.

$\tan\theta$ is the slope of the radius, $\sin\theta / \cos\theta$: extend the radius and it's how far up the line $x = 1$ it reaches. it's 1 at $\pi/4$, runs off the top near $\pi/2$ and comes back from the bottom past it. where $\cos\theta = 0$ there is no value.

## explore

> **interactive figure:** a point on the unit circle, at 60° to start, with the line from the center to it, its height in cyan, its x in amber, and the slope of the radius measured up the line x = 1. beside it, a graph of sin in cyan and cos in amber from 0 to 2π with the same angle marked, level with the point. drag the point, or tab to it and use the arrow keys, and the readout follows: θ in degrees and radians, 60° = π/3 ≈ 1.047 to start, sin θ = √3/2 ≈ 0.866, cos θ = 1/2, tan θ = √3 ≈ 1.732, and the quadrant with the sign of each. at 90° and 270° tan reads undefined.

## try it

> **interactive figure:** a practice exercise with a small circle under each question: the signs of cos θ and sin θ in a quadrant, the exact value of sin, cos or tan at a special angle like 2π/3, and degrees to radians and back. after an answer the circle shows the legs with their signs or their exact values.

## the rules

### radians

$$
\begin{gathered}
\theta = \frac{s}{r} \\[6pt]
2\pi \text{ rad} = 360^\circ \\[6pt]
1 \text{ rad} = \frac{180^\circ}{\pi} \approx 57.3^\circ
\end{gathered}
$$

an angle in radians is the length of arc it cuts out of the unit circle. a full turn is the whole circumference, 2π ≈ 6.28, a half turn is π, and a right angle is π/2 ≈ 1.57. it holds on any circle: an arc on a circle of radius r is r times longer, so arc over radius depends on the angle alone. to convert, multiply by π/180 one way and by 180/π the other: 90° = π/2, 60° = π/3, 45° = π/4, 30° = π/6, and 1° = π/180 ≈ 0.01745.

### the point

$$
\begin{gathered}
(x, y) = (\cos\theta,\ \sin\theta) \\[6pt]
\tan\theta = \frac{\sin\theta}{\cos\theta}
\end{gathered}
$$

the point at angle θ is (cos θ, sin θ). that is the definition: cos is the x and sin is the y, for any θ, including angles past 90° and negative ones, where no triangle fits. tan θ is the slope of the radius, rise over run. it has no value where cos θ = 0, at π/2, 3π/2 and every half turn from there: the radius is vertical, and a vertical line has no slope.

### signs by quadrant

sin is the height, so it's positive above the x axis: quadrants I and II. cos is the x, positive to the right of the y axis: I and IV. tan is their ratio, positive where they have the same sign: I and III.

| quadrant | θ          | cos θ | sin θ | tan θ |
| -------- | ---------- | ----- | ----- | ----- |
| I        | 0 to π/2   | +     | +     | +     |
| II       | π/2 to π   | −     | +     | −     |
| III      | π to 3π/2  | −     | −     | +     |
| IV       | 3π/2 to 2π | +     | −     | −     |

### the special angles

| θ       | 0  | π/6  | π/4  | π/3  | π/2  |
| ------- | -- | ---- | ---- | ---- | ---- |
| degrees | 0° | 30°  | 45°  | 60°  | 90°  |
| sin θ   | 0  | 1/2  | √2/2 | √3/2 | 1    |
| cos θ   | 1  | √3/2 | √2/2 | 1/2  | 0    |
| tan θ   | 0  | √3/3 | 1    | √3   | none |

tan has no value at π/2, where cos is 0. two triangles explain the rest of the table. cut a square with a diagonal of 1 in half along the diagonal: the two legs are equal, and pythagoras gives each one √2/2 ≈ 0.707, so sin and cos at 45° are both √2/2 and tan is 1. cut an equilateral triangle of side 1 down the middle: the half base is 1/2, the height is √(1 − 1/4) = √3/2 ≈ 0.866, and the corners are 60° at the base and 30° at the top. so at 60° the height √3/2 is sin and the half base 1/2 is cos, and at 30° they swap. tan at 30° is (1/2) / (√3/2) = 1/√3 = √3/3 ≈ 0.577.

> **interactive figure:** three right triangles. the first has θ at its left corner, with the hypotenuse, the side opposite θ in cyan and the side adjacent to it in amber. the second is an equilateral triangle of side 1 cut down the middle: the left half has a base of 1/2, a height of √3/2, a corner of π/3 at the bottom and π/6 at the top. the third is a square with a diagonal of 1 cut along the diagonal: the lower half has two legs of √2/2 and a corner of π/4.

for an angle outside the first quadrant, take its reference angle, how far it is from the x axis, read the size off the table, and put the sign in from the quadrant: 5π/6 is π/6 short of π, in quadrant II, so sin(5π/6) = 1/2 and cos(5π/6) = −√3/2.

do that for every multiple of 30° and 45° and you have the unit circle chart: 16 angles, each with its radians and its exact point. the sizes are the first quadrant's, four times over. only the signs change.

> **interactive figure:** the 16 special angles as points on the unit circle, each labeled with its degrees, and on a wide screen with its radians and exact (cos θ, sin θ) too: 0° = 0: (1, 0); 30° = π/6: (√3/2, 1/2); 45° = π/4: (√2/2, √2/2); 60° = π/3: (1/2, √3/2); 90° = π/2: (0, 1); 120° = 2π/3: (−1/2, √3/2); 135° = 3π/4: (−√2/2, √2/2); 150° = 5π/6: (−√3/2, 1/2); 180° = π: (−1, 0); 210° = 7π/6: (−√3/2, −1/2); 225° = 5π/4: (−√2/2, −√2/2); 240° = 4π/3: (−1/2, −√3/2); 270° = 3π/2: (0, −1); 300° = 5π/3: (1/2, −√3/2); 315° = 7π/4: (√2/2, −√2/2); 330° = 11π/6: (√3/2, −1/2). tap a point, or tab into the chart and use the arrow keys, Home and End, to pick one: the chart draws its x in amber and its height in cyan, and the readout gives its degrees, radians and exact (cos θ, sin θ). it starts at 150° = 5π/6 ≈ 2.618, with cos θ = −√3/2 ≈ −0.866 and sin θ = 1/2. on a phone a button opens the full chart in a frame that scrolls sideways. buttons under the chart save, copy or share the full chart as an image.

### pythagoras on the circle

$$
\sin^2\theta + \cos^2\theta = 1
$$

the point is on the circle of radius 1, so its distance from the origin is 1, and its coordinates are cos θ and sin θ. pythagoras on the two legs is the identity. it's why one of sin and cos pins down the other up to a sign: cos θ = ±√(1 − sin²θ), with the sign from the quadrant.

### mirrors and turns

$$
\begin{gathered}
\sin(-\theta) = -\sin\theta \\[6pt]
\cos(-\theta) = \cos\theta \\[6pt]
\sin(\theta + 2\pi) = \sin\theta \\[6pt]
\cos\theta = \sin\!\left(\theta + \tfrac{\pi}{2}\right)
\end{gathered}
$$

−θ is the same walk clockwise, so its point is the mirror image across the x axis: the same x, the opposite y. a full turn lands on the same point, so adding 2π changes nothing, and neither does adding any number of turns. a quarter turn sends (x, y) to (−y, x), so the height of the point a quarter turn ahead is this point's x: cos θ = sin(θ + π/2), and the cosine wave is the sine wave moved π/2 to the left. the mirror across the y axis gives two more: sin(π − θ) = sin θ and cos(π − θ) = −cos θ.

### the angle sum

$$
\begin{aligned}
\sin(\alpha + \beta) &= \sin\alpha\cos\beta \\
&\quad + \cos\alpha\sin\beta \\[6pt]
\cos(\alpha + \beta) &= \cos\alpha\cos\beta \\
&\quad - \sin\alpha\sin\beta
\end{aligned}
$$

the point at α + β is the point at β, turned by α. turning by α sends (1, 0) to (cos α, sin α) and (0, 1) to (−sin α, cos α), and turning a sum of arrows turns each one, so (cos β, sin β) = cos β · (1, 0) + sin β · (0, 1) lands on cos β · (cos α, sin α) + sin β · (−sin α, cos α). its x is the cosine formula and its y the sine formula. those two turned arrows are the columns of the rotation matrix on the [cg fundamentals](/graphics/cg-fundamentals/#transforms) page. with α = β: sin 2θ = 2 sin θ cos θ and cos 2θ = cos²θ − sin²θ.

### the graphs

$$
y = a\,\sin\big(b(x - h)\big) + k
$$

sin and cos drawn against θ are waves between −1 and 1 with period 2π, and cos is sin moved π/2 to the left. the four numbers are the transformations from the [functions page](/math/functions/): a stretches the wave up and down, so the amplitude, half its height, is |a|. b squeezes it sideways, so the period is 2π/|b|. h slides it right, k slides it up, and y = k is the midline it swings around.

> **interactive figure:** the graph of a sin(b(x − h)) + k in cyan from −2π to 2π, against sin x dashed in gray, with a slider for each of a, b, h and k. it starts at 2 sin(3(x − π/6)) + 1: amplitude 2, period 2π/3 ≈ 2.09, shifted π/6 right, midline 1, and its highest point, 3, marked at x = π/3. a readout gives those five numbers for any setting.

a spring that overshoots traces a cosine that shrinks: the spring figure on the [motion page](/graphics/motion/) draws one.

### right triangles

$$
\begin{gathered}
\sin\theta = \frac{\text{opposite}}{\text{hypotenuse}} \\[6pt]
\cos\theta = \frac{\text{adjacent}}{\text{hypotenuse}} \\[6pt]
\tan\theta = \frac{\text{opposite}}{\text{adjacent}}
\end{gathered}
$$

take a right triangle with an acute angle θ and scale it so its hypotenuse is 1. put the corner at θ on the origin, and the far corner lands on the circle: the opposite side is the height, sin θ, and the adjacent side is the x, cos θ. scaling keeps every ratio, so the three fractions hold at any size. the circle is the triangle with hypotenuse 1, and it keeps going past 90°, where no triangle does.

### going back

$$
\begin{gathered}
\arcsin x \in \left[-\tfrac{\pi}{2}, \tfrac{\pi}{2}\right] \\[6pt]
\arccos x \in [0, \pi] \\[6pt]
\arctan x \in \left(-\tfrac{\pi}{2}, \tfrac{\pi}{2}\right) \\[6pt]
\operatorname{atan2}(y, x) \in [-\pi, \pi]
\end{gathered}
$$

every height between −1 and 1 is reached twice in a turn, once on the way up and once on the way down, so arcsin has to pick, and it picks the angle nearest 0: between −π/2 and π/2.[^mdn-asin] the other one is π − arcsin x, and then any number of turns on top of either. arccos picks between 0 and π, and arctan, like arcsin, between −π/2 and π/2, so atan(y / x) can't tell (3, 4) from (−3, −4): the ratio is the same. atan2(y, x) takes the two parts separately, y first, and gives the whole direction, from −π to π.[^mdn-atan2] it's the one to reach for when you need the angle of a vector, in a game, a plot or a shader.

## worked examples

**135° to radians, and 5π/6 to degrees.** degrees to radians is times π/180: 135 · π/180 = (135/180) π, and 135/180 reduces to 3/4, so 135° = 3π/4. the other way is times 180/π: (5π/6) (180/π) = 5 · 30 = 150°.

**sin, cos and tan of 5π/6.** 5π/6 is 150°, in quadrant II. the reference angle is π − 5π/6 = π/6, 30°. from the table, sin(π/6) = 1/2 and cos(π/6) = √3/2. in quadrant II sin is positive and cos negative, so sin(5π/6) = 1/2, cos(5π/6) = −√3/2, and tan(5π/6) = (1/2) / (−√3/2) = −1/√3 = −√3/3 ≈ −0.577.

**a right triangle.** the hypotenuse is 10 and one angle is 30°. the side opposite the angle is 10 · sin 30° = 10 · 1/2 = 5. the side next to it is 10 · cos 30° = 10 · √3/2 = 5√3 ≈ 8.66. check with pythagoras: 5² + (5√3)² = 25 + 75 = 100 = 10².

**read a wave.** y = 2 sin(3(x − π/6)) + 1. the amplitude is 2, so it swings from 1 − 2 = −1 up to 1 + 2 = 3. the period is 2π/3 ≈ 2.09. it's shifted π/6 to the right and 1 up, so it crosses its midline going up at x = π/6, and reaches its first high point a quarter period later, at π/6 + π/6 = π/3. that's the wave in the figure above, where it starts.

**the direction of a vector.** which way does (−3, 4) point? atan2(4, −3) = 2.2143 rad = 126.87°: up and to the left, quadrant II, as it should be. atan(4 / −3) = atan(−4/3) = −0.9273 rad = −53.13°, which points down and to the right: the ratio −4/3 is also the ratio of (3, −4), and atan can't tell them apart. the two answers differ by exactly π.

**sin 75° from the angle sum.** 75° = 45° + 30°, so sin 75° = sin 45° cos 30° + cos 45° sin 30° = (√2/2)(√3/2) + (√2/2)(1/2) = (√6 + √2) / 4 ≈ 0.9659. the calculator agrees: sin(75 · π/180) = 0.9659.

## the key idea up close

### the circle, unrolled

the graph of sine is a record of the walk. the horizontal axis is the distance the point has walked along the arc, which is what a radian measures, and the vertical axis is how high the point is at that moment. every moment of the walk is one point of the graph:

$$
(\text{arc walked},\ \text{height}) = (\theta,\ \sin\theta)
$$

that's the whole construction, and the shape of the wave follows from the shape of the circle.

- it starts at 0 and climbs fastest at the start. near (1, 0) the circle runs straight up, so a short step along the arc is almost all height: sin 0.1 = 0.0998. for small angles, sin θ ≈ θ.
- it flattens out at 1 when θ = π/2, where the circle runs level for a moment and the point is as high as it gets.
- it comes back down the mirror image of the way up, since the circle is symmetric across the y axis: sin(π − θ) = sin θ.
- it's negative below the x axis, bottoms out at −1 at 3π/2, and at 2π the point is back where it started. the record repeats: the period is 2π, and nothing in the graph is new after the first turn.
- cos is the same record started a quarter turn earlier, since this point's x is the height of the point a quarter turn ahead. so the cosine wave is the sine wave moved π/2 to the left, and the two together give the point back: (cos θ, sin θ).

the slope of that first climb, 1 at θ = 0, is where calculus picks the story up: the slope of sin θ at any θ is cos θ, and at 0 cos is 1.

## pitfalls

- **degrees in, nonsense out.** `Math.sin(90)` is 0.8939966636005579, not 1: it read 90 radians, 14.32 turns, and answered for where that lands.[^mdn-sin] convert first: `Math.sin(90 * Math.PI / 180)` is 1. the same goes for glsl, numpy and most languages. a result that's wrong but not wildly wrong is the sign.
- **sin π isn't 0 on a computer.** `Math.sin(Math.PI)` is 1.2246467991473532e-16. `Math.PI` is the double nearest π, about 1.2 × 10⁻¹⁶ short of it, and the sine of an angle that close to π is the gap. test against a tolerance, or round before you print.
- **atan against atan2.** `Math.atan(y / x)` loses the quadrant and divides by zero at x = 0. `Math.atan2(y, x)` has neither problem, and it takes y first:[^mdn-atan2] `Math.atan2(4, -3)` is 2.2143, 126.87°, while `Math.atan2(-3, 4)` is −36.87°, the angle of a different vector.
- **tan blows up near π/2.** tan 89° ≈ 57.3 and tan 89.9° ≈ 573, and `Math.tan(Math.PI / 2)` is 16331239353195370, not infinity, because `Math.PI / 2` isn't exactly π/2. a slope is a bad way to store a direction near vertical: keep the angle, or the vector.
- **sin⁻¹ x is arcsin x, not 1 / sin x.** the −1 means the inverse function: `Math.asin(0.5)` is π/6 ≈ 0.524, while 1 / sin 0.5 ≈ 2.086, a different thing, the cosecant.
- **arcsin forgets.** `Math.asin(Math.sin(2.5))` is 0.6416, which is π − 2.5, not 2.5: 2.5 rad is past π/2, outside the range arcsin answers in.[^mdn-asin] to get an angle back from a point, use atan2 on both coordinates.

## practice

1. convert 210° to radians, and 7π/4 to degrees.

   **answer:** 210° = 210 · π/180, and 210/180 reduces to 7/6, so 210° = 7π/6 ≈ 3.665. the other way: 7π/4 = (7/4) · 180° = 315°.

2. without a calculator: cos(5π/4), sin(7π/6) and tan(2π/3).

   **answer:** 5π/4 is 225°, in quadrant III, with reference angle π/4. cos is negative there, so cos(5π/4) = −√2/2. 7π/6 is 210°, also quadrant III, with reference angle π/6: sin(7π/6) = −1/2. 2π/3 is 120°, quadrant II, with reference angle π/3: tan(π/3) = √3, and tan is negative in quadrant II, so tan(2π/3) = −√3.

3. a right triangle has a hypotenuse of 12 and an angle of 30°. find the other two sides.

   **answer:** the side opposite the 30° angle is 12 · sin 30° = 12 · 1/2 = 6. the side next to it is 12 · cos 30° = 12 · √3/2 = 6√3 ≈ 10.39. check: 6² + (6√3)² = 36 + 108 = 144 = 12².

4. for y = 3 cos(2x − π/2) − 1, give the amplitude, the period, the shift, and the highest and lowest values.

   **answer:** factor the inside first: 2x − π/2 = 2(x − π/4), so the wave is 3 cos(2(x − π/4)) − 1. amplitude 3. period 2π/2 = π ≈ 3.14. shifted π/4 to the right and 1 down, so the midline is y = −1. highest −1 + 3 = 2, at x = π/4, where the inside is 0 and cos is 1. lowest −1 − 3 = −4, half a period later at x = 3π/4.

5. solve cos θ = −1/2 for 0 ≤ θ < 2π.

   **answer:** arccos(−1/2) = 2π/3, the one answer between 0 and π. its mirror across the x axis has the same x, so the same cos: 2π − 2π/3 = 4π/3. so θ = 2π/3 or 4π/3. check: both have reference angle π/3, where cos is 1/2, and both sit left of the y axis, where cos is negative.

6. a sprite at (2, 5) has to face a target at (−1, 1). what angle should it turn to, in radians and degrees, and what does atan get wrong?

   **answer:** the direction is the target minus the sprite: (−1 − 2, 1 − 5) = (−3, −4). atan2(−4, −3) = −2.2143 rad = −126.87°, which is 233.13° measured counter-clockwise from the x axis: down and to the left, quadrant III. atan(−4 / −3) = atan(4/3) = 0.9273 rad = 53.13°, up and to the right. the two minus signs cancel in the ratio, so atan sees (3, 4) and answers for the opposite direction.

[^mdn-sin]: mdn's [Math.sin()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin) page: the argument is an angle in radians.

[^mdn-asin]: mdn's [Math.asin()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asin) page: the result is in radians, between −π/2 and π/2 inclusive, and NaN for an input outside −1 to 1.

[^mdn-atan2]: mdn's [Math.atan2()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2) page: the arguments are the y coordinate first and the x second, the result is the angle in radians from the positive x axis to the ray through (x, y), between −π and π inclusive, and a table of the inputs where it differs from atan(y / x).

## related

- https://learning.anthnydas.com/math/functions.md
- https://learning.anthnydas.com/math/vectors.md
- https://learning.anthnydas.com/math/matrices.md
- https://learning.anthnydas.com/graphics/cg-fundamentals.md
- https://learning.anthnydas.com/graphics/motion.md
- game dev (soon)
