Math Tools Math Tools

The Mathematics Behind Computer Graphics

The Mathematics Behind Computer Graphics

By Math Tools ·

The Mathematics Behind Computer Graphics

Almost every 3D object you've ever seen on a screen, from a game character to a movie dinosaur, is made of triangles. Not curves, not clay: flat triangles, often millions of them, each defined by just three points.

Turning those triangles into a realistic image comes down to a surprisingly small toolbox: vectors, matrices, a cosine, and one division. This is the mathematics every graphics card performs billions of times per second.


Perspective Is Just Division

Why do distant objects look smaller? It feels like it should need complex optics. In a computer, it comes down to a single operation: divide by depth.

A point at (x, y, z) in front of a camera appears on the screen at:

x_screen = f × x / z
y_screen = f × y / z

Where f is the focal length. Double the distance z and the object's image halves in size. Renaissance painters like Filippo Brunelleschi worked out linear perspective geometrically in the early 1400s. Graphics cards do it with one division per vertex.


Building Block 1: Vectors

A vector has direction and length, written as components like v = (3, 4, 0). Graphics uses vectors for positions, directions, surface orientations and light rays.

Two operations matter most:

Dot product:

a · b = aₓbₓ + a_yb_y + a_zb_z = |a| |b| cos θ

Cross product:

a × b = (a_yb_z − a_zb_y,  a_zbₓ − aₓb_z,  aₓb_y − a_ybₓ)

The dot product measures how aligned two vectors are. The cross product gives a vector perpendicular to both, which is exactly how a renderer finds the direction a triangle faces, called its normal vector.


Building Block 2: Matrices

To move, rotate or scale an object, you multiply every vertex by a transformation matrix. A rotation by angle θ around the z-axis is:

[ cos θ   −sin θ   0 ]
[ sin θ    cos θ   0 ]
[   0        0     1 ]

Rotating a point by 90° around z turns (1, 0, 0) into (0, 1, 0). Try multiplying matrices yourself with the matrix multiplication calculator, and convert angles with degrees to radians.


Homogeneous Coordinates: The 4th Dimension Trick

Rotation and scaling are linear transformations, so they fit in a 3 × 3 matrix. But translation (moving an object) isn't linear. You can't write "add 5 to x" as a 3 × 3 multiplication.

The solution is homogeneous coordinates: add a fourth component w = 1, so a point becomes (x, y, z, 1). Now translation is a 4 × 4 matrix:

[ 1  0  0  tₓ ]
[ 0  1  0  t_y ]
[ 0  0  1  t_z ]
[ 0  0  0  1  ]

Every transformation, including perspective projection, becomes a 4 × 4 matrix. A whole chain of transformations (model → world → camera → screen) collapses into one matrix product. After projection, dividing by w performs the perspective divide.


Lighting: Lambert's Cosine Law

How bright should a surface be? In 1760, Swiss mathematician Johann Heinrich Lambert described how a matte surface's brightness depends on the angle of incoming light. Brightness is proportional to the cosine of the angle between the surface normal N and the light direction L:

brightness = max(0, N · L)

Since N and L are unit vectors, their dot product is that cosine. Light hitting a surface head-on (0°) gives full brightness. At 60°, brightness drops to cos 60° = 0.5. At 90° or more, the surface is in shadow. Check the values with the cosine calculator.


Rasterization: Filling in Triangles

Once a triangle's three corners are on screen, the renderer decides which pixels it covers. For a pixel point P, it computes barycentric coordinates (α, β, γ):

P = αA + βB + γC,   where α + β + γ = 1

If all three are non-negative, P is inside the triangle. The same weights then blend colors, textures and depth values smoothly across the triangle's surface.


Curves: Bézier and de Casteljau

Fonts, vector logos and animation paths use Bézier curves. A cubic Bézier curve with control points P₀, P₁, P₂, P₃ is:

B(t) = (1−t)³P₀ + 3(1−t)²tP₁ + 3(1−t)t²P₂ + t³P₃,   0 ≤ t ≤ 1

The curves are named after Pierre Bézier, who popularized them for car body design at Renault in the 1960s. But Paul de Casteljau had developed an equivalent method at rival automaker Citroën around 1959. Citroën kept it secret, so Bézier's name stuck.


An Insider Reference: The Birth of Interactive Graphics

In 1963, MIT PhD student Ivan Sutherland built Sketchpad, a program that let users draw directly on a screen with a light pen. It introduced ideas such as constraints, zooming and reusable objects. Sutherland received the 1988 Turing Award, and many early graphics pioneers came through the University of Utah program he helped build, including Pixar co-founder Ed Catmull.


Two Concepts Worth Knowing

Linear Transformations

A linear transformation preserves addition and scaling: T(a + b) = T(a) + T(b). Every linear transformation of 3D space can be written as a matrix, which is why GPUs are built to multiply matrices extremely fast.

Normalization

A unit vector has length 1. To normalize v, divide it by its length √(vₓ² + v_y² + v_z²). Lighting formulas only work with normalized vectors.


Quick Answer: What Math Is Used in Computer Graphics?

Computer graphics relies on linear algebra: vectors for positions and directions, 4 × 4 matrices in homogeneous coordinates for transformations and perspective, dot products for lighting, barycentric coordinates to fill triangles, and polynomial curves such as Bézier curves for smooth shapes.


Try Them Yourself

Take the point (1, 0), rotate it by 30°, 60° and 90° with the rotation matrix, and plot the results. You'll have drawn part of a circle using nothing but matrix multiplication.