SMALLTALK LAB
Find a polygon’s area centroid in Squeak
Use signed edge contributions to compute the balance point of a filled polygon.
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 - Close the ring without duplicating the first vertex
Pair each vertex with the next clock position; modulo wraps the last vertex back to the first.
| points |
points := {0@0. 6@0. 0@3}.
(1 to: points size) collect: [:i |
{points at: i. points at: (i \\ points size) + 1}]Expected: {{0@0. 6@0}. {6@0. 0@3}. {0@3. 0@0}}

Try it: Check the receiver and argument of the closing edge.
02 - Calculate one edge contribution
The signed cross product contributes to twice the area. Multiply it by the sum of each endpoint coordinate for the corresponding centroid numerator.
| a b cross |
a := 6@0. b := 0@3.
cross := (a x * b y) - (b x * a y).
{cross. (a x + b x) * cross. (a y + b y) * cross}Expected: #(18 108 54)

Try it: Reverse the edge. All three terms change sign.
03 - Accumulate and divide for a triangle
The other two triangle edges touch the origin and contribute zero. Dividing its moments by three times the signed double-area gives (2, 1).
| centroid |
centroid := [:points |
| area2 sx sy a b cross |
area2 := sx := sy := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
cross := (a x * b y) - (b x * a y).
area2 := area2 + cross.
sx := sx + ((a x + b x) * cross).
sy := sy + ((a y + b y) * cross)].
area2 = 0 ifTrue: [nil] ifFalse: [
(sx / (3 * area2)) @ (sy / (3 * area2))]].
centroid value: {0@0. 6@0. 0@3}Expected: 2@1

Try it: A triangle happens to agree with the mean of its vertices. Try the L below.
04 - Balance an L shape in either winding
The horizontal bar has area four and the remaining stem has area three. The final balance point is in the cutout; reversing the vertices changes all signs but not the quotient.
| centroid path |
centroid := [:points |
| area2 sx sy a b cross |
area2 := sx := sy := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
cross := (a x * b y) - (b x * a y).
area2 := area2 + cross.
sx := sx + ((a x + b x) * cross).
sy := sy + ((a y + b y) * cross)].
area2 = 0 ifTrue: [nil] ifFalse: [
(sx / (3 * area2)) @ (sy / (3 * area2))]].
path := {0@0. 4@0. 4@1. 1@1. 1@4. 0@4}.
{centroid value: path. centroid value: path reversed}Expected: {(19/14)@(19/14). (19/14)@(19/14)}

Try it: Both coordinates exceed 1, so this point is not in the filled L.
05 - Return no result when the area is zero
A collinear ring and an empty input both have zero signed area here. Return nil rather than divide by zero.
| centroid |
centroid := [:points |
| area2 sx sy a b cross |
area2 := sx := sy := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
cross := (a x * b y) - (b x * a y).
area2 := area2 + cross.
sx := sx + ((a x + b x) * cross).
sy := sy + ((a y + b y) * cross)].
area2 = 0 ifTrue: [nil] ifFalse: [
(sx / (3 * area2)) @ (sy / (3 * area2))]].
{centroid value: {0@0. 1@0. 2@0}. centroid value: #()}Expected: {nil. nil}

Try it: Try the single point 2@3. Its closing edge has zero cross product.
06 - Translate the shape without changing its balance
Although individual cross products change when you move the coordinate origin, the centroid follows the shape. Move the triangle ten units right and its x coordinate increases by ten.
| centroid |
centroid := [:points |
| area2 sx sy a b cross |
area2 := sx := sy := 0.
1 to: points size do: [:i |
a := points at: i.
b := points at: (i \\ points size) + 1.
cross := (a x * b y) - (b x * a y).
area2 := area2 + cross.
sx := sx + ((a x + b x) * cross).
sy := sy + ((a y + b y) * cross)].
area2 = 0 ifTrue: [nil] ifFalse: [
(sx / (3 * area2)) @ (sy / (3 * area2))]].
centroid value: {10@0. 16@0. 10@3}Expected: 12@1

Try it: Repeat with a vertical translation.
Replay the checkpoints

Reason about correctness and limits
Use a simple polygon in a plane with uniform density. This example does not validate self-intersections, merge holes, or operate on longitude/latitude.
A nonzero centroid can lie outside a concave polygon. Do not assume it is a valid interior point for a label.
The single pass is O(n) time and constant accumulator storage. Integer coordinates make the example fractions exact; large or almost cancelling float coordinates warrant care.
Three practice challenges
Compute the area-weighted answer by hand
What are the centers of the two rectangles in the L?
Show solution
The area-four bar is centered at 2@(1/2) and the area-three stem at (1/2)@(5/2). Their weighted center is (19/14)@(19/14).
Translate a triangle
Add 10 to each x-coordinate. Where does the centroid go?
Show solution
It moves ten units right, from 2@1 to 12@1.
Separate the questions
Can the centroid double as an interior test?
Show solution
No. The centroid describes balance; use a point-in-polygon test or an interior-point algorithm for that other question.
Reference
Continue learning
Polygon Area | Point In Polygon
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.