Introduction

“Sine” and “cosine” sound like scary school maths, but in graphics they have a very friendly meaning: they’re the x and y of a point going round a circle. Whenever something rotates, orbits, bobs, waves or pulses, they’re doing the work. This post covers:

  • Measuring angles, in degrees and in radians
  • What sine and cosine actually are, using a circle
  • How they make rotation work
  • How they make smooth waves for animation

It builds on Coordinates and Vectors.

Angles: Degrees and Radians

You’re probably used to measuring angles in degrees: 90° is a right angle, 180° is a half turn, 360° is a full turn.

Maths libraries and shaders almost always use radians instead. One radian is the angle you get when you walk around a circle a distance equal to its radius. A full turn is 2π radians (π, “pi”, is about 3.14159, so a full turn is about 6.28 radians). So:

  • 360° = 2π radians ≈ 6.28
  • 180° = π radians ≈ 3.14
  • 90° = π ÷ 2 radians ≈ 1.57

To convert, multiply degrees by π ÷ 180. Radians are used because they make the maths underneath simpler; for us, the only thing to remember is to convert before calling sin or cos, which expect radians.

Sine and Cosine: A Point on a Circle

Imagine a circle with a radius of exactly 1, centred on the origin (called the unit circle). Start at the point (1, 0) on its right-hand edge, and walk anticlockwise round the circle by some angle. Where do you end up?

  • Your x coordinate is the cosine of the angle, written cos(angle).
  • Your y coordinate is the sine of the angle, written sin(angle).

That’s the whole definition. The point below spins by itself. Drag it round the circle (or select it and use the arrow keys), or drag along the wave graph to pick an angle by its position; either one pauses the spin.

A point going round the unit circle, and the sine wave it traces

Some things to notice:

  • At 0°, the point is at (1, 0): cos is 1 and sin is 0. At 90°, it’s at the top, (0, 1). At 180°, (−1, 0). At 270°, the bottom, (0, −1).
  • Both values always stay between −1 and 1, because the circle has radius 1.
  • Because the point is always 1 from the centre, Pythagoras (vectors) says cos² + sin² = 1, always.
  • As the point goes round and round, its height (sine) rises and falls smoothly, tracing the famous sine wave. Cosine traces exactly the same wave, just a quarter turn ahead.

This also explains a puzzle from the dot product post: the dot product of two unit vectors is the cosine of the angle between them. Turn one of them so it lies along the x axis, and the dot product just picks out the other one’s x coordinate, which is the cosine of the angle.

Rotation

To place something at a given angle and distance from a centre, use: (distance × cos(angle), distance × sin(angle)). That’s how objects are arranged in a ring, how a camera orbits a target, and how a clock hand works.

Rotating a whole shape uses the same idea. In Transformation Matrices, rotating sent the x axis to (cos θ, sin θ) and the y axis to (−sin θ, cos θ) (θ, “theta”, is a common name for an angle): those are just the points a quarter turn apart on the unit circle. The rotation matrix is two points on the unit circle, written in a grid.

Waves

Because sine goes up and down smoothly forever, it’s the perfect building block for anything that moves back and forth: bobbing, breathing, swaying, pulsing lights, water. Four numbers shape a wave:

  • Amplitude: how tall it is (multiply the result)
  • Frequency: how many waves fit in a given distance (multiply what goes in)
  • Speed: how fast it travels (add time × speed to what goes in)
  • Phase: where it starts (add a fixed angle)

Shape the wave below. Across the picture, x goes from 0 at the left to 2π at the right: one full turn of the circle in radians. So with a frequency of 1, exactly one wave fits across; with 3, three waves. The grass at the bottom bends using the same formula.

Shaping a sine wave, and grass bending with it

This is exactly the Wave preset in the Vertex Shaders editor: p.y += amount * sin(p.x * 4.0 + uTime * 2.0), where p is the vertex’s position, amount is the wave’s height (the amplitude), 4.0 is the frequency, and uTime is the time in seconds, so 2.0 is the speed. Adding a few sine waves of different sizes and speeds together gives convincing water.

Under the hood

Graphics cards compute sin and cos with special hardware units that give a fast, slightly approximate answer, typically in one instruction (or a few) rather than the long calculation a maths library on the CPU might do. That’s accurate enough for animation and shading, but it’s why very large inputs (for example, a time value that has been counting up for days) can make waves go jittery: the angle loses precision as the number grows. Games usually keep time values small by wrapping them round.

Where You’ll Use This

Questions You Might Have

What about tan?

Tangent is sine divided by cosine: the slope of the line from the centre to the point (how far up per step across). Graphics uses it less than sine and cosine, mostly to turn a field-of-view angle into a size, as the perspective matrix does.

Summary

  • Angles can be measured in degrees (360 per turn) or radians (2π ≈ 6.28 per turn); code usually wants radians.
  • On a circle of radius 1, a point at an angle is at (cos, sin).
  • Both stay between −1 and 1, and cos² + sin² = 1.
  • Sine and cosine give rotation, orbits and smooth waves.

Further Reading