Encodes a boundary as a sequence of 8-directional steps. A compact representation used as input to higher-level shape analysis and simplification.
A chain code traces a pixel boundary by recording the direction of each step rather than absolute coordinates. With an 8-connected neighbourhood, only 3 bits per step are needed — far more compact than storing (x, y) pairs.
Starting from a known pixel, follow the boundary clockwise. At each step record the direction to the next boundary pixel using the numbering below, then advance. The resulting sequence of digits is the chain code. The starting point and a single (x, y) anchor are sufficient to reconstruct the full outline.
Draw a filled shape on the pixel grid. The algorithm traces its boundary and generates the chain code below. Each coloured step in the code corresponds to a highlighted segment on the boundary.
Click or drag to paint pixels. Press Trace to run the algorithm.
Watch the tracer advance one step at a time around the boundary. The amber cell is the current position; the arrow shows the direction about to be appended to the code.