SMALLTALK LAB
Barycentric Coordinates in Squeak: Test Points Against a Triangle
Turn three signed areas into exact weights. Rebuild a point from those weights, classify it as inside, on the edge, or outside, and handle triangles that have collapsed into a line.
Before you begin
Open a Squeak Workspace. Use Print it on each complete checkpoint, including its temporary variable declarations. The examples use Squeak Points such as 2@1 and exact arithmetic on integer inputs.
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 show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.
01 - Measure doubled area with a cross product
Subtract the first point from the other two to make two vectors. Multiply their coordinates diagonally and subtract. The magnitude is twice the triangle’s area, so this triangle has area 18. Swapping the last two vertices reverses the sign. Three collinear points give zero, because the vectors point along the same line.
| area2 |
area2 := [:u :v :w | | d e |
d := v - u.
e := w - u.
(d x * e y) - (d y * e x)].
{area2 value: 0@0 value: 6@0 value: 0@6.
area2 value: 0@0 value: 0@6 value: 6@0.
area2 value: 0@0 value: 2@2 value: 4@4}Expected: #(36 -36 0)

Try it: Move all three points by the same amount. Neither difference vector changes, so the result stays the same.
02 - Replace one vertex at a time
The first number belongs to A: replace A with P and keep B and C. Repeat for B and C without changing their slots. When P is inside, the three smaller triangles fill the large one and their doubled areas add to 36. Outside the triangle, the same sum still holds, but some signed terms can be negative.
| area2 a b c p |
area2 := [:u :v :w | | d e |
d := v - u.
e := w - u.
(d x * e y) - (d y * e x)].
a := 0@0. b := 6@0. c := 0@6. p := 2@1.
{area2 value: p value: b value: c.
area2 value: a value: p value: c.
area2 value: a value: b value: p}Expected: #(18 12 6)

Try it: Move P to A. The first small triangle recovers the whole area and the other two become flat.
03 - Normalize with exact fractions
Divide each signed area by the original signed area. With integer coordinates and ordinary division, Squeak keeps the results as exact fractions. These are the barycentric coordinates of P, in the same order as A, B, and C. If you reverse the vertex order, both the denominator and the corresponding numerators change sign, so the weights follow their vertices.
| area2 weights triangle ws |
area2 := [:u :v :w | | d e |
d := v - u.
e := w - u.
(d x * e y) - (d y * e x)].
weights := [:vertices :p | | a b c denom |
a := vertices first. b := vertices second. c := vertices third.
denom := area2 value: a value: b value: c.
denom = 0
ifTrue: [nil]
ifFalse: [
{(area2 value: p value: b value: c) / denom.
(area2 value: a value: p value: c) / denom.
(area2 value: a value: b value: p) / denom}]].
triangle := {0@0. 6@0. 0@6}.
ws := weights value: triangle value: 2@1.
{ws. ws sum = 1}Expected: {{1 / 2. 1 / 3. 1 / 6}. true}

Try it: The weight at C equals 1/6. Here that means P is one-sixth of the way up from the base line AB.
04 - Rebuild the point from the three weights
The weights tell you how much of each vertex goes into P. Multiply each vertex’s x and y by its weight, then sum the x contributions and the y contributions separately. This is useful beyond geometry: the same weights blend any values attached to the vertices, such as color channels.
| area2 weights triangle ws x y |
area2 := [:u :v :w | | d e |
d := v - u.
e := w - u.
(d x * e y) - (d y * e x)].
weights := [:vertices :p | | a b c denom |
a := vertices first. b := vertices second. c := vertices third.
denom := area2 value: a value: b value: c.
denom = 0
ifTrue: [nil]
ifFalse: [
{(area2 value: p value: b value: c) / denom.
(area2 value: a value: p value: c) / denom.
(area2 value: a value: b value: p) / denom}]].
triangle := {0@0. 6@0. 0@6}.
ws := weights value: triangle value: 2@1.
x := 0. y := 0.
1 to: 3 do: [:i | | v weight |
v := triangle at: i. weight := ws at: i.
x := x + (v x * weight).
y := y + (v y * weight)].
x@yExpected: 2@1

Try it: Attach numbers 10, 40, and 70 to A, B, and C. The blend at P is 30.
05 - Tell inside, edge, and outside apart
Strictly positive weights mean inside. A zero with no negatives means edge; this version includes vertices in that category. Any negative weight means outside, even if another weight is zero. The point 7@0 is on the line through the bottom edge but lies beyond the segment, so checking negatives first matters.
| area2 weights classify triangle |
area2 := [:u :v :w | | d e |
d := v - u.
e := w - u.
(d x * e y) - (d y * e x)].
weights := [:vertices :p | | a b c denom |
a := vertices first. b := vertices second. c := vertices third.
denom := area2 value: a value: b value: c.
denom = 0
ifTrue: [nil]
ifFalse: [
{(area2 value: p value: b value: c) / denom.
(area2 value: a value: p value: c) / denom.
(area2 value: a value: b value: p) / denom}]].
classify := [:corners :p | | ws |
ws := weights value: corners value: p.
ws ifNil: [#degenerate] ifNotNil: [
(ws anySatisfy: [:w | w < 0]) ifTrue: [#outside] ifFalse: [
(ws includes: 0) ifTrue: [#edge] ifFalse: [#inside]]]].
triangle := {0@0. 6@0. 0@6}.
{classify value: triangle value: 2@1.
classify value: triangle value: 3@0.
classify value: triangle value: 7@0.
classify value: triangle value: 0@0}Expected: #(inside edge outside edge)

Try it: Try 3@3 on the slanted edge. Its A weight is zero.
06 - Reject a flat triangle and check vertex order
When the doubled area is zero, these coordinates are not uniquely determined, so return nil from the weight block rather than dividing by zero. The classifier reports a degenerate triangle whether or not the query is on its line. Reversing a valid triangle leaves the classification intact and swaps the two weights with their vertices.
| area2 weights classify triangle reversed flat |
area2 := [:u :v :w | | d e |
d := v - u.
e := w - u.
(d x * e y) - (d y * e x)].
weights := [:vertices :p | | a b c denom |
a := vertices first. b := vertices second. c := vertices third.
denom := area2 value: a value: b value: c.
denom = 0
ifTrue: [nil]
ifFalse: [
{(area2 value: p value: b value: c) / denom.
(area2 value: a value: p value: c) / denom.
(area2 value: a value: b value: p) / denom}]].
classify := [:corners :p | | ws |
ws := weights value: corners value: p.
ws ifNil: [#degenerate] ifNotNil: [
(ws anySatisfy: [:w | w < 0]) ifTrue: [#outside] ifFalse: [
(ws includes: 0) ifTrue: [#edge] ifFalse: [#inside]]]].
triangle := {0@0. 6@0. 0@6}.
reversed := {0@0. 0@6. 6@0}.
flat := {0@0. 3@0. 6@0}.
{classify value: reversed value: 2@1.
(weights value: reversed value: 2@1) asArray
= {1 / 2. 1 / 6. 1 / 3}.
classify value: flat value: 2@1.
classify value: flat value: 2@0}Expected: #(inside true degenerate degenerate)

Try it: Repeat a vertex, such as using B twice. That also makes the triangle degenerate.
Replay the checkpoints

Reason about correctness and limits
Keep the vertex order consistent between the whole triangle and every replacement. Taking absolute areas erases the sign that reveals an outside point.
The sign convention assumes a mathematical coordinate plane with y increasing upward. With screen coordinates y often points down, which flips the sign interpretation, but all normalized ratios and classifications still work.
The exact zero tests are intended for integer or Fraction coordinates. Floating-point geometry near an edge needs a documented tolerance or a robust orientation predicate; an arbitrary global epsilon is not a drop-in replacement.
Three practice challenges
Check the centroid
What weights should the point 2@2 have for the example triangle?
Show solution
A, B, and C each have weight 1/3.
Find two zeros
What are the three weights at B?
Show solution
0, 1, and 0, respectively. The classifier counts vertices as edge points.
Spot a negative weight
Which vertex weight is negative at 4@4, and what does it tell you?
Show solution
A has weight −1/3. The other weights are both 2/3, so the point is outside across the edge BC.
Continue learning
Polygon Area | Affine Transforms | First Point
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.