Chapter 4
Computing: numbers, points, querying curves
Many measures are not written down but computed: how many holes fit
on a bar, at what angle a slope stands, where exactly on a curve a post
sits. This chapter shows the functions for that. All examples only
compute values; you see them in the browser under
Values or with kf render file.kf (chapter
3).
Numbers
let width = 185 // length of the bar [mm]
let spacing = 12 // distance between the holes [mm]
let count = floor(width / spacing) // 15 holes
let margin = (width - (count - 1) * spacing) / 2 // 8.5 mm margin at each end
- Operators:
+ - * /,^for powers (2 ^ 10is 1024) and%for the remainder of a division.^binds more tightly than*and/, and these more tightly than+and-:1 + 2 * 3 ^ 2is 19. The remainder is never negative:-7 % 3is 2. That suits counting in a circle, for instance round the corners of a polygon:(i - 1) % nis the last corner, n - 1, for i = 0. - Rounding:
floor(down),ceil(up),round(to the nearest whole number). To two places:round(x * 100) / 100. - Limiting:
min(a, b),max(a, b),clamp(x, lo, hi).clamp(42, 10, 30)is 30. That is useful when a derived measure must not leave a sensible range, such as a radius that may be at most half the wall. - Also:
abs,sqrt,exp,log(natural logarithm) and the constantpi. Another logarithm comes fromlog(x) / log(10).
Every calculation gives a finite number. 1 / 0 or
sqrt(-1) is an error with its line, not "infinity" or "not
a number".
Angles
All angles are degrees, in these functions as in
rotate.
let s = sin(30) // 0.5
let slope = atan2(30, 40) // 36.87°: the slope, 30 high over 40 wide
let dir = atan2(-1, -1) // -135°: the point (-1, -1) seen from the centre
let back = asin(0.5) // 30
let tip = 25 * (cos(72), sin(72)) // one tip of a pentagon of radius 25
sin,cos,tantake an angle,asin,acos,atangive one back.atan2(y, x)gives the direction of the point (x, y) as an angle from -180 to 180, right in all four quadrants. For directions it is nearly always the better choice thanatan(y / x).- At the round angles the values are exact:
sin(30)is exactly 0.5,cos(90)exactly 0.
r * (cos(a), sin(a)) is the point at distance r and
angle a. The example star builds its star that way: outside
and inside by turns, the angles evenly spread.
Points and vectors
A point (x, y, z) is a vector at the same time: an arrow
from the origin to it, or the step from one point to another. Points
therefore calculate like numbers:
let p = (10, 0, 0)
let q = (10, 20, 5)
let mid = lerp(p, q, 0.5) // (10, 10, 2.5): the middle
let gap = distance(p, q) // 20.6
let ahead = p + 10 * normalize(q - p) // 10 mm from p towards q
let up = cross((1, 0, 0), (0, 1, 0)) // (0, 0, 1)
let angle = acos(dot(normalize((1, 0, 0)), normalize((1, 1, 0)))) // 45
q - pis the vector from p to q,length(v)its length;distance(p, q)is the same aslength(q - p).normalize(v)makes a vector of length 1: only the direction is left. Times a number it becomes a step of a given length, as inahead.lerp(a, b, t)goes from a (t = 0) to b (t = 1) on a straight line; it takes numbers as well as points.slerpgoes along the great circle instead, handy for directions and points on a sphere.dot(a, b)(dot product) says how far two directions agree: for vectors of length 1 it is the cosine of the angle between them, 0 means at right angles.cross(a, b)(cross product) gives a vector at right angles to both, for instance the normal of a plane spanned by two edges.
Read the coordinates with p.x, p.y,
p.z; a point in the plane has z = 0.
Random numbers
Random values in knotflow are repeatable: the same inputs give the same number. So a model looks the same the next time it is opened, and a slider does not reshuffle all the chance on the side.
param seed = 42 in 1..100 "chance"
let heights = [10 + 30 * rand(seed, i) | i <- range(0, 5)] // five heights from 10 to 40
let even = fibonacci_sphere(200) // 200 points, evenly over the sphere
let random = random_sphere(200, seed) // 200 points, at random on the sphere
rand(seed, i)gives a number from 0 to 1.seedpicks the sequence,ithe place in it. Aparam seedturns that into a slider "roll again".random_sphere(n, seed)scatters n points at random on the sphere of radius 1,random_sphere_point(seed, i)gives a single one.fibonacci_sphere(n)spreads n points as evenly as possible, without any chance;fibonacci_point(i, n)gives the i-th.
Times a radius and moved, they become spikes, studs or holes on a
ball. Random points on any surface are scattered by scatter
(chapter 10).
Sorting and grouping lists
let parts = [{name: "lid", h: 5}, {name: "body", h: 80}, {name: "foot", h: 12}]
let sizes = sort([30, 10, 20]) // [10, 20, 30]
let order = [p.name | p <- sort(parts, key: p => p.h)] // ["lid", "foot", "body"]
let groups = buckets([3, 14, 7, 22, 18, 1], 3, x => floor(x / 10)) // [[3, 7, 1], [14, 18], [22]]
sort(list)sorts numbers in ascending order. Withkeyit sorts any values by a number thatkeymakes of each.buckets(list, n, key)shares the elements out into n lists: x goes into list numberkey(x), from 0 to n - 1. The order within a list stays. That way, for instance, each row of a shelf gets its own list of parts.kind(value)says as a text what a value is:"number","point","curve","surface"… That helps when a function is to take different kinds of values.
Querying curves and surfaces
A curve runs from t = 0 (start) to t = 1 (end). With this t you query it:
let path = interpolate([(0, 0), (40, 30), (80, 0), (120, 20)])
let line = polyline([(0, 15), (120, 15)])
let start = at(path, 0) // (0, 0, 0)
let dir = tangent(path, 0.5) // the direction in the middle, length 1
let half = segment(path, 0, 0.5) // the first half as a curve of its own
let hits = crossings(path, line) // where the two cross: [{ta, tb, point}, …]
let area = bounds(path) // {min, max}: the box around the curve
let arc90 = arc_between((30, 0, 0), (0, 30, 0)) // a quarter circle around the origin
| function | gives |
|---|---|
at(curve, t) |
the point at t |
tangent(curve, t) |
the direction at t, length 1 |
segment(curve, from, to) |
the piece between two values of t, exactly |
crossings(a, b) |
where two curves drawn flat cross, with t on both and the point |
corners(curve) |
the corners of a flat curve: t, point and turning angle (positive: to the left) |
clockwise(curve) |
whether a closed flat curve runs clockwise |
bounds(shapes) |
the box {min, max} around points, curves, surfaces,
solids, meshes or copies (instance) |
arc_between(from, to, center) |
the exact circular arc between two points around a centre |
point_on(surface, u, v) |
the point of a surface at u, v (both 0 to 1) |
With these, parts are set on a curve and a plate is fitted underneath:
let path = interpolate([(0, 0), (40, 30), (80, 0), (120, 20)])
fn post(t) = cylinder(at(path, t), at(path, t) + (0, 0, 15), 1.5)
let area = bounds(path)
let size = area.max - area.min
let middle = (area.min + area.max) / 2
export [post(t) | t <- linspace(0, 1, 7)] color "terracotta"
export box(size.x + 10, size.y + 10, 2) |> move(x: middle.x, y: middle.y, z: -2) color "sage"

Two things worth knowing:
t does not measure length. Equal steps in t are mostly of different lengths on the curve; so the posts above do not stand exactly equally far apart. Equal distances come from
divide(curve, n)(chapter 10).A length is measured by cutting the curve into many short pieces and adding up their lengths:
fn curve_length(c, n = 200) = sum([distance(at(c, i / n), at(c, (i + 1) / n)) | i <- range(0, n)]) let around = curve_length(circle(10)) // 62.83, very nearly 2 π · 10More pieces make it more exact and slower; 200 are usually good to a hundredth of a millimetre.
For surfaces point_on(surface, u, v) gives a point,
frame_on(surface, u, v) a frame as well, on which parts can
be set (chapters 5 and 10).
Further reading
Why degrees are exact at the round angles and how knotflow deals with floating-point numbers: lesson 1.4. What t means on a curve: lesson 1.2.