SMALLTALK LAB
Bresenham Lines in Squeak: Draw with Integer Steps
Turn a mathematical line into a sequence of grid cells. Walk the error term, then test horizontal, steep and reflected lines.
Before you begin
Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.
01 - Start with two grid cells
A grid point names a pixel center. The endpoints may be far apart, but each following pixel must be a neighbor of the previous one.
{0@0. 5@2}Expected: {0@0. 5@2}

Try it: Count the steps across and up separately.
02 - Measure integer displacement
Magnitudes control the error term; signs control direction. This separation lets the same rule work in reflected quadrants.
| target |
target := -5@2.
{target x abs. target y abs. target x sign. target y sign}Expected: #(5 2 -1 1)

Try it: For 5@-2 the magnitudes remain the same and signs reverse.
03 - Decide the first move
Use a negative vertical magnitude so the two comparisons share one error term. Read twice once, before either branch updates err.
| dx dy err twice |
dx := 5. dy := -2. err := dx + dy.
twice := 2 * err.
{twice. twice >= dy. twice <= dx}Expected: #(6 true false)

Try it: Both comparisons can pass in a single iteration; the branches must not be mutually exclusive.
04 - Walk a shallow line
For each pixel, record it and check for the target before changing either coordinate. Capture the doubled error once and allow both axes to advance when needed.
| pixels |
pixels := [:target | | x y dx dy sx sy err out |
x := 0. y := 0.
dx := target x abs. dy := target y abs negated.
sx := target x sign. sy := target y sign.
err := dx + dy.
out := OrderedCollection new.
[out add: x@y. (x@y) = target] whileFalse: [ | twice |
twice := 2 * err.
twice >= dy ifTrue: [err := err + dy. x := x + sx].
twice <= dx ifTrue: [err := err + dx. y := y + sy]].
out asArray].
pixels value: 5@2Expected: {0@0. 1@0. 2@1. 3@1. 4@2. 5@2}

Try it: Compare the second and third points: the decision changes from horizontal to diagonal.
05 - Check a steep reflected line
A larger vertical magnitude means y advances more often. The negative horizontal sign sends x left without changing the comparisons.
| pixels |
pixels := [:target | | x y dx dy sx sy err out |
x := 0. y := 0.
dx := target x abs. dy := target y abs negated.
sx := target x sign. sy := target y sign.
err := dx + dy.
out := OrderedCollection new.
[out add: x@y. (x@y) = target] whileFalse: [ | twice |
twice := 2 * err.
twice >= dy ifTrue: [err := err + dy. x := x + sx].
twice <= dx ifTrue: [err := err + dx. y := y + sy]].
out asArray].
pixels value: -2@5Expected: {0@0. 0@1. -1@2. -1@3. -2@4. -2@5}

Try it: The sequence still has six points, including both endpoints.
06 - Test axis-aligned and zero-length lines
The same exit condition covers the single-cell case. A coordinate whose delta is zero never needs to change.
| pixels |
pixels := [:target | | x y dx dy sx sy err out |
x := 0. y := 0.
dx := target x abs. dy := target y abs negated.
sx := target x sign. sy := target y sign.
err := dx + dy.
out := OrderedCollection new.
[out add: x@y. (x@y) = target] whileFalse: [ | twice |
twice := 2 * err.
twice >= dy ifTrue: [err := err + dy. x := x + sx].
twice <= dx ifTrue: [err := err + dx. y := y + sy]].
out asArray].
{pixels value: 3@0. pixels value: 0@-2. pixels value: 0@0}Expected: {{0@0. 1@0. 2@0. 3@0}. {0@0. 0@-1. 0@-2}. {0@0}}

Try it: A display may invert Y relative to math coordinates. Invert only at the display boundary.
Replay the checkpoints

Download the native grid demo. Do it in Workspace. Close the window, change shown := 1. to a number from 2 to 6, and run again.
Reason about correctness and limits
This is a one-pixel-wide raster trace, not antialiasing or a list of all intersected cells. This tie rule may choose different interior pixels when traversed in reverse.
For arbitrary endpoints, subtract the start to obtain the target displacement, then add the start to each returned Point. Cost is linear in the larger displacement.
Three practice challenges
Translate the path
Shift a line from 0@0→3@0 to start at 4@2.
Show solution
Add 4@2 to each point: 4@2, 5@2, 6@2, 7@2.
Count output cells
How many points does 2@7 produce from the origin?
Show solution
Eight, because the largest absolute displacement is seven and both endpoints are included.
Check neighboring points
How can you detect an illegal jump?
Show solution
For adjacent points, subtract them. Neither coordinate of the difference should have absolute value greater than one.
Continue learning
First Point | Ray Casting | Bezier Curves
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.