SMALLTALK LAB
Find whether a point is inside a polygon
Cast a ray through a polygon and return inside, outside, or boundary.
Before you begin
Open a Workspace in native Squeak. Select one complete checkpoint and choose Print it.
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 - Decide whether an edge spans the ray
For a horizontal ray at y = 2, the edge straddles the ray if one endpoint is strictly above it and the other is not.
| u v y |
u := 1@1. v := 1@4. y := 2.
(u y > y) ~= (v y > y)Expected: true

Try it: Set y to 4. The result becomes false.
02 - Find the crossing coordinate
Interpolate along the edge. The resulting x is exact because Squeak keeps this division as a Fraction.
| u v y |
u := 1@1. v := 3@5. y := 2.
u x + ((y - u y) * (v x - u x) / (v y - u y))Expected: (3/2)

Try it: Change y to 3; the crossing becomes 2.
03 - Skip a horizontal edge
Both endpoints are on the ray height. Do not divide by their zero height difference; use the boundary test separately.
| u v y |
u := 0@0. v := 4@0. y := 0.
(u y > y) ~= (v y > y)Expected: false

Try it: Move the ray up to y = 1; the horizontal edge still cannot straddle it.
04 - Classify a concave shape
Toggle parity only when a crossing lies to the right of the point. The lower-case results distinguish the L-shaped interior from its missing corner.
| classify polygon |
classify := [:pt :vertices |
| odd edge n |
odd := false. edge := false. n := vertices size.
n < 3 ifTrue: [Error signal: 'Need at least three vertices'].
1 to: n do: [:i | | u v cross spans xHit |
u := vertices at: i.
v := vertices at: (i = n ifTrue: [1] ifFalse: [i + 1]).
cross := ((v x - u x) * (pt y - u y))
- ((v y - u y) * (pt x - u x)).
(cross = 0
and: [(pt x between: (u x min: v x) and: (u x max: v x))
and: [pt y between: (u y min: v y) and: (u y max: v y)]])
ifTrue: [edge := true].
spans := (u y > pt y) ~= (v y > pt y).
spans ifTrue: [
xHit := u x + ((pt y - u y) * (v x - u x) / (v y - u y)).
pt x < xHit ifTrue: [odd := odd not]]].
edge ifTrue: [#boundary]
ifFalse: [odd ifTrue: [#inside] ifFalse: [#outside]]].
polygon := {0@0. 4@0. 4@1. 1@1. 1@4. 0@4}.
{classify value: ((1/2)@2) value: polygon.
classify value: (3@3) value: polygon}Expected: #(inside outside)

Try it: Try 2@0; the point lies on the bottom edge.
05 - Keep the edge separate
A zero cross product says the point lies on the supporting line. Both bounding comparisons are needed to decide whether it is on the finite edge.
| classify polygon |
classify := [:pt :vertices |
| odd edge n |
odd := false. edge := false. n := vertices size.
n < 3 ifTrue: [Error signal: 'Need at least three vertices'].
1 to: n do: [:i | | u v cross spans xHit |
u := vertices at: i.
v := vertices at: (i = n ifTrue: [1] ifFalse: [i + 1]).
cross := ((v x - u x) * (pt y - u y))
- ((v y - u y) * (pt x - u x)).
(cross = 0
and: [(pt x between: (u x min: v x) and: (u x max: v x))
and: [pt y between: (u y min: v y) and: (u y max: v y)]])
ifTrue: [edge := true].
spans := (u y > pt y) ~= (v y > pt y).
spans ifTrue: [
xHit := u x + ((pt y - u y) * (v x - u x) / (v y - u y)).
pt x < xHit ifTrue: [odd := odd not]]].
edge ifTrue: [#boundary]
ifFalse: [odd ifTrue: [#inside] ifFalse: [#outside]]].
polygon := {0@0. 4@0. 4@1. 1@1. 1@4. 0@4}.
{classify value: (1@2) value: polygon.
classify value: (4@1) value: polygon.
classify value: (5@0) value: polygon}Expected: #(boundary boundary outside)

Try it: Test the corner 0@0. It is boundary.
06 - Reverse the vertex order
The segment set is unchanged when traversed in reverse. This parity test does not depend on clockwise order.
| classify polygon |
classify := [:pt :vertices |
| odd edge n |
odd := false. edge := false. n := vertices size.
n < 3 ifTrue: [Error signal: 'Need at least three vertices'].
1 to: n do: [:i | | u v cross spans xHit |
u := vertices at: i.
v := vertices at: (i = n ifTrue: [1] ifFalse: [i + 1]).
cross := ((v x - u x) * (pt y - u y))
- ((v y - u y) * (pt x - u x)).
(cross = 0
and: [(pt x between: (u x min: v x) and: (u x max: v x))
and: [pt y between: (u y min: v y) and: (u y max: v y)]])
ifTrue: [edge := true].
spans := (u y > pt y) ~= (v y > pt y).
spans ifTrue: [
xHit := u x + ((pt y - u y) * (v x - u x) / (v y - u y)).
pt x < xHit ifTrue: [odd := odd not]]].
edge ifTrue: [#boundary]
ifFalse: [odd ifTrue: [#inside] ifFalse: [#outside]]].
polygon := {0@0. 4@0. 4@1. 1@1. 1@4. 0@4}.
classify value: ((1/2)@2) value: polygon reversedExpected: #inside

Try it: Try reversing the vertices for an outside point too.
Replay the checkpoints

Reason about correctness and limits
Use a simple polygon with at least three vertices and exact integer or Fraction coordinates. This example does not validate self-intersection or add a floating-point tolerance.
Each edge is visited once. The half-open vertical test counts shared vertices consistently without dividing on horizontal edges.
Three practice challenges
Predict one intermediate value
Change y to 3; the crossing becomes 2.
Show solution
At y = 3, the ray meets this edge halfway at x = 2.
Work one small variation
Try 2@0; the point lies on the bottom edge.
Show solution
2@0 is on the bottom line segment; report #boundary, not #inside.
Check the edge of the model
Why is testing only the cross product insufficient for boundary detection?
Show solution
The infinite supporting line also contains points beyond either endpoint; bounds exclude them.
Continue learning
Polygon Area | Convex Hull | First Point
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.