Master the fundamental concepts of software rasterizer through this focused micro-challenge.
In 1962, Jack Bresenham developed an algorithm to draw straight lines on a pixel grid. Given endpoints (x0, y0) and (x1, y1), which pixels best approximate the ideal line?
The naive approach uses y = mx + b, computing and rounding y for each integer x. That requires floating-point multiply and divide on every pixel.
Bresenham's insight: track an integer error term instead of computing y directly. For slopes between 0 and 1:
cLoading…
For example, drawing from (0,0) to (7,3) uses only integer addition and subtraction. Other octants swap x/y or reverse step direction, but the error-term logic stays the same.
You will implement Bresenham's algorithm for a line with slope between 0 and 1, printing each plotted pixel coordinate. This task asks you to output the full pixel list for endpoints read from stdin. The integer-only path you write here is still used inside Cairo, Skia's software backend, and embedded displays.
Implement Bresenham's line algorithm for all eight octants, using integer arithmetic only: no floats, no division, no slope. Rasterize lines onto an ASCII canvas and list the pixels each line covers, including lines that run off the canvas (those pixels are listed and counted, but not drawn).
# starts a comment line.
cLoading…
Use the single-error-term form, so every implementation produces the same pixels:
cLoading…
cLoading…
... when there are more.N pixel(s), plus , K outside the canvas when some fall off.# for drawn pixels and . for the rest.canvas: W (1-64) H (1-32), line: X0 Y0 X1 Y1 (|v| <= 1000), unknown command X.Input:
cLoading…
Output:
cLoading…
Hidden tests cover steep and shallow lines in every direction, vertical, horizontal and 45° lines, a single-point line, lines crossing the canvas edge, and a resized canvas.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works