SMALLTALK LAB
Convex hull: wrap a cloud of points
Use an orientation test and a monotone chain to find the outer corners of a point set, with exact integer geometry and a native visual trace.
Before you begin
Know Point coordinates, sorting, collections and blocks. A convex hull is the smallest convex boundary containing all input points. This lesson keeps only extreme corners and discards collinear points along straight edges.
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.
01 - Read a point as two coordinates
A Point is a pair of numbers, not a screen pixel by itself. We use mathematical y-up geometry for orientation; the later visual converts to Squeak’s screen y-down coordinates.
| p |
p := 4@3.
{p x. p y}Expected: {4. 3.}

Try it: Try a negative coordinate; the arithmetic still works.
02 - Classify a turn
The two-dimensional cross product is positive for a left turn, negative for a right turn and zero for collinear points. Integer input keeps this decision exact.
| turn |
turn := [:a :b :c |
((b x-a x)*(c y-a y))-((b y-a y)*(c x-a x))].
{turn value: 0@0 value: 4@0 value: 4@3.
turn value: 0@0 value: 4@0 value: 2@0}Expected: {12. 0.}

Try it: Swap the last two points and observe the sign change.
03 - Sort and remove duplicates
The lower boundary is built from left to right. Sort first by x, then y. A Set removes duplicate Points so repeated input does not create duplicate corners.
| points |
points := {0@0. 4@0. 4@3. 0@3. 2@1. 1@2. 4@0.}.
points asSet asArray sorted: [:a :b |
a x = b x ifTrue: [a y <= b y] ifFalse: [a x < b x]]Expected: {0@0. 0@3. 1@2. 2@1. 4@0. 4@3.}

Try it: Why do we need the y tie-breaker for the two points with x=0?
04 - Remove a corner that bends inward
Treat the chain as a stack. If the new point creates a right turn or a straight continuation, remove the old last point and try again. The final two points form the correct lower edge.
| turn chain p |
turn := [:a :b :c |
((b x-a x)*(c y-a y))-((b y-a y)*(c x-a x))].
chain := OrderedCollection with: 0@0 with: 0@3. p := 1@2.
[chain size >= 2 and: [
(turn value: (chain at: chain size-1) value: chain last value: p) <= 0]]
whileTrue: [chain removeLast].
chain add: p. chain asArrayExpected: {0@0. 1@2.}

Try it: The short-circuit and: protects the second-last index when the chain is small.
05 - Build lower and upper chains
Apply the same rule in opposite traversal directions. Remove each chain’s final endpoint before joining, so the leftmost and rightmost points occur once. The hull does not repeat its first point at the end.
| turn hull |
turn := [:a :b :c |
((b x-a x)*(c y-a y))-((b y-a y)*(c x-a x))].
hull := [:input | | sorted lower upper append |
sorted := input asSet asArray sorted: [:a :b |
a x = b x ifTrue: [a y <= b y] ifFalse: [a x < b x]].
lower := OrderedCollection new. upper := OrderedCollection new.
append := [:chain :p |
[chain size >= 2 and: [
(turn value: (chain at: chain size-1) value: chain last value: p) <= 0]]
whileTrue: [chain removeLast].
chain add: p].
sorted do: [:p | append value: lower value: p].
sorted reversed do: [:p | append value: upper value: p].
sorted size > 1 ifTrue: [lower removeLast. upper removeLast].
sorted size <= 1
ifTrue: [sorted]
ifFalse: [(lower,upper) asArray]].
hull value: {0@0. 4@0. 4@3. 0@3. 2@1. 1@2. 4@0.}Expected: {0@0. 4@0. 4@3. 0@3.}

Try it: The two interior points and duplicate are absent from the result.
06 - Define degenerate results
Empty input has an empty hull; one unique point stays one point. Collinear inputs reduce to their two extreme endpoints under our non-left-turn removal rule.
| turn hull |
turn := [:a :b :c |
((b x-a x)*(c y-a y))-((b y-a y)*(c x-a x))].
hull := [:input | | sorted lower upper append |
sorted := input asSet asArray sorted: [:a :b |
a x = b x ifTrue: [a y <= b y] ifFalse: [a x < b x]].
lower := OrderedCollection new. upper := OrderedCollection new.
append := [:chain :p |
[chain size >= 2 and: [
(turn value: (chain at: chain size-1) value: chain last value: p) <= 0]]
whileTrue: [chain removeLast].
chain add: p].
sorted do: [:p | append value: lower value: p].
sorted reversed do: [:p | append value: upper value: p].
sorted size > 1 ifTrue: [lower removeLast. upper removeLast].
sorted size <= 1
ifTrue: [sorted]
ifFalse: [(lower,upper) asArray]].
{hull value: #(). hull value: {2@2. 2@2.}.
hull value: {3@0. 1@0. 2@0.}}Expected: {#(). {2@2.}. {1@0. 3@0.}.}

Try it: A segment is a valid degenerate hull, but it has no enclosed area.
07 - Check every boundary turn
A nondegenerate result should turn left at every corner, including the wrap-around corners. This test checks cyclic indexing without duplicating the first point.
| turn hull h |
turn := [:a :b :c |
((b x-a x)*(c y-a y))-((b y-a y)*(c x-a x))].
hull := [:input | | sorted lower upper append |
sorted := input asSet asArray sorted: [:a :b |
a x = b x ifTrue: [a y <= b y] ifFalse: [a x < b x]].
lower := OrderedCollection new. upper := OrderedCollection new.
append := [:chain :p |
[chain size >= 2 and: [
(turn value: (chain at: chain size-1) value: chain last value: p) <= 0]]
whileTrue: [chain removeLast].
chain add: p].
sorted do: [:p | append value: lower value: p].
sorted reversed do: [:p | append value: upper value: p].
sorted size > 1 ifTrue: [lower removeLast. upper removeLast].
sorted size <= 1
ifTrue: [sorted]
ifFalse: [(lower,upper) asArray]].
h := hull value: {0@0. 4@0. 4@3. 0@3. 2@1. 1@2. 4@0.}.
(1 to: h size) allSatisfy: [:i |
(turn value: (h at: i) value: (h at: (i \\ h size)+1)
value: (h at: ((i+1) \\ h size)+1)) > 0]Expected: true

Try it: Do not apply this positive-turn assertion to a one-point or two-point hull.
08 - Test a larger grid
All sixteen integer grid points from zero through three fit inside a square. The hull should return exactly four corners, independent of enumeration order and interior points.
| turn hull grid |
turn := [:a :b :c |
((b x-a x)*(c y-a y))-((b y-a y)*(c x-a x))].
hull := [:input | | sorted lower upper append |
sorted := input asSet asArray sorted: [:a :b |
a x = b x ifTrue: [a y <= b y] ifFalse: [a x < b x]].
lower := OrderedCollection new. upper := OrderedCollection new.
append := [:chain :p |
[chain size >= 2 and: [
(turn value: (chain at: chain size-1) value: chain last value: p) <= 0]]
whileTrue: [chain removeLast].
chain add: p].
sorted do: [:p | append value: lower value: p].
sorted reversed do: [:p | append value: upper value: p].
sorted size > 1 ifTrue: [lower removeLast. upper removeLast].
sorted size <= 1
ifTrue: [sorted]
ifFalse: [(lower,upper) asArray]].
grid := OrderedCollection new.
0 to: 3 do: [:x | 0 to: 3 do: [:y | grid add: x@y]].
(hull value: grid) = {0@0. 3@0. 3@3. 0@3.}Expected: true

Try it: Expand the grid and verify that the boundary still contains just four extreme corners.
Watch the computed states

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.
Reason about correctness and limits
Sorting costs O(n log n). Each point is pushed and popped at most once per chain, so the scan after sorting is linear. Storage is O(n).
This variant discards collinear edge points. Replacing <= 0 by < 0 changes that policy but also requires care with fully collinear input and repeated endpoints.
Integer coordinates make the orientation arithmetic exact in Squeak. With measured Float coordinates, near-collinear points need a deliberately chosen robust geometric predicate; an arbitrary epsilon is not a universal solution.
Three practice challenges
Challenge 1
Why traverse in reverse for the upper hull?
Show solution
The same left-turn invariant then wraps the other side of the point set.
Challenge 2
Why remove both final endpoints before joining?
Show solution
Each extreme endpoint belongs to both chains; removing one occurrence from each prevents duplicates.
Challenge 3
How can you draw the closed boundary?
Show solution
Connect successive hull points and then connect the last point back to the first, without changing the returned data.
Continue learning
Polygon Area | First Point | Sorting Visualiser
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.