knotflow Handbook Deutsch Lessons Modeller ↗ Source

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

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

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

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

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]]

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"

Seven posts at points of a curve, below them a plate fitted with bounds

Two things worth knowing:

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.