s LearnSqueak.com

GEOMETRY · COMPOSITION

Affine transforms: move shapes with six numbers

Explore native Squeak affine transforms: translation, scaling, quarter turns, shear, composition order and exact inverse mappings.

8 native experiments10 SUnit testsComplete source

Install and open the native lab

A rectangle is a collection of Points. Instead of editing each corner by hand, define one mapping and apply it to every point. Six numbers represent a two-dimensional affine transform: x′ = a×x + c×y + tx and y′ = b×x + d×y + ty. This lesson focuses on coordinate mappings and composition rather than Bézier interpolation or ray intersections.

  1. Use a copy of Squeak 6.1 with its matching changes and sources files. This is a native-only workshop; these classes are not installed in the SqueakJS playground.
  2. Download affine-transforms.st. Select the file in Squeak’s File List and choose fileIn. Do not paste the entire chunk-format file into a Workspace.
  3. Keep one Workspace open for the numbered commands. Read method definitions in the Browser, not as Workspace expressions. The complete model and view are installed before you start; the steps explain and exercise that source.

Prerequisites: classes and objects, the related foundations lesson, and SUnit basics.

SUnit tests · Complete source kit

Keep the fixed-layout native window at least 980 × 620 pixels. No network, file saving or timers are used by the app. Close the window to clean up; Reset returns the view to its initial experiment.

01 · Keep every point unchanged

The identity mapping uses a=1, d=1 and zeros elsewhere. apply: evaluates the two coordinate formulas and creates a new Point. The original points are never changed. Gray shows the source rectangle; purple shows the mapped rectangle. With identity they overlap.

LSAffine2D method · read in Browser
apply: point
 ^ ((a * point x) + (c * point y) + tx) @
   ((b * point x) + (d * point y) + ty)
Same Workspace · select and Do it
view := LSAffineView open.

Expected: The purple outline covers the 80-by-60 gray rectangle. Values are #(1 0 0 1 0 0).

The drawing adds a screen origin after transforming model coordinates. This separates the mathematical origin from the window’s top-left corner. Smalltalk evaluates binary messages from left to right, so the parentheses around products and sums are intentional, not decorative.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 1: Keep every point unchanged
Genuine Squeak Morphic window after experiment 1.

02 · Translate without changing size

tx and ty add a fixed offset after the linear part. With the identity linear part, changing only these two values moves every corner by the same amount. The rectangle remains the same size and orientation.

LSAffine2D method · read in Browser
set: values
 (values size = 6 and: [values allSatisfy: [:each | each isNumber]])
  ifFalse: [self error: 'Use six numbers'].
 a := values at: 1. b := values at: 2. c := values at: 3.
 d := values at: 4. tx := values at: 5. ty := values at: 6.
 ^ self
Same Workspace · select and Do it
view next.

Expected: The purple rectangle moves 100 pixels right and 40 down. Values are #(1 0 0 1 100 40).

Print view model apply: 3@4 and expect 103@44. The six-value order is a, b, c, d, tx, ty, matching the two-dimensional matrix convention discussed in the linked W3C specification. This lab does not parse CSS or modify browser styles.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 2: Translate without changing size
Genuine Squeak Morphic window after experiment 2.

03 · Scale around the origin

Setting a to 2 doubles x while leaving y unchanged. Scaling acts about the model origin, so points farther from the origin move farther. A point exactly at 0@0 stays there. Scaling is not the same as changing a Morph’s extent.

LSAffine2D method · read in Browser
values
 ^ {a. b. c. d. tx. ty}
Same Workspace · select and Do it
view next.

Expected: The mapped rectangle becomes 160 wide and 60 high, starting at the same origin.

Print view model apply: 3@4 and expect 6@4. The values accessor returns a new Array. Changing that returned array does not edit the transform. Use set: with six numeric values to replace the mapping deliberately.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 3: Scale around the origin
Genuine Squeak Morphic window after experiment 3.

04 · Turn a quarter circle

The coefficients #(0 1 -1 0 0 0) map x@y to (-y)@x. In an upward-y mathematical plot this is a positive quarter turn. Squeak screen y increases downward, so the same coefficients appear as a clockwise turn on screen.

LSAffine2D method · read in Browser
determinant
 ^ (a * d) - (b * c)
Same Workspace · select and Do it
view next.

Expected: The rectangle extends left and down from the origin. Its area is unchanged; determinant is 1.

Print view model apply: 3@4 and expect -4@3. These integer coefficients produce an exact quarter turn without trigonometric rounding. General angles need sine and cosine and usually floating-point tolerance in tests. Do not silently compare those results for exact equality.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 4: Turn a quarter circle
Genuine Squeak Morphic window after experiment 4.

05 · Shear instead of rotate

The shear mapping x′=x+y, y′=y shifts each row horizontally by its y coordinate. A rectangle becomes a parallelogram. Straight lines remain straight and parallel lines remain parallel, but angles generally change.

LSAffine2D method · read in Browser
apply: point
 ^ ((a * point x) + (c * point y) + tx) @
   ((b * point x) + (d * point y) + ty)
Same Workspace · select and Do it
view next.

Expected: The lower edge shifts 60 pixels right; the upper edge remains in place.

Print view model apply: 3@4 and expect 7@4. Affine transformations can translate, rotate, scale, reflect and shear. They cannot model a perspective projection with vanishing points; that requires a different coordinate representation.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 5: Shear instead of rotate
Genuine Squeak Morphic window after experiment 5.

06 · Compose move then scale

then: means apply the receiver first, then its argument. The implementation transforms the receiver’s origin and two basis endpoints through the second mapping. Subtracting the transformed origin recovers the two new basis vectors. This is equivalent to composing the affine formulas.

LSAffine2D method · read in Browser
then: other
 | p xAxis yAxis |
 p := other apply: tx@ty.
 xAxis := (other apply: (a + tx)@(b + ty)) - p.
 yAxis := (other apply: (c + tx)@(d + ty)) - p.
 ^ self class new set: {xAxis x. xAxis y. yAxis x. yAxis y. p x. p y}
Same Workspace · select and Do it
view next.

Expected: The combined values are #(2 0 0 1 200 40). The x translation has doubled.

Read experiment in LSAffineView: move then: scale creates this state. Print view model apply: 3@4 and expect 206@44. Moving by 100 before doubling x makes that offset 200. This explicit then: convention avoids guessing whether a matrix library uses pre- or post-multiplication.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 6: Compose move then scale
Genuine Squeak Morphic window after experiment 6.

07 · Reverse the order and compare

Scaling first and moving second gives a different result. The scale changes the shape, while the later translation remains exactly 100@40. Composition is associative but is generally not commutative: regrouping a sequence is different from swapping operations.

LSAffine2D method · read in Browser
then: other
 | p xAxis yAxis |
 p := other apply: tx@ty.
 xAxis := (other apply: (a + tx)@(b + ty)) - p.
 yAxis := (other apply: (c + tx)@(d + ty)) - p.
 ^ self class new set: {xAxis x. xAxis y. yAxis x. yAxis y. p x. p y}
Same Workspace · select and Do it
view next.

Expected: The combined values are #(2 0 0 1 100 40); 3@4 maps to 106@44.

Compare this screenshot with the previous one. Width is identical, but the left edge differs by 100 pixels. To scale around a chosen pivot, translate that pivot to the origin, scale, then translate back. The order of those three mappings is essential.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 7: Reverse the order and compare
Genuine Squeak Morphic window after experiment 7.

08 · Undo an invertible mapping

inverse uses the determinant to invert the linear part and correct the translation. A zero determinant means information was collapsed, so no inverse exists. The teaching model raises an Error rather than dividing by zero or returning a misleading mapping.

LSAffine2D method · read in Browser
inverse
 | det |
 det := self determinant.
 det = 0 ifTrue: [self error: 'Singular transform has no inverse'].
 ^ self class new set: {d / det. b negated / det. c negated / det. a / det.
  ((c * ty) - (d * tx)) / det. ((b * tx) - (a * ty)) / det}
Same Workspace · select and Do it
view next.

Expected: The inverse values are #(1/2 0 0 1 -50 -40), displayed with Squeak’s Fraction print form.

Print view model apply: 106@44 and expect 3@4. The picture applies the inverse to the original gray rectangle, not to the previously transformed purple rectangle. To demonstrate a round trip, compose a transform with its inverse or apply the inverse to an already mapped point.

NATIVE SQUEAK CAPTURE
Native Squeak Affine transforms: move shapes with six numbers, experiment 8: Undo an invertible mapping
Genuine Squeak Morphic window after experiment 8.

Replay the native experiments

NATIVE SQUEAK CAPTURE
Affine transforms: move shapes with six numbers before the experiment sequence
Eight genuine native window captures after the scripted lesson commands, not a mouse recording or interpolated animation.

Verify, extend and know the limits

File in the tests first · select and Print it
LSAffineTest suite run

Expected: 10 tests, 10 passes, 0 failures and 0 errors. File the test source in through the File List before evaluating the suite. Tests were run in Squeak 6.1 build 23976.

The tests cover identity, translation, scaling, rotation, shear, noncommuting composition, a round trip, singular matrices and malformed value lists. Exact integer and Fraction examples avoid rounding ambiguity. For floating-point work, choose a tolerance based on the scale of your data; matrices near singularity can amplify error even when the determinant is nonzero. This is a small mathematical model, not a full scene graph, mouse-coordinate system or perspective renderer.

W3C: CSS Transforms Level 1 provides background for the underlying idea; this Smalltalk implementation and tutorial are original. Official Squeak documentation covers the environment and language.

Continue with Prefix tries: build an autocomplete index · Topological ordering: schedule prerequisites.