Linear Algebra for Graphics
A hands-on introduction to the vectors and matrices behind computer graphics. Play with the arrows and sliders.
What is a vector?
A vector describes a length and a direction. In a 2D coordinate plane with an x-axis and a y-axis, the vector means 4 units in the x direction and 3 units in the y direction.
Unit vectors & normalization
Often you care about direction, not length. Dividing a vector by its magnitude produces a unit vector of length 1 in the same direction:
Unit vectors show up everywhere in graphics: surface normals, light directions, camera axes, and ray directions. The dashed circle below has radius 1; the solid arrow always lands on it.
Addition & subtraction
For and , add the corresponding components:
Subtraction works the same way, with the second vector negated: .
Dot product
The dot product of two vectors gives a scalar. It measures how much one vector points in the direction of another.
It also relates to the angle between the vectors, , and to the signed length of the projection of a onto b, .
Projection
The orthogonal projection of onto is the shadow of on the line spanned by . It is the part of that lies parallel to :
What is left over is the rejection, perpendicular to : . Lighting, constraints, and sliding along a surface all use this split.
Cross product
The cross product takes two vectors, a and b, and produces a third vector perpendicular to both, following the right-hand rule. In 2D we only see its z-component: a signed number equal to the area of the parallelogram the two vectors span.
A positive value means b is counterclockwise from a; negative means clockwise.
Matrices
A matrix is an array of numbers with rules for multiplying vectors and other matrices. In graphics, each column is where a basis vector lands: the first column is the image of , the second of .
Matrix multiplication composes transforms. Order matters: means “apply first, then ” (right to left). In general .
Rotation
To rotate a vector by θ, picture rotating the basis vectors and watching where they land. The x-axis unit vector becomes , and the y-axis unit vector becomes . Those two new columns are the rotation matrix:
The first column is where the new x-axis points; the second is where the new y-axis points. Multiplying a vector by R gives the rotated vector:
Scale
Scaling works the same way. To stretch x by 3 and shrink y to half, the new x-axis unit vector becomes and the new y-axis unit vector becomes :
Shear
Shear slides one axis along the other. A horizontal shear () keeps heights fixed and pushes by an amount proportional to . Vertical shear () does the opposite. Together they form the matrix:
Shear is still a linear map (the origin stays put), but it is not a pure rotation or scaling. Reflection is related: flipping an axis is equivalent to applying a negative scale, as shown in the Scale playground.
Translation
Translation shifts every point by a fixed offset: translated by lands at .
Unlike rotation and scaling, translation cannot be written as a matrix multiplied by a 2D vector: there is no matrix M for which produces for every x and y. Homogeneous coordinates solve this problem.
Homogeneous coordinates
To fold translation into matrix multiplication, we add an extra dimension. A 2D vector becomes , and translation becomes:
Rotation and scaling extend the same way: add a third row and column that pass the extra coordinate through unchanged:
Now every affine transformation, including rotation, scaling, and translation, is represented by a 3×3 matrix. These transformations all compose through ordinary matrix multiplication.
Inverse transforms
Every nonsingular transform has an inverse that undoes it. Invert a translation with , a rotation with , and a nonzero scale with . For a rotation matrix, the inverse is simply the transpose: .
For a composite , the inverse reverses the order: . Apply , then , and you return to the original coordinates.
Determinant
The determinant of a 2×2 matrix is the signed area of the parallelogram formed by its columns. If , then:
A positive value means orientation is preserved (a counterclockwise basis stays counterclockwise). A negative value means the shape is flipped by a reflection. The absolute value tells us how much the matrix scales area. Zero means the matrix collapses space onto a line.
Putting it together
Suppose a table asset must be transformed. First translate it by , then rotate it counterclockwise by θ, and finally scale it by and . Transformations apply from right to left, so the combined matrix is:
Multiply the three matrices once in composition order, with translation innermost because it is rightmost. Reuse that single M for every vertex of the table, no matter how complex the mesh.
Playground: move the sliders or type values. The dashed rectangle is the original table; the vertex is marked before and after.
Loading 3D sections…
Next: Part II: Viewing