s LearnSqueak.com

SMALLTALK LAB

Find whether a point is inside a polygon

Cast a ray through a polygon and return inside, outside, or boundary.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

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.

Independent Workspace expression - select all, then Print it
| u v y |
u := 1@1. v := 1@4. y := 2.
(u y > y) ~= (v y > y)

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Decide whether an edge spans the ray
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Set y to 4. The result becomes false.

Download this checkpoint

02 - Find the crossing coordinate

Interpolate along the edge. The resulting x is exact because Squeak keeps this division as a Fraction.

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

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

Try it: Change y to 3; the crossing becomes 2.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| u v y |
u := 0@0. v := 4@0. y := 0.
(u y > y) ~= (v y > y)

Expected: false

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Skip a horizontal edge
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Move the ray up to y = 1; the horizontal edge still cannot straddle it.

Download this checkpoint

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.

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

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

Try it: Try 2@0; the point lies on the bottom edge.

Download this checkpoint

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.

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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Keep the edge separate
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Test the corner 0@0. It is boundary.

Download this checkpoint

06 - Reverse the vertex order

The segment set is unchanged when traversed in reverse. This parity test does not depend on clockwise order.

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

Expected: #inside

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Reverse the vertex order
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

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

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Find whether a point is inside a polygon 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 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.