Lesson 3.2 · III Modelling
Knots and building blocks
What it's about: A knot framework, written in the
language itself. Along the way it becomes clear what a language needs so
that you can write libraries in it: the right small building
blocks. Whatever can be built from them doesn't have to be in
Rust. The framework is the standard library knots
(import knots, see Lesson 2.4).

1. A knot is a function
A mathematical knot is a closed curve in space that doesn't intersect itself. In the language it is a path: a function that maps t = 0 … 360 (once around) to a point.
let trefoil = t => (r * cos(2 * t), r * sin(2 * t), 0.45 * sin(3 * t)) // r = 1 + 0.45 cos 3t
Because paths are functions, they can be reshaped with combinators. A combinator is a function that takes functions and returns a function again:
fn scaled(path, s) = t => path(t) * s
fn fitted(path, size) = scaled(path, size / 2 / reach(path))
fitted measures the path (at 180 points) and scales it
so that it fits exactly into size. A catalogue is a list of
records {name, path}, and knot(path, r) turns
a path into a solid. So the framework is just a handful of ordinary
functions.
The library offers:
| Kind | Functions |
|---|---|
| Paths | torus_knot(p, q),
turks_head(leads, bights),
lissajous(nx, ny, nz, px, py),
figure_eight |
| Combinators | scaled, moved, tilted,
squashed, fitted; reach measures
a path |
| Path → shape | curve_of (a smooth closed curve), knot (a
tube), rope (a twisted rope) |
| Ready-made | catalog (eight well-known knots with names),
carrick_bend (two ropes, section 3) |
A Lissajous knot oscillates along each axis with a frequency of its own: (cos(nx·t + px), cos(ny·t + py), cos(nz·t)). With coprime frequencies and suitable phases the path never meets itself. The figure-eight knot (4₁) is the simplest real knot after the trefoil; its formula is a rosette that rises and falls twice.
For the Turk's head (the family the Carrick mat belongs to) a simple formula turned up: a rosette r = 1 + a·cos(B·t) at angle L·t, with height z = h·sin(B·(L−1)·t). Here L is the number of strands and B the number of bights (the loops on the outside). B·(L−1) is exactly the number of crossings, and with this height the strand lies alternately over and under at each crossing. The formula was found numerically: find the crossings of the flat rosette and try out height functions.
2. What is missing is a building block
A tube around a closed curve is made by the built-in
tube (in Rust). Without such a basic operation, all that
would be left is merging many short rods, with visible grooves.
tube is a handy primitive. But it doesn't answer the real
question: Can it be written in the language?
Yes – with two small building blocks. The proof is in
examples/sweep_in_language.kf:
- Frames along the curve: three axes at every point,
z in the direction of travel. They should rotate as little as possible
(rotation-minimising), otherwise the tube gets twisted. The
double reflection method first reflects the sideways vector in
the plane between two points, then in the plane between the two
tangents. In the language this is a
historyover a record state{i, r}. - Holonomy: after one lap the frame comes back rotated by some angle. The angle depends on how the path winds. You spread it evenly over all the frames.
- Cross-sections and skin: put the profile on each
frame (
place) and stretch a surface through all of them (loft).
Two things can't be written this way. A smoothly closed curve needs a
knot vector that you can't specify with nurbs(…). And
loft has to know that the last cross-section is the first
one again; without that, the tessellation puts caps in at the seam. For
these there are two building blocks:
interpolate(points, closed: true) and
loft(sections, closed: true).
With them, the sweep written in the language gives the same
mesh as the built-in tube when it starts with the
same initial vector: the same triangles, every corner in the same place
to within 10⁻⁹ mm. It takes 0.03 instead of 0.02 seconds.

And it can do more: a profile with five lobes that rotates four times
in one lap makes a rope. There is a trap here. If you rotate the profile
by only three of its five lobes, it looks the same at the end
as at the start. But loft connects the control points with
the same index, and point 0 is then 216° further round.
Between the last and the first cross-section the skin twists once all
the way round. So looking the same is not enough; the points have to
correspond.
What sweep_in_language.kf shows step by step is
ready-made in the library sweeps:
frames(curve, n) gives the rotation-minimising frames
including the holonomy correction,
closed(curve, profile, twist:, count:) puts a profile on
them and lays the skin. knots.rope uses exactly that. A
flat band along the figure-eight knot, twisted twice:
let sweeps = import sweeps
let knots = import knots
let path = knots.curve_of(knots.fitted(knots.figure_eight, 60))
let band = rectangle(6, 1.5)
export sweeps.closed(path, band, twist: 2)
This is the idea that Lisp made famous: you can get by with a few
real primitives, the rest is library. A handy primitive like
tube may still exist, as a fast shortcut. From this follows
the project's rule: New geometry comes first as a small building
block; whatever can be built from it belongs in the
language.
3. The Carrick bend

If you join the ends of a Carrick bend, you get the Carrick mat, the
Turk's head with 3 strands and 4 bights. Conversely, the mat cut open
gives two ropes. So that they don't touch at the cut, each one ends a
little before it, and the ends run horizontally outwards.
knots.carrick_bend(size, r) gives the two ropes as a list,
for two colours for example:
let knots = import knots
let ropes = knots.carrick_bend(60, r: 2)
export ropes[0] color "terracotta"
export ropes[1] color "blue"
The same mat, drawn flat as a band with gaps where it passes
underneath, is the logo of knotflow (tools/favicon.py):
How it is checked
The sweep in the language and the built-in tube must
give the same triangles; a test computes both with the same start
vector. The frames of the built-in method must be perpendicular to each
other and turn only a little from one to the next. And the examples with
knots (knots, chain,
sweep_in_language) must give closed solids, in as many
parts as expected.
Try it yourself
cargo run --release -p kf -- render examples/knots.kf -D all=true -D rope=true -o out/knots.png
cargo run --release -p kf -- render examples/knots.kf -D which=9 -o out/carrick.stl
cargo run --release -p kf -- render examples/sweep_in_language.kf -D lobes=3 -D twist=2 -o out/rope.png
- Add a knot to
lib/knots.kf:turks_head(4, 5). How many crossings does it have according to the formula from section 1? Count them in the picture. - In
sweep_in_language.kf, rotate the profile by a fraction of a turn (change the line withrotate). Where does the seam appear, and why exactly there? - Something to think about: which built-in functions could be moved into the language with the existing building blocks? Where would Rust still be better, and why?