s LearnSqueak.com

SMALLTALK LAB

Find a polygon’s area centroid in Squeak

Use signed edge contributions to compute the balance point of a filled polygon.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

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.

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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Close the ring without duplicating the first vertex
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Check the receiver and argument of the closing edge.

Download this checkpoint

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.

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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Calculate one edge contribution
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Reverse the edge. All three terms change sign.

Download this checkpoint

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).

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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Accumulate and divide for a triangle
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: A triangle happens to agree with the mean of its vertices. Try the L below.

Download this checkpoint

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.

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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Balance an L shape in either winding
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Both coordinates exceed 1, so this point is not in the filled L.

Download this checkpoint

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.

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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Return no result when the area is zero
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try the single point 2@3. Its closing edge has zero cross product.

Download this checkpoint

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.

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

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Translate the shape without changing its balance
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Repeat with a vertical translation.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Find a polygon’s area centroid in Squeak 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 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

Robert Nürnberg’s centroid derivation

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.