liveBook · Manning

Bresenham's line algorithm

As explained in Clojure: The Essential Reference

Bresenham's line algorithm is a rasterization algorithm that selects discrete pixel positions approximating a straight line between two endpoints.

Bresenham's line algorithm rasterizes a straight line by selecting integer-coordinate pixels that approximate its slope. It is useful when a digital canvas stores pixels and coordinates as integers, especially for oblique lines. section 5.14

A Clojure implementation organizes the work into these stages:

  • Use unchecked integer operators when integer indexing, reduced boxing, and performance are important; a long-based variant is also shown. section 5.14

Drawing a line

Calling (to-points 2 3 10 10) and painting those points on a 12-by-12 canvas produces a stepped diagonal of 1s from approximately [2,3] to [10,10]. Repeated neighboring pixels make the rasterized line look straight at larger scale.

How Clojure: The Essential Reference covers it

How deep it goes
Details slope adjustment, endpoint ordering, error accumulation, pixel painting, unchecked arithmetic, and a long-based variant.
Examples
Demonstrates a line from [2,3] to [10,10] rendered as 1-valued pixels on a 12-by-12 canvas.
What it assumes
Assumes basic Clojure functions, loops, arrays, and integer arithmetic while motivating unchecked operators through a concrete image task.
What you can do after
Build it from scratch using coordinate helpers, an error-driven loop, and a canvas-painting function.
How it connects
Connects rasterization with integer arithmetic, multidimensional arrays, and performance benchmarking.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.