Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 50 · Projects, Practice and Review

Computer Graphics Cheat Sheet

A one-page computer graphics cheat sheet: key formulas, matrices, line and circle algorithms, clipping, shading, ray tracing and pipeline stages, with live checks.

This page is the whole track on one screen. It lists the formulas, matrices, algorithms and pipeline stages you will reach for most often, with a link to the lesson that explains each one. Use it for quick revision before an exam or interview, or as a reference while you write a renderer.

Conventions used throughout: points are column vectors, (x, y, z, 1) in homogeneous form; matrices multiply on the left (M · p); angles are in radians in code. The notation N is a unit normal, L the unit vector to the light, V the unit vector to the viewer and H the half vector.

Algorithms at a glance

Task Algorithm Key idea Cost
Line DDA Step the larger delta by 1, add slope increments, round O(n) pixels, floating point
Line Bresenham Integer decision variable picks the closer pixel O(n), integers only
Circle Midpoint Evaluate x² + y² - r² at the midpoint; 8-way symmetry O(r)
Ellipse Midpoint ellipse Two regions split where slope = -1; 4-way symmetry O(a + b)
Polygon fill Scanline Sorted edge crossings per row, fill pairs O(height + edges)
Region fill Flood fill Spread from a seed over 4 or 8 neighbors O(region area)
Triangle Edge functions Test pixels in the bounding box, barycentric weights O(box area)
Line clip Cohen-Sutherland 4-bit outcodes, accept, reject or clip repeatedly O(1) per edge
Line clip Liang-Barsky Parametric t interval from p and q values O(1)
Polygon clip Sutherland-Hodgman Clip against one window edge at a time O(vertices × edges)
Visibility Z-buffer Keep the nearest depth per pixel O(covered pixels)
Visibility Painter's Sort polygons far to near, draw in order O(n log n)

Line and circle formulas

Line from (x0, y0) to (x1, y1), with dx = x1 - x0 and dy = y1 - y0:

  • Slope: m = dy / dx. DDA steps = max(|dx|, |dy|); increments are dx / steps and dy / steps.
  • Bresenham for 0 ≤ m ≤ 1: start p = 2·dy - dx; if p < 0 plot (x+1, y) and p += 2·dy; else plot (x+1, y+1) and p += 2·dy - 2·dx.
  • Midpoint circle: start at (0, r) with p = 1 - r; each step first does x++; then if p < 0 use p += 2x + 1, else do y-- and use p += 2(x - y) + 1. Plot the eight symmetric points (±x, ±y), (±y, ±x).
  • Circle: x² + y² = r². Ellipse: x²/a² + y²/b² = 1.

Transformation matrices

2D transformations use 3x3 homogeneous matrices; 3D ones use 4x4. These are the 3D forms (drop the z row and column for 2D):

Transform Matrix
Translate (tx, ty, tz) identity with tx, ty, tz in the last column
Scale (sx, sy, sz) diagonal sx, sy, sz, 1
Rotate about z by θ rows: [cos θ, -sin θ, 0, 0], [sin θ, cos θ, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]
Rotate about x by θ rows: [1, 0, 0, 0], [0, cos θ, -sin θ, 0], [0, sin θ, cos θ, 0], [0, 0, 0, 1]
Rotate about y by θ rows: [cos θ, 0, sin θ, 0], [0, 1, 0, 0], [-sin θ, 0, cos θ, 0], [0, 0, 0, 1]
Reflect across the x axis (2D) diagonal 1, -1, 1
Shear in x by k (2D) identity with k in row 0, column 1

Rules to remember:

  • Composition is a matrix product; with column vectors the rightmost matrix acts first. T · R · p rotates, then translates.
  • Rotation about a pivot (px, py): T(px, py) · R(θ) · T(-px, -py).
  • The inverse of a rotation is its transpose; the inverse of a translation negates the offsets; the inverse of a scale uses reciprocals. Normals use the inverse transpose of the upper 3x3.
  • Window to viewport: xv = xvmin + (xw - xwmin) · sx with sx = (xvmax - xvmin) / (xwmax - xwmin), and the same for y. Unequal sx and sy stretch the picture.
  • Quaternion for a rotation of angle θ about unit axis a: q = (cos(θ/2), a · sin(θ/2)). Use slerp to interpolate.

Viewing and projection

flowchart LR
  A["Object space"] -->|"model"| B["World space"]
  B -->|"view"| C["Camera space"]
  C -->|"projection"| D["Clip space"]
  D -->|"divide by w"| E["NDC"]
  E -->|"viewport"| F["Screen pixels"]

The chain of spaces a vertex travels through: clip = P · V · M · p.

  • Look-at: f = normalize(target - eye), r = normalize(f × up), u = r × f. The view matrix rotates with rows r, u, -f and translates by -eye.
  • Orthographic: maps a box to the unit cube; size does not change with distance.
  • Perspective (OpenGL style, t = 1 / tan(fov / 2)): the diagonal holds t / aspect, t, (f + n) / (n - f), and the entries 2fn / (n - f) and -1 build depth and w. After this, w equals the distance in front of the camera.
  • Perspective divide: (x, y, z) = (xc / w, yc / w, zc / w). NDC runs from -1 to 1 (OpenGL) or 0 to 1 in z (Direct3D, Vulkan).
  • Screen position: sx = (x_ndc · 0.5 + 0.5) · width, sy = (1 - (y_ndc · 0.5 + 0.5)) · height (the flip because screen y points down).
  • Depth is non-linear: most precision sits close to the near plane, so keep the near plane as far out as you can.

This script follows one point through the chain with a 90 degree field of view, near plane 1, far plane 10 and an 800 by 600 screen.

JS One point through the pipeline

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Polygons, triangles and clipping

  • Edge function for P to Q: E(x, y) = (x - Px)(Qy - Py) - (y - Py)(Qx - Px). A pixel is inside when all three have the same sign.
  • Barycentric weights: w_i = E_i / (E_0 + E_1 + E_2). Interpolate any attribute as w0·a0 + w1·a1 + w2·a2.
  • Even-odd rule: odd crossing count means inside. Nonzero winding rule: inside when the winding number is not zero.
  • Cohen-Sutherland outcode bits: left, right, below, above. Accept when both codes are 0; reject when code1 & code2 != 0.
  • Liang-Barsky: p = (-dx, dx, -dy, dy) and q = (x0 - xmin, xmax - x0, y0 - ymin, ymax - y0). If p < 0 it enters (t0 = max(t0, q/p)), if p > 0 it leaves (t1 = min(t1, q/p)); if p = 0 and q < 0 the line is outside. Visible when t0 ≤ t1.
  • Sutherland-Hodgman cases per polygon edge: in to in (output the end), in to out (output the intersection), out to out (nothing), out to in (output the intersection and the end).

Color

Model Notes
RGB Additive; light on screens; each channel 0 to 255 in 8 bits
CMY / CMYK Subtractive; C = 1 - R and so on; K = black ink: K = min(C, M, Y)
HSV / HSL Hue (angle 0 to 360), saturation, value or lightness; intuitive for pickers
Gamma Display value ≈ linear^(1/2.2); do lighting and blending in linear space, store in sRGB
Lerp c = (1 - t)·c0 + t·c1

Lighting and shading

  • Lambert diffuse: Id = kd · max(0, N·L) · light.
  • Phong specular: Is = ks · max(0, R·V)^n, with R = 2(N·L)N - L. Blinn-Phong: max(0, N·H)^n with H = normalize(L + V).
  • Total: I = ka·Ia + Σ (kd·max(0, N·L) + ks·max(0, N·H)^n) · light / attenuation.
  • Attenuation: commonly 1 / (c + l·d + q·d²) for distance d.
  • Flat: one color per face. Gouraud: interpolate vertex colors. Phong shading: interpolate normals, light per pixel.
  • Texture lookup: (u, v) in [0, 1] maps to texel (u · width, v · height). Bilinear filtering blends four texels; mipmaps pick a pre-filtered level. Perspective-correct: interpolate u/w, v/w and 1/w, then divide.

Curves and fractals

Item Formula
Cubic Bezier B(t) = (1-t)³P0 + 3(1-t)²t P1 + 3(1-t)t² P2 + t³ P3
de Casteljau Repeated lerp of adjacent control points at t until one point remains
Catmull-Rom (uniform) 0.5 · [(2P1) + (P2-P0)t + (2P0-5P1+4P2-P3)t² + (3P1-P0-3P2+P3)t³] between P1 and P2
Continuity C0 same point; G1 same tangent direction; C1 same tangent vector
B-spline Local control: each point affects only a few spans; knot vector sets the parameter spacing
Mandelbrot z → z² + c, start z = 0; c is in the set if
Koch curve Each segment becomes four segments of one third the length; dimension ≈ 1.2619

Ray tracing

  • Camera ray: P(t) = O + t·D, with t > 0.
  • Sphere: a = D·D, b = 2·(O - C)·D, c = |O - C|² - r²; discriminant b² - 4ac; nearest root t = (-b - √disc) / (2a).
  • Plane with normal N through point Q: t = N·(Q - O) / (N·D); parallel when N·D = 0.
  • Shadow ray: from the hit point (moved along the normal by a tiny epsilon) toward the light; any hit before the light means shadow.
  • Reflection: R = D - 2(D·N)N. Refraction (Snell): n1 · sin θ1 = n2 · sin θ2; total internal reflection when the sine would exceed 1.
  • Recursion depth is limited (commonly 3 to 8) to stop infinite mirror bounces.

Pipeline stages

Stage Input Output
Vertex processing Model-space vertices Clip-space positions and varyings
Primitive assembly and clipping Clip-space triangles Triangles inside the view volume
Perspective divide and viewport Clip space Screen coordinates and depth
Rasterization Screen triangles Fragments with interpolated attributes
Fragment processing Fragments Shaded colors
Output merging Colors and depths Depth test, blending, framebuffer

Here is one more quick check, a triangle whose three corner colors are blended with barycentric weights, the formula behind most of the tables above.

Barycentric color blend

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In the chain clip = P * V * M * p, which matrix is applied to the point first?

    With column vectors the matrix closest to the point acts first, so the model matrix places the object in the world before the view and projection.
  2. 2. What does the perspective divide do?

    After projection the vertex is (x, y, z, w); dividing by w makes distant points smaller and yields NDC.
  3. 3. A Liang-Barsky edge with p = 0 and q < 0 means...

    p = 0 means the line is parallel to the boundary, and q < 0 means it lies on the outside of it, so no part can be visible.
  4. 4. Which formula gives the mirror reflection direction of an incoming ray D at a surface with unit normal N?

    Subtracting twice the component of D along N flips that component and keeps the part parallel to the surface.

Summary

  • Lines, circles and fills are incremental algorithms that favor integer arithmetic and symmetry.
  • Every transform is a matrix; order matters, and rotation about a pivot is translate, rotate, translate back.
  • A vertex travels model, view, projection, divide by w, viewport; depth is non-linear.
  • Barycentric weights interpolate everything across a triangle; the z-buffer keeps the nearest fragment.
  • Lighting is ambient plus diffuse plus specular, computed per face, vertex or pixel.
  • Rasterization loops over triangles, ray tracing over pixels.

You have reached the end of the track. To revise, return to the Computer Graphics Interview Questions or rebuild a renderer from the first lesson.