s LearnSqueak.com

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.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

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.

Independent Workspace expression - select all, then Print it
{0@0. 5@2}

Expected: {0@0. 5@2}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Start with two grid cells
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Count the steps across and up separately.

Download this checkpoint

02 - Measure integer displacement

Magnitudes control the error term; signs control direction. This separation lets the same rule work in reflected quadrants.

Independent Workspace expression - select all, then Print it
| target |
target := -5@2.
{target x abs. target y abs. target x sign. target y sign}

Expected: #(5 2 -1 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Measure integer displacement
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: For 5@-2 the magnitudes remain the same and signs reverse.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| dx dy err twice |
dx := 5. dy := -2. err := dx + dy.
twice := 2 * err.
{twice. twice >= dy. twice <= dx}

Expected: #(6 true false)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Decide the first move
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Both comparisons can pass in a single iteration; the branches must not be mutually exclusive.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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@2

Expected: {0@0. 1@0. 2@1. 3@1. 4@2. 5@2}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Walk a shallow line
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Compare the second and third points: the decision changes from horizontal to diagonal.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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@5

Expected: {0@0. 0@1. -1@2. -1@3. -2@4. -2@5}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Check a steep reflected line
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The sequence still has six points, including both endpoints.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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}}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Test axis-aligned and zero-length lines
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

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

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Squeak Morphic 6-by-3 grid showing the first raster cell at 0@0 in yellow
Native Morphic replay of the calculated cell path. Yellow marks the newest cell and green marks earlier cells.

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.