Chapter 10
Patterns and copies
Many models repeat something: pegs on a plate, holes on a circle, the links of a chain, spines on a body. In knotflow you describe the part once and then say where it should go. This chapter shows the ways to do that, from a simple grid to hundreds of copies spread at random.
A grid: two generators in one list
param n = 5 in 2..10 "pegs per direction"
param pitch = 12 in 6..30 "distance between the pegs [mm]"
param peg = 2 in 1..5 "radius of a peg [mm]"
let side = n * pitch
let places = [(i - (n - 1) / 2) * pitch | i <- range(n)]
let plate = box(side, side, 3) |> move(z: 1.5)
let pegs = [cylinder((x, y, 2), (x, y, 12), peg) | x <- places, y <- places]
export fold(pegs, plate, union)
placesare the n positions on one axis, spread around the middle: for n = 5 the values -24, -12, 0, 12, 24.range(n)counts as in Python, from 0 to n - 1.boxhas its middle at the origin;move(z: 1.5)stands the 3 mm plate on z = 0.[… | x <- places, y <- places]has two generators: for every x, y runs through all its values. That makes n × n pegs.- Each peg starts at z = 2, so 1 mm inside the plate. Parts that only touch are two separate bodies for a slicer; a little overlap makes them one for sure.
fold(pegs, plate, union)joins one peg after the other to the plate: firstunion(plate, pegs[0]), then the result withpegs[1], and so on.
Instead of the union you can simply export the parts as a list:
export [plate, ...pegs] (... puts in the
elements of a list). kf render then reports
parts: 26: 26 bodies that overlap. That is a valid STL, and
slicers join overlapping bodies when they print. union, on
the other hand, computes one single, exact solid (chapter 7); with many
parts that takes longer.
Staggered rows
For a honeycomb pattern every second row moves by half a pitch. The
if stands right inside the arithmetic:
param rows = 6 in 2..12 "rows"
param cols = 8 in 2..12 "holes per row"
param pitch = 8 in 4..20 "distance between the holes [mm]"
let dy = pitch * sqrt(3) / 2 // distance between the rows
fn spot(i, j) = (pitch * i + (if j % 2 == 1 then pitch / 2 else 0), dy * j)
let holes = [polygon(6, pitch / 2 - 1) |> rotate(30) |> translate(spot(i, j))
| j <- range(rows), i <- range(cols)]
let plate = rectangle(pitch * (cols + 1.5), dy * (rows - 1) + 2 * pitch)
|> move(x: pitch * (cols - 1) / 2 + pitch / 4, y: dy * (rows - 1) / 2)
export fold([extrude(h, 4) |> move(z: -1) | h <- holes], extrude(plate, 2), difference)
With dy = pitch · √3 / 2 all neighbours are the same
distance apart. The holes are prisms 4 mm high that start 1 mm below the
plate: that way they cut through both sides for sure instead of ending
exactly on a face. difference takes one solid at a time; a
list as its second argument is an error, hence the
fold.
A circle: turn instead of compute
Draw one hole of a bolt circle at 0° on the x axis, then turn it about the upright axis:
param holes = 8 in 3..24 "holes"
param radius = 30 in 15..60 "radius of the bolt circle [mm]"
let disc = cylinder((0, 0, 0), (0, 0, 4), radius + 10)
let hole = cylinder((radius, 0, -1), (radius, 0, 5), 3)
export fold([rotate(hole, 360 * k / holes) | k <- range(holes)], disc, difference)
rotate(shape, angle) turns about the z axis through the
origin. That is why the part stands at its place at 0° before it is
turned, not at the origin.
Pitfall: [0 .. holes] includes
holes itself. The last hole would then lie at 360°, exactly
on the first one. range(holes) stops one before.
Symmetry: mirroring
Many parts are the same on the left and the right, only
mirror-inverted. You build one half and mirror it with
mirror(shape, normal) at the plane through the origin that
is perpendicular to normal:
param reach = 30 in 15..60 "length of the arms [mm]"
let post = cylinder((0, 0, 0), (0, 0, 20), 5)
let arm = pipe(interpolate([(3, 0, 6), (reach / 2, 10, 10), (reach, 4, 16)]), 2.5)
export [post, arm, mirror(arm, (1, 0, 0))]
mirror(arm, (1, 0, 0))mirrors at the y-z plane: x becomes -x, y and z stay. So the arm bends towards +y on both sides.- A turn by 180° (
rotate(arm, 180)) would give something else: the second arm would bend towards -y. Mirroring and turning are only the same for shapes that are symmetric themselves. - Two mirrorings make it fourfold symmetric:
[a, mirror(a, (1, 0, 0)), mirror(a, (0, 1, 0)), mirror(mirror(a, (1, 0, 0)), (0, 1, 0))]. - The triangles of a mirrored solid still face outwards; you need not take care of that.
Along a curve: divide
param count = 24 in 4..80 "beads"
let path = interpolate([(0, 0), (30, 20), (60, -10), (90, 15)])
export pipe(path, 1) color "gray"
export instance(sphere(3), divide(path, count)) color "terracotta"
interpolatelays a smooth curve through the points,pipemakes a wire of it.divide(path, count)givescountpoints at equal distances along the curve, measured by its length. On a closed curve (acircle, say) the start is not repeated at the end.instance(shape, places)puts a copy of the ball on every point. What makesinstancespecial is explained further down.
Facing the right
way: frames and frames_along
A point only says where something goes, not which way
round. That is what frames are for: a point with
three axes, as a record {origin, x, y, z}, a small
coordinate system of its own. A shape standing on the origin and
pointing up (+z) stands on the frame once it is placed, pointing along
the frame's z axis.

param count = 12 in 3..40 "rings"
let path = interpolate([(0, 0, 0), (40, 0, 20), (40, 40, 40), (0, 40, 60)])
let ring = torus(5, 1)
export tube(path, 1.5) color "gray"
export instance(ring, frames_along(path, count)) color "teal"
frames_along(path, count) gives frames at equal
distances: z points along the curve, x and y lie across it and turn as
little as possible. The torus lies flat around the z axis, so once
placed it goes around the wire.
Tools for frames:
| call | does |
|---|---|
place(shape, f) |
puts a single shape on the frame f |
spin(f, angle) |
turns the frame about its own z axis |
sized(f, k) |
makes its axes k times as long: what stands on it becomes k times as large |
frame_at(point, direction) |
a frame at a point, z along the direction |
frame_on(surface, u, v) |
a frame on a surface, z is the normal |
A chain puts oval links along a curve and turns every second one by 90°, so that they interlock:
param links = 24 in 8..60 "links (an even number closes the chain)"
param wire = 1 in 0.5..2 "wire thickness (radius) [mm]"
let path = circle(40)
let pitch = 2 * pi * 40 / links // distance from link to link
let long = pitch + 4 * wire + 0.6 // long enough to reach around the next one
fn oval(t) = (long * 0.31 * cos(t), 0, long / 2 * sin(t))
let link = tube(interpolate([oval(t) | t <- range(0, 360, step: 15)], closed: true), wire)
let frames = frames_along(path, links)
export instance(link, [if i % 2 == 0 then f else spin(f, 90) | (i, f) <- enumerate(frames)]) color "gray"
- The link is an oval in the x-z plane, its long side along z, that is
in the frame's direction of travel.
tubemakes a wire of the closed curve. enumerategives each element with its number,(i, f) <- …takes the pair apart.- The example
chaindoes the same along a knot and measures the length of the curve for it itself.
What frames are exactly and how frames_along carries
them on without needless twisting is explained in Lesson
3.1.
Scattering over a surface:
scatter
param count = 200 in 10..1000 "spines (at most)"
param spacing = 4 in 0..10 "minimum spacing [mm]"
param seed = 1 in 0..100 "random seed"
let body = sphere(25)
let spike = cone(1.5, 0, 10) |> move(z: -1)
let spots = scatter(body, count, seed: seed, spacing: spacing)
let sizes = [sized(f, 0.6 + 0.8 * rand(seed, i)) | (i, f) <- enumerate(spots)]
let n = len(spots)
export body color "plum"
export instance(spike, sizes) color "ochre"
scatter(surface, count, seed:, spacing:)gives random frames on the surface. They are spread by area (they do not crowd at the poles of the ball), and z is the normal, pointing out.seedchooses the chance: the same value always gives the same points, on every computer and in the browser. If you do not like a spread, try another seed.spacingis the minimum spacing. Without it, chance makes clumps and gaps; with it, it looks sown. If not all points fit on the surface, there are fewer. Socountis an upper limit. How many there are is shown byn(in the browser under "Values", withkf renderin the list of values).rand(seed, i)is a random number between 0 and 1, always the same for the same arguments. Withsizedevery spine gets its own length that way, between 0.6 and 1.4 times.- The cone starts 1 mm below the origin, so it sits a little inside the body. On a curved surface its rim would otherwise stand in the air.
scatter takes meshes too, such as the result of
isosurface (chapter 12), and solids with holes made by
perforate (chapter 6): no frame lies in a hole.
By the surface's grid:
grid_on

param around = 16 in 4..40 "studs around"
param up = 6 in 1..20 "studs one above the other"
let vase = revolve(nurbs([(0, 0), (30, 0), (38, 30), (22, 70), (26, 90), (0, 90)], degree: 2))
let stud = sphere(2.5)
export vase color "sage"
export instance(stud, grid_on(vase, around, up)) color "ochre"
grid_on(surface, nu, nv) divides the surface into nu ×
nv cells and gives the frames at their centres. Around, the distances
are equal; up the side they follow the surface's
parameters, not its length. That is why the rows are
not equally far apart, and because the profile goes in to the axis at
the top, the lid gets a row too. If you need even distances, use
scatter with spacing.
instance:
compute once, show many times
instance(shape, places) takes a shape and a list of
frames or points. The shape is cut into triangles once,
fine enough for the largest copy; all others are only placements.
- In the browser the graphics card gets the shape once and a list of placements with it. It draws 1,600 balls that way in milliseconds instead of seconds.
- In the STL all the triangles are there all the same; they are put together when the file is saved.
- In the 3MF the shape is stored once, the copies refer to it (chapter 13). The file stays small.
move, rotate, scale and
mirror work on instances as on other shapes.
union, difference and
intersection do not take them, though: "union: b must be a
surface or a solid, got instances". realize turns instances
back into separate shapes:
let frames = [frame_at((x, 0, 0), (0, 0, 1)) | x <- [0, 15, 30]]
let posts = instance(cylinder((0, 0, 0), (0, 0, 10), 3), frames)
let base = box(50, 12, 3) |> move(x: 15, z: -1)
export fold(realize(posts), base, union)
Rule of thumb: for many equal parts that are only shown and printed,
instance. If they are to merge with something else into one
solid, realize and union, and then some
patience: for the sea urchin with 450 spines that takes a few
seconds.
A single part is worth an instance too when a slider
turns or moves it: a turned solid is a new solid and is cut into
triangles again at every move, a copy at a new frame takes the triangles
of the shape that are already there. In the example
differential (instance(part, [pose]) for each
gear), turning the drive recomputes in 0.8 instead of 14 seconds this
way.
The examples
In the browser under Model, in the source in
examples/:
- chain – links along a trefoil knot with
frames_along, every second one turned withspin. The model works out the length of the curve itself and makes the links as long as they need to be. - urchin – a sea urchin: spines with
scatterand a minimum spacing, each one a different length withsized. Withwhole = 1all spines are joined to the body into one solid. - sowing – the same number of points on two balls, on
the left pure chance, on the right with a minimum spacing. This shows
what
spacingdoes.
How divide, scatter and
instance work inside is explained in lesson
3.1.