Chapter 9
Constructing
Some measures are not known directly, only through conditions: the
side is at 43°, here is a right angle, the circle touches all three
sides. Instead of working out the points yourself, you write down the
conditions, and solve finds the values.
param at_b = 43 in 10..80 "Angle at B [°]"
let a = (0, 0)
let b = (50, 0)
let s = solve({c: (25, 20), m: (22, 7), r: 5}, v => [
angle_at(a, b, v.c) - at_b, // the angle at B
angle_at(a, v.c, b) - 90, // a right angle at C
touches(v.m, v.r, a, b), // the circle touches all three sides
touches(v.m, v.r, b, v.c),
touches(v.m, v.r, a, v.c)])
export extrude(polyline([a, b, s.c], closed: true), 4)
export cylinder((s.m.x, s.m.y, 4), (s.m.x, s.m.y, 8), s.r)
- The first part of
solveis the unknowns with starting values, roughly as you would draw them: numbers and points. - The second part is a function giving the conditions as a list. Each is a number that should be 0 when the condition holds.
- What comes out is a record with the same names and the solved
values:
s.c,s.m,s.r.
Conditions
The conditions are in the prelude, the part of the
built-in functions written in knotflow itself (chapter 3); you can read
there how each is computed. They are the ones CAD programs offer in
sketches and assemblies. Each is a number (or a point, a list: then
several equations) that becomes 0 when the condition holds. Points are
(x, y) in a sketch or (x, y, z) in space; a
line is given by two of its points, a circle by its centre and
radius.
Where things are
| holds when … | |
|---|---|
coincident(p, q) |
p and q coincide (also: a point stays where it is:
coincident(p, (3, 4))) |
on_line(p, a, b) |
p lies on the line through a and b (sketch) |
on_line_in_space(p, a, b) |
the same in space |
on_circle(p, m, r) |
p lies on the circle |
on_curve(p, c, t) |
p lies on the curve c at its parameter t (an unknown of its own, 0 … 1) |
midpoint(p, a, b) |
p lies in the middle of a and b |
symmetric(p, q, a, b) |
p and q are mirror images across the line through a and b |
symmetric_about(p, q, m) |
p and q are mirror images through the point m |
concentric(m1, m2) |
two circles have the same centre |
Which way lines run (sketch)
| holds when … | |
|---|---|
horizontal(a, b), vertical(a, b) |
a–b is horizontal or vertical |
parallel(a, b, c, d) |
a–b and c–d are parallel |
perpendicular(a, b, c, d) |
a–b and c–d are at right angles |
collinear(a, b, c, d) |
c–d lies on the line through a and b |
Touching
| holds when … | |
|---|---|
touches(m, r, a, b) |
the circle touches the line through a and b |
touches_outside(m1, r1, m2, r2) |
two circles touch from outside |
touches_inside(m1, r1, m2, r2) |
one circle touches the other from inside |
touches_curve(m, r, c, t) |
the circle touches the curve c at its parameter t |
tangent_to_curve(a, b, c, t) |
the line a–b touches the curve at its parameter t (a tangent) |
normal_to_curve(a, b, c, t) |
the line a–b crosses the curve at right angles at its parameter t |
Sizes (… - value means: should have
this value)
| holds when … | |
|---|---|
apart(p, q, d), length_is(a, b, d) |
p and q are d apart; the line is d long |
horizontal_distance(p, q, d),
vertical_distance(p, q, d) |
q lies d to the right of or above p |
apart_from_line(p, a, b, d) |
p is d away from the line (distance_to_line
measures) |
equal_length(a, b, c, d) |
a–b and c–d are equally long |
equal(x, y) |
two numbers are equal (equal radii:
equal(v.r1, v.r2)) |
angle_at(p, q, r) - w |
the angle at q (between q→p and q→r) is w degrees (0 … 180) |
angle_is(a, b, c, d, w) |
c–d is turned w degrees from a–b, counter-clockwise, also at 180°
(angle_from(a, b, c, d) measures it) |
In space
| holds when … | |
|---|---|
on_plane(p, o, n) |
p lies on the plane through o with normal n |
apart_from_plane(p, o, n, d) |
p lies d above the plane (on the side of n) |
on_surface(p, s, u, v) |
p lies on the surface s at its parameters u, v (unknowns of their own) |
pierce(c, t, o, n) |
the curve c pierces the plane at its parameter t |
Conditions of your own are small functions in the same pattern, for
example "the circle passes through the point p":
fn through(m, r, p) = distance(m, p) - r. What matters is a
sign: the number should pass through 0 at the solution.
If it only touches 0 (a double root, as in
(distance(m, p) - r) ^ 2), the equation does hold, but its
derivative is 0 there: solve gets only roughly there (r =
9.9997 instead of 10). A distance that is never negative
(distance_to_line(p, a, b) instead of on_line)
has a kink at the solution, and solve cannot read there
what it fixes.
Putting parts together
Assemblies are constructions too. A part's placement is a record
{at: (x, y, z), turn: (x, y, z)}: moved by at,
turned about the direction of turn by its length in degrees
(turned(v, w) turns a vector that way). That is six
unknowns; the turn is given between −180° and 180° (at 360° the
direction of the axis would not matter). A connector
(connector(origin, z, x) or
frame_on(surface, u, v)) is a frame (chapter 5) on the
part; placed(connector, pose) puts it where the part is.
The mates take two connectors and leave as much movement as in CAD
programs:
| Mate | what stays free |
|---|---|
fastened(c1, c2) |
nothing: fixed together |
revolute(c1, c2) |
turning about the z axis (a hinge) |
slider(c1, c2) |
sliding along z |
cylindrical(c1, c2) |
turning about and sliding along z |
pin_slot(c1, c2) |
turning about z and sliding along x (a pin in a slot) |
planar(c1, c2) |
sliding in the xy plane and turning about z |
ball_joint(c1, c2) |
turning in every direction (a ball joint) |
parallel_mate(c1, c2) |
everything but tilting the z axis (aligned(c1, c2): the
z axes parallel) |
The mates say that axes are parallel, not which way they point: a
part can also fit turned half round. Which position is meant, the start
says, like "flip" in CAD programs. The unknowns are numbers and points;
with two parts, the placements are called, say, a_at,
a_turn, b_at, b_turn, and
{at: v.a_at, turn: v.a_turn} puts one together.
let hinge = connector((0, 0, 0), (0, 0, 1), (1, 0, 0)) // on the fixed part
let pivot = connector((0, 0, 0), (0, 0, 1), (1, 0, 0)) // on the lever
fn tip(pose) = placed_point((50, 0, 0), pose) // the end of the lever
let lever = solve({at: (0, 0, 0), turn: (0, 0, 20)}, v => [
revolute(hinge, placed(pivot, v)),
on_line(tip(v), (0, 0), (1, 1))]) // points at 45°
Without the second condition, solve warns: one degree of
freedom, the hinge can still turn. A degree of freedom
is a direction in which the construction can still move without breaking
a condition. As in a sketch, the warning says what is missing.
The example linkage is a four-bar
linkage: four bars joined at four pivots. One is fixed (the
base), the crank turns all the way round, the
rocker swings to and fro, and the
coupler joins the two. Two lengths decide where the
coupler and the rocker meet. Drag the crank and the linkage follows, and
a corner of the coupler traces a closed path, the coupler
curve. Linkages like this turn a rotation into another
movement, such as the to and fro of a windscreen wiper.

Measuring: volume and
area
volume(shape) gives the volume of a solid in mm³ (1 ml =
1000 mm³), area(shape) the area in mm². Both measure what
export would draw:
volume |
area |
|
|---|---|---|
a closed curve in one plane (circle(5), a polygon) that
does not cross itself |
– | the area it encloses |
a surface closed around (revolve, sphere,
extrude or loft of closed sections) |
with a flat face on each open end | the surface and those faces |
an open surface (extrude(line(…), h)) |
– (error) | the surface alone |
a solid of faces,
union/difference/intersection, a
mesh |
from its triangles | from its triangles |
| copies whose boxes do not meet | each piece measured once, times the count | likewise |
A curve and a single surface are measured on the exact geometry, to
rounding. The rest comes from triangles, finer than for printing: within
about 0.1 %, curved faces a little too small – as finely as the solid is
thick on average, so also for a long thin rod or parts far apart. A list
of shapes is a group whose parts may overlap; measure
union(a, b) or add up the parts. Copies
(instance) are such a group too: they are added up only
where the boxes around them do not meet (a row of pegs). Then each piece
is measured once and counted with what each place makes of it (a frame
twice as long: eight times the volume, four times the area) – 400 balls
as fast and as exactly as one; otherwise volume and
area stop with “the copies may overlap near …” – a ring of
turned copies whose boxes meet included, although the copies themselves
may not touch; then measure the union of realize(copies). A
curve that crosses itself (a figure eight) encloses no one area and
stops with “the curve crosses itself near …”.
Because the exact measure changes smoothly with the shape,
solve can take it as a condition. A cup that holds 250 ml:
the inside is drawn with its height unknown, and the wall goes outside
(thicken with a negative wall, chapter 6), so the inside
stays as measured.
param ml = 250 in 100..500 "capacity [ml]"
param wall = 2 in 0.8..5 "wall thickness [mm]"
// the inside of the cup, from the axis along the floor and up the wall; its height h is unknown
fn inside(h) = chain([line((0, 0), (30, 0)), line((30, 0), (36, h))])
let s = solve({h: 80}, v => [volume(revolve(inside(v.h))) - ml * 1000])
let height = s.h
export revolve(thicken(inside(height), -wall)) color "terracotta"
The inside's top end gets a flat face, the level of a full cup: 72.9
mm high for 250 ml. Ask for the volume of something measured on
triangles (a difference, a shell) in
solve and the equation never quite holds: its triangles
change with the shape in steps.
What solve reports
- Not fully determined: a condition is missing, and
the figure can still move. The warning names what can move.
solvetakes a solution near the start. - Twice: a condition follows from others (the three angles of a triangle). The warning names the equations that say the same thing (counted as in the list), and how many are too many.
- Contradictory: not everything can hold at once. That is an error naming the equations that do not come out, and by how much.
solve takes at most 300 unknowns and 1000 equations. The
exact texts of the messages are in chapter 14.
Tips
- Starting values: a construction often has several
solutions (the angle up or down, the circle inside or outside).
solvefinds one near the start, mostly the nearest. So draw the start roughly right, also for curve parameters t and surface parameters u, v. - Sliders: a
paramin a condition turns the whole construction into a slider. - Counting equations: unknowns count one by one (a point in the plane: 2, a circle: 3). A construction is exactly determined when it has as many independent conditions as unknowns; each one missing leaves a degree of freedom.
The example construction builds a rounded triangular
plate from this with a hole in the inscribed circle. How the solver
works is in Lesson
3.11.