SMALLTALK LAB
Polygon area: walk the boundary with the shoelace formula
Measure a polygon in Squeak using oriented edge contributions, exact fractions and tests for winding, translation and scaling.
Before you begin
Use Points, Arrays and a two-argument block. Coordinates below use mathematical x-right, y-up axes. Supply a simple polygon with its vertices in boundary order, without crossing edges. This lesson calculates signed area; it does not validate that a polygon is simple.
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 - Describe a boundary
Each Point is one corner. The final edge returns to the first corner automatically. An unordered collection of corners does not describe the same boundary.
| corners |
corners := {0@0. 4@0. 4@3. 0@3}.
corners sizeExpected: 4

Try it: Change the rectangle width from 4 to 6. The number of corners remains four.
02 - Measure one oriented edge
The cross term x1*y2 - y1*x2 measures a signed double-area contribution. Keep the parentheses: Smalltalk binary messages have equal precedence.
| a b |
a := 4@0. b := 4@3.
(a x * b y) - (a y * b x)Expected: 12

Try it: Reverse a and b; the contribution becomes -12.
03 - Close the last edge
Indices start at one. Remainder wraps the last index back to zero, then adding one selects the first Point. This makes the closing edge explicit.
| corners |
corners := {0@0. 4@0. 4@3. 0@3}.
(1 to: corners size) collect: [:i |
(i \\ corners size) + 1]Expected: #(2 3 4 1)

Try it: For a triangle the next-index sequence is #(2 3 1).
04 - Keep all edge contributions
Collect before reducing so you can inspect the arithmetic. Two zero terms are legitimate: these edges lie along axes through the origin.
| corners |
corners := {0@0. 4@0. 4@3. 0@3}.
(1 to: corners size) collect: [:i |
| a b |
a := corners at: i.
b := corners at: (i \\ corners size) + 1.
(a x * b y) - (a y * b x)]Expected: #(0 12 12 0)

Try it: Add the four terms and divide by two: 12 square units.
05 - Make a reusable signed-area block
After processing an edge, total is the sum of all cross terms seen so far. At the end every edge has contributed exactly once. Division by two gives signed area in square coordinate units.
| area |
area := [:points |
| total a b |
points size < 3 ifTrue: [Error signal: 'Need at least three vertices'].
total := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
total := total + ((a x * b y) - (a y * b x))].
total / 2].
area value: {0@0. 4@0. 4@3. 0@3}Expected: 12

Try it: Use a 6-by-3 rectangle. The result should be 18.
06 - Use orientation and exact arithmetic
Reversing the boundary negates the signed result. abs gives geometric area for a simple polygon. With integer coordinates, Squeak keeps halves exact as Fractions.
| area triangle |
area := [:points |
| total a b |
points size < 3 ifTrue: [Error signal: 'Need at least three vertices'].
total := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
total := total + ((a x * b y) - (a y * b x))].
total / 2].
triangle := {0@0. 3@0. 0@1}.
{area value: triangle. area value: triangle reversed.
(area value: triangle reversed) abs}Expected: {3/2. -3/2. 3/2}

Try it: A collinear three-point boundary has zero area. That is not an exception.
07 - Handle an inward corner
The formula also handles concave simple polygons. This L shape is a 4-by-3 rectangle with a 3-by-2 rectangle removed. The answer must therefore be six.
| area |
area := [:points |
| total a b |
points size < 3 ifTrue: [Error signal: 'Need at least three vertices'].
total := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
total := total + ((a x * b y) - (a y * b x))].
total / 2].
area value: {0@0. 4@0. 4@1. 1@1. 1@3. 0@3}Expected: 6

Try it: Draw the boundary on paper before reordering any vertices.
08 - Check invariants, not only one answer
Translations preserve area; uniform scaling by k multiplies it by k squared. This checkpoint also tests winding, a degenerate triangle and the too-short-input guard.
| area p checks |
area := [:points |
| total a b |
points size < 3 ifTrue: [Error signal: 'Need at least three vertices'].
total := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
total := total + ((a x * b y) - (a y * b x))].
total / 2].
p := {0@0. 4@0. 4@3. 0@3}.
checks := {
(area value: (p collect: [:v | v + (7@(-5))])) = 12.
(area value: (p collect: [:v | v * 3])) = 108.
(area value: p reversed) = -12.
(area value: {0@0. 1@1. 2@2}) = 0.
([area value: {0@0. 1@1}. false]
on: Error do: [:e | true])}.
checks allSatisfy: [:each | each]Expected: true

Try it: A self-intersecting bow tie may give zero signed area. The contract excludes that case; abs cannot repair it.
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
For a simple polygon, abs of the signed result gives area. For self-intersections, positive and negative regions can cancel: do not present that result as filled area.
The calculation takes O(n) time and constant extra working space. The inspection checkpoint collects n terms only to make them visible.
Screen y increases downward. A clockwise screen boundary can have the positive sign of a counterclockwise mathematical boundary. Distinguish coordinate conventions before naming the winding.
Large integer coordinates stay exact in Squeak. Floating-point coordinates can suffer cancellation; this example does not provide a robust computational-geometry predicate.
Three practice challenges
Translate it
Move every L-shape vertex by 10@20. What changes?
Show solution
The coordinates change; the area remains 6. The extra cross terms cancel around the closed boundary.
Scale it
Double both coordinates of the triangle from step six.
Show solution
The signed area becomes 6: four times 3/2.
Choose a contract
Should a two-point input return zero?
Show solution
That is a possible API design, but this block explicitly rejects it. Keep the guard and tests consistent with your chosen contract.
Continue learning
First Point | Affine Transforms | Fractions
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Windows.