s LearnSqueak.com

SMALLTALK LAB

Liang–Barsky Line Clipping in Squeak: Keep the Visible Segment

Clip a segment against a rectangular viewport without walking along pixels. Track the earliest and latest allowed position, then turn those positions into exact Squeak Points.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Squeak Workspace. Select one entire checkpoint, including its first line, then choose Print it. Use a rectangle whose lower coordinate limits are no larger than its upper limits; both limits count as inside.

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 - Put a slider on the segment

Think of t as the position of a slider that runs from zero to one. Starting at A, add t times the change from A to B. At zero you get A; at one you get B. Squeak’s Point multiplies each coordinate by a scalar, and division of integers keeps the midpoint exact.

Independent Workspace expression - select all, then Print it
| a b d at |
a := (-2)@1. b := 8@4. d := b - a.
at := [:t | a + (d * t)].
{at value: 0. at value: 1 / 2. at value: 1}

Expected: {(-2)@1. 3@(5 / 2). 8@4}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Put a slider on the segment
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try t = 1/10. You are still to the left of x = 0, at the point (-1)@(13/10).

Download this checkpoint

02 - Rewrite each edge as p times t ≤ q

The left bound says a.x + dx·t must be at least xmin. Moving terms makes it −dx·t ≤ a.x − xmin. The right edge instead gives dx·t ≤ xmax − a.x. Repeat for y. Each pair holds p and q in the order minimum x, maximum x, minimum y, maximum y.

Independent Workspace expression - select all, then Print it
| a b d lo hi |
a := (-2)@1. b := 8@4. d := b - a.
lo := 0@0. hi := 6@4.
{{d x negated. a x - lo x}. {d x. hi x - a x}.
 {d y negated. a y - lo y}. {d y. hi y - a y}}

Expected: #((-10 -2) (10 8) (-3 1) (3 3))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Rewrite each edge as p times t ≤ q
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: For the first pair, dividing −10·t ≤ −2 by −10 reverses the inequality, so t must be at least 1/5.

Download this checkpoint

03 - Take the strongest entry and exit limits

The starting interval is [0,1], since we want a segment rather than its infinite supporting line. A negative p imposes a lower bound on t after the inequality flips; a positive p imposes an upper bound. Keep the greatest lower bound and the least upper bound. This checkpoint uses only nonzero p; the next one handles parallel edges.

Independent Workspace expression - select all, then Print it
| enter exit |
enter := 0. exit := 1.
#((-10 -2) (10 8) (-3 1) (3 3))
 do: [:edge | | p q t |
  p := edge first. q := edge second.
  t := q / p.
  p < 0
   ifTrue: [enter := enter max: t]
   ifFalse: [exit := exit min: t]].
{enter. exit}

Expected: {1 / 5. 4 / 5}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Take the strongest entry and exit limits
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change the second pair to (10 5). The right bound tightens, and exit becomes 1/2.

Download this checkpoint

04 - Clip and reconstruct exact endpoints

A p of zero means the coordinate cannot change along this segment. If q is negative then no t could satisfy 0 ≤ q, so mark the segment outside; otherwise that edge imposes no restriction. Once all four edges are checked, reject an empty interval or turn its two limits back into Points. The first and last intersections here lie on the vertical edges, and neither y coordinate was rounded.

Independent Workspace expression - select all, then Print it
| clip |
clip := [:a :b :lo :hi | | d enter exit outside |
 d := b - a.
 enter := 0. exit := 1. outside := false.
 {{d x negated. a x - lo x}. {d x. hi x - a x}.
  {d y negated. a y - lo y}. {d y. hi y - a y}}
  do: [:boundary | | p q t |
   p := boundary first. q := boundary second.
   p = 0
    ifTrue: [q < 0 ifTrue: [outside := true]]
    ifFalse: [
     t := q / p.
     p < 0
      ifTrue: [enter := enter max: t]
      ifFalse: [exit := exit min: t]]].
 (outside or: [enter > exit]) ifTrue: [nil] ifFalse: [
  {a + (d * enter). a + (d * exit)}]].
clip value: (-2)@1 value: 8@4 value: 0@0 value: 6@4

Expected: {0@(8 / 5). 6@(17 / 5)}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Clip and reconstruct exact endpoints
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Swap A and B. You should see the same intersections in reverse order.

Download this checkpoint

05 - Distinguish parallel, separated, and touching segments

The first segment stays above the window, so a parallel edge rejects it. The second heads toward the window but stops short: its required entry t is greater than one, so enter exceeds exit. The third can touch only the shared corner at 0@0. Equal entry and exit limits are valid and describe a one-point intersection.

Independent Workspace expression - select all, then Print it
| clip |
clip := [:a :b :lo :hi | | d enter exit outside |
 d := b - a.
 enter := 0. exit := 1. outside := false.
 {{d x negated. a x - lo x}. {d x. hi x - a x}.
  {d y negated. a y - lo y}. {d y. hi y - a y}}
  do: [:boundary | | p q t |
   p := boundary first. q := boundary second.
   p = 0
    ifTrue: [q < 0 ifTrue: [outside := true]]
    ifFalse: [
     t := q / p.
     p < 0
      ifTrue: [enter := enter max: t]
      ifFalse: [exit := exit min: t]]].
 (outside or: [enter > exit]) ifTrue: [nil] ifFalse: [
  {a + (d * enter). a + (d * exit)}]].
{clip value: (-2)@5 value: 8@5 value: 0@0 value: 6@4.
 clip value: (-3)@1 value: (-2)@2 value: 0@0 value: 6@4.
 clip value: (-2)@2 value: 2@(-2) value: 0@0 value: 6@4}

Expected: {nil. nil. {0@0. 0@0}}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Distinguish parallel, separated, and touching segments
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Slide the first segment to y = 4. Parallel to that edge now means on it, and the segment clips to the top boundary of the coordinate range.

Download this checkpoint

06 - Treat a zero-length segment as a point

All four p values are zero when A equals B. An inside or boundary point satisfies every fixed inequality and comes back as two identical endpoints. An outside point violates at least one of those inequalities, so clipping returns nil. The caller can draw an explicit pixel or marker if it wants point intersections to be visible.

Independent Workspace expression - select all, then Print it
| clip |
clip := [:a :b :lo :hi | | d enter exit outside |
 d := b - a.
 enter := 0. exit := 1. outside := false.
 {{d x negated. a x - lo x}. {d x. hi x - a x}.
  {d y negated. a y - lo y}. {d y. hi y - a y}}
  do: [:boundary | | p q t |
   p := boundary first. q := boundary second.
   p = 0
    ifTrue: [q < 0 ifTrue: [outside := true]]
    ifFalse: [
     t := q / p.
     p < 0
      ifTrue: [enter := enter max: t]
      ifFalse: [exit := exit min: t]]].
 (outside or: [enter > exit]) ifTrue: [nil] ifFalse: [
  {a + (d * enter). a + (d * exit)}]].
{clip value: 3@2 value: 3@2 value: 0@0 value: 6@4.
 clip value: 6@4 value: 6@4 value: 0@0 value: 6@4.
 clip value: 7@2 value: 7@2 value: 0@0 value: 6@4}

Expected: {{3@2. 3@2}. {6@4. 6@4}. nil}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Treat a zero-length segment as a point
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try 0@2 twice. Because the limits are inclusive, a point on the side is also retained.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Liang–Barsky Line Clipping in Squeak: Keep the Visible Segment native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

See the viewport trim the line

NATIVE SQUEAK CAPTURE
Native Squeak viewport diagram showing a segment crossing a rectangle
Native Squeak diagram: gray shows the full segment, red marks the two exact intersection coordinates, and green shows the retained part.

Download the native visual example

Reason about correctness and limits

Smalltalk’s binary messages share one precedence and run left to right. Write a + (d * t), not a + d * t; write 0@(8 / 5) when constructing a point with a fractional y coordinate.

Only divide by p when it is nonzero. In nearly parallel floating-point data, a small nonzero p is still meaningful, especially on long segments. Define a tolerance for your own coordinate scale rather than replacing the exact zero test blindly.

These are inclusive coordinate bounds, independent of which way y increases on screen. For a pixel buffer indexed from 0, the last valid x is width − 1, not width. Clip before choosing how to round for raster drawing.

Clipping keeps a subset of the original segment. It does not clip curved paths or arbitrary polygon outlines, and it assumes correctly ordered limits. A rectangle with zero width or height is allowed, but may leave only a segment or a point.

Three practice challenges

Keep a segment on the edge

What should the full clip block return for A = (-2)@0 and B = 8@0 in the same viewport?

Show solution

{0@0. 6@0}. The y coordinate stays on a valid boundary and imposes no extra constraint.

Check a vertical crossing

Clip 3@(-2) to 3@6 against limits 0@0 and 6@4. Which constraints become parallel?

Show solution

Both x constraints have p = 0 and their q values are nonnegative. The result is {3@0. 3@4}, with entry 1/4 and exit 3/4.

Recognize an entirely visible path

For A = 1@1 and B = 5@3 inside the same rectangle, what is the final t interval?

Show solution

[0,1]. No edge tightens either bound, so the original endpoints are returned unchanged.

Continue learning

Bresenham Lines | Barycentric Triangle | Affine Transforms

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.