s LearnSqueak.com

SMALLTALK LAB

Simplify a path with Douglas–Peucker

Keep the meaningful bends of an open polyline with a distance tolerance.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Workspace in native Squeak and use Print it on one complete checkpoint at a time.

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 display equivalent Smalltalk literals with different spaces or delimiters.

01 - Measure to the nearest point on a segment

Compute the projection fraction using a dot product. Clamp it to 0–1 so a point past an end is compared to that endpoint, not to the infinite line. A segment with equal endpoints reduces to a point.

Independent Workspace expression - select all, then Print it
| dist2 |
dist2 := [:p :a :b |
 | dx dy px py len t rx ry |
 dx := b x - a x. dy := b y - a y.
 px := p x - a x. py := p y - a y.
 len := dx squared + dy squared.
 t := len = 0 ifTrue: [0] ifFalse: [
  ((((px * dx) + (py * dy)) / len) max: 0) min: 1].
 rx := px - (t * dx). ry := py - (t * dy).
 rx squared + ry squared].
{dist2 value: 3@1 value: 0@0 value: 2@0.
 dist2 value: 3@4 value: 0@0 value: 0@0}

Expected: #(2 25)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Measure to the nearest point on a segment
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The first squared distance is 2 because the nearest point is 2@0.

Download this checkpoint

02 - Find the bend that breaks the chord

The chord from the first point to the last lies along y = 0. The apex has squared distance 4, so it is 2 units away.

Independent Workspace expression - select all, then Print it
| dist2 path |
dist2 := [:p :a :b |
 | dx dy px py len t rx ry |
 dx := b x - a x. dy := b y - a y.
 px := p x - a x. py := p y - a y.
 len := dx squared + dy squared.
 t := len = 0 ifTrue: [0] ifFalse: [
  ((((px * dx) + (py * dy)) / len) max: 0) min: 1].
 rx := px - (t * dx). ry := py - (t * dy).
 rx squared + ry squared].
path := {0@0. 1@0. 2@2. 3@0. 4@0}.
(2 to: 4) collect: [:i |
 dist2 value: (path at: i) value: path first value: path last]

Expected: #(0 4 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Find the bend that breaks the chord
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Raise the apex by one. Its squared distance becomes 9.

Download this checkpoint

03 - Split at the farthest point

With a tolerance of 1, keep the apex and repeat the test on each side. Both remaining intermediate points are less than one unit from their shorter chords.

Independent Workspace expression - select all, then Print it
| dist2 reduce |
dist2 := [:p :a :b |
 | dx dy px py len t rx ry |
 dx := b x - a x. dy := b y - a y.
 px := p x - a x. py := p y - a y.
 len := dx squared + dy squared.
 t := len = 0 ifTrue: [0] ifFalse: [
  ((((px * dx) + (py * dy)) / len) max: 0) min: 1].
 rx := px - (t * dx). ry := py - (t * dy).
 rx squared + ry squared].
reduce := [:points :epsilon |
 | keep visit |
 epsilon < 0 ifTrue: [Error signal: 'Use a nonnegative tolerance'].
 keep := OrderedCollection new.
 visit := [:lo :hi |
  | a b worst split d |
  a := points at: lo. b := points at: hi.
  worst := epsilon squared. split := nil.
  lo + 1 to: hi - 1 do: [:i |
   d := dist2 value: (points at: i) value: a value: b.
   d > worst ifTrue: [worst := d. split := i]].
  split ifNil: [keep add: b] ifNotNil: [
   visit value: lo value: split.
   visit value: split value: hi]].
 points isEmpty ifFalse: [
  keep add: points first.
  points size > 1 ifTrue: [visit value: 1 value: points size]].
 keep asArray].
reduce value: {0@0. 1@0. 2@2. 3@0. 4@0} value: 1

Expected: {0@0. 2@2. 4@0}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Split at the farthest point
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Notice how the shared apex appears just once; the left branch emits it and the right branch emits its own end.

Download this checkpoint

04 - Compare three tolerances

At half a unit all the vertices survive. At two units even the apex is within tolerance. A strict greater-than test treats equality as acceptable.

Independent Workspace expression - select all, then Print it
| dist2 reduce path |
dist2 := [:p :a :b |
 | dx dy px py len t rx ry |
 dx := b x - a x. dy := b y - a y.
 px := p x - a x. py := p y - a y.
 len := dx squared + dy squared.
 t := len = 0 ifTrue: [0] ifFalse: [
  ((((px * dx) + (py * dy)) / len) max: 0) min: 1].
 rx := px - (t * dx). ry := py - (t * dy).
 rx squared + ry squared].
reduce := [:points :epsilon |
 | keep visit |
 epsilon < 0 ifTrue: [Error signal: 'Use a nonnegative tolerance'].
 keep := OrderedCollection new.
 visit := [:lo :hi |
  | a b worst split d |
  a := points at: lo. b := points at: hi.
  worst := epsilon squared. split := nil.
  lo + 1 to: hi - 1 do: [:i |
   d := dist2 value: (points at: i) value: a value: b.
   d > worst ifTrue: [worst := d. split := i]].
  split ifNil: [keep add: b] ifNotNil: [
   visit value: lo value: split.
   visit value: split value: hi]].
 points isEmpty ifFalse: [
  keep add: points first.
  points size > 1 ifTrue: [visit value: 1 value: points size]].
 keep asArray].
path := {0@0. 1@0. 2@2. 3@0. 4@0}.
#(0.5 1 2) collect: [:eps | (reduce value: path value: eps) size]

Expected: #(5 3 2)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Compare three tolerances
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Predict the count at a tolerance of zero for this particular zigzag.

Download this checkpoint

05 - Handle very short input

Avoid reading nonexistent endpoints for an empty path. Return the one supplied point for a singleton, and reject a negative tolerance.

Independent Workspace expression - select all, then Print it
| dist2 reduce |
dist2 := [:p :a :b |
 | dx dy px py len t rx ry |
 dx := b x - a x. dy := b y - a y.
 px := p x - a x. py := p y - a y.
 len := dx squared + dy squared.
 t := len = 0 ifTrue: [0] ifFalse: [
  ((((px * dx) + (py * dy)) / len) max: 0) min: 1].
 rx := px - (t * dx). ry := py - (t * dy).
 rx squared + ry squared].
reduce := [:points :epsilon |
 | keep visit |
 epsilon < 0 ifTrue: [Error signal: 'Use a nonnegative tolerance'].
 keep := OrderedCollection new.
 visit := [:lo :hi |
  | a b worst split d |
  a := points at: lo. b := points at: hi.
  worst := epsilon squared. split := nil.
  lo + 1 to: hi - 1 do: [:i |
   d := dist2 value: (points at: i) value: a value: b.
   d > worst ifTrue: [worst := d. split := i]].
  split ifNil: [keep add: b] ifNotNil: [
   visit value: lo value: split.
   visit value: split value: hi]].
 points isEmpty ifFalse: [
  keep add: points first.
  points size > 1 ifTrue: [visit value: 1 value: points size]].
 keep asArray].
{reduce value: #() value: 1.
 reduce value: {3@4} value: 1.
 [reduce value: {3@4} value: -1] on: Error do: [:ex | #rejected]}

Expected: {#(). {3@4}. #rejected}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Handle very short input
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Pass two points. Both endpoints remain.

Download this checkpoint

06 - Remove collinear waypoints at zero tolerance

Points on a straight chord have exactly zero deviation. Zero tolerance forbids changing the line, not removing waypoints that lie on it.

Independent Workspace expression - select all, then Print it
| dist2 reduce |
dist2 := [:p :a :b |
 | dx dy px py len t rx ry |
 dx := b x - a x. dy := b y - a y.
 px := p x - a x. py := p y - a y.
 len := dx squared + dy squared.
 t := len = 0 ifTrue: [0] ifFalse: [
  ((((px * dx) + (py * dy)) / len) max: 0) min: 1].
 rx := px - (t * dx). ry := py - (t * dy).
 rx squared + ry squared].
reduce := [:points :epsilon |
 | keep visit |
 epsilon < 0 ifTrue: [Error signal: 'Use a nonnegative tolerance'].
 keep := OrderedCollection new.
 visit := [:lo :hi |
  | a b worst split d |
  a := points at: lo. b := points at: hi.
  worst := epsilon squared. split := nil.
  lo + 1 to: hi - 1 do: [:i |
   d := dist2 value: (points at: i) value: a value: b.
   d > worst ifTrue: [worst := d. split := i]].
  split ifNil: [keep add: b] ifNotNil: [
   visit value: lo value: split.
   visit value: split value: hi]].
 points isEmpty ifFalse: [
  keep add: points first.
  points size > 1 ifTrue: [visit value: 1 value: points size]].
 keep asArray].
reduce value: {0@0. 1@1. 2@2. 3@3} value: 0

Expected: {0@0. 3@3}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Remove collinear waypoints at zero tolerance
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Move the middle point off the diagonal, then run the same threshold.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Simplify a path with Douglas–Peucker native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

Use planar coordinates and nonnegative tolerance in the same units. Project geographic data first for a suitable local scale.

This open-path simplifier does not preserve polygon topology, prevent new intersections, or promise the fewest possible vertices.

Each split scans its interior again. Unbalanced splits can take quadratic time and linear recursion depth.

Three practice challenges

Zero tolerance

Does zero always keep every vertex?

Show solution

No. A vertex lying exactly on the segment can be dropped because its deviation is zero.

Exact tie

What if every interior point lies exactly at the tolerance?

Show solution

Keep only the endpoints of that segment; none exceeds the tolerance.

Change units

If every coordinate doubles, how do you preserve selection?

Show solution

Double the tolerance. Geometric distances also double, or multiply by four in squared form.

Reference

Douglas and Peucker’s original paper

Continue learning

Point In Polygon | First Point

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.