spline v1
Smooth curves through points, as numbers. The curve mark smooths through its points by itself, with this library’s
through (the compiler brings it in), so a piece calls spline only when it wants the numbers: the segments of the
curve, to hand to curve(segments: ...) or to work on, or the same curve as plain points, for what takes points. It
draws nothing itself and rolls nothing, so it works on both targets.
use spline v1Names
| call | gives | |
|---|---|---|
spline.through(points, smooth: 1, closed: false) | vec[] | the segments of a smooth curve through points, for curve(segments: ...) |
spline.sample(points, steps: 8, smooth: 1, closed: false) | vec[] | the same curve as plain points, steps short straight pieces for each piece of it, for what takes points (polygon, polyline, a brush path) |
| value | ||
|---|---|---|
points | vec[] | the points the curve passes through, in order. Two or more; fewer gives the points as they are. |
smooth | num | how round the corners are. 0 draws straight lines, 1 is the usual smooth curve, a little more is looser. |
closed | bool | join the last point back to the first, and round that join too |
through gives a list of 1 + 3 * pieces points: the first point, then for each piece its two handles and its end.
curve(points, smooth: s, closed: c) draws exactly the curve of curve(segments: spline.through(points, s, c), closed: c). A closed list has one piece more, ending where it began; draw it with closed: true too, so that its
outline joins with no ends. An open curve through three points has two pieces and seven points.
sample leaves a closed curve’s first point off the end, so the list can go straight into a polygon; an open
curve keeps its last point.
Example
plate(1920, 2400)
use spline v1
random layout
background(#f3efe6)
let ring = vec[]
for i in 0 ..< 9 {
ring.add(vec(960, 1200) + polar(TAU * i / 9, layout.float(400 ..< 700)))
}
// a closed blob through the points: the curve mark smooths by itself
curve(ring, closed: true, fill: #c2452d, stroke: #1b1b1f, width: 6)
// the segments of a looser open curve, from the library, drawn over it
curve(segments: spline.through(ring, 1.3), stroke: #f3efe6, width: 10, opacity: 0.7, caps: "round")
// and the closed curve as a polygon of short steps
polygon(spline.sample(ring, 6, closed: true), stroke: #1b1b1f, width: 2)Notes
- Smooth curves need no library:
curve(points, closed: true, fill: ink). Nor do hand-written segments:curve(segments: [a, c1, c2, b], stroke: ink)is one piece fromatobwith handlesc1andc2. Usesplinewhen you want the numbers. - The corners are rounded with the same handle length at every point, so points spaced very unevenly can make a
piece overshoot. Space them out roughly evenly, or lower
smooth. - What it costs: about 340 bytes of shipped JavaScript for
through, which is also what a smoothed curve costs, and about 360 more forsample, which uses it. Only what the piece calls is shipped, and a piece that names spline v1 and smooths a curve carriesthroughonce.
The whole library, 51 lines
// spline v1: smooth curves through points.
//
// A curve's segments are a first point, then three points for each piece (its
// two handles, and where it ends). through works those out from points the
// curve passes through. The curve mark uses it itself, for curve(points, ...),
// so a piece calls it only to have the segments: curve(segments: ...). sample
// walks the same curve in short straight steps, for what takes plain points (a
// polygon, a brush path). Neither draws, and neither rolls.
// The segments of a smooth curve through points, for curve(segments: ...).
// smooth is how round the corners are: 0 makes straight lines, 1 the usual
// smooth curve, a little more a looser one. closed joins the last point back to
// the first and rounds the join too (the curve takes closed: true as well, to be
// drawn closed). Fewer than two points give the points as they are.
fn through(points: vec[], smooth: num = 1, closed: bool = false): vec[] {
let n = points.len
let out = vec[]
if n == 0 { return out }
out.add(points[0])
if n < 2 { return out }
let k = smooth / 6
let pieces = closed ? n : n - 1
for i in 0 ..< pieces {
// the points either side of this piece: ends of an open curve stand in for the ones that are missing
let before = points[closed ? (i + n - 1) % n : max(i - 1, 0)]
let from = points[i]
let to = points[closed ? (i + 1) % n : i + 1]
let after = points[closed ? (i + 2) % n : min(i + 2, n - 1)]
out.add(from + (to - before) * k)
out.add(to - (after - from) * k)
out.add(to)
}
return out
}
// The same curve as plain points: steps short straight pieces for each piece of
// the curve. A closed curve does not repeat its first point at the end.
fn sample(points: vec[], steps: int = 8, smooth: num = 1, closed: bool = false): vec[] {
let seg = through(points, smooth, closed)
let out = vec[]
if seg.len < 4 { return seg }
let pieces = floor((seg.len - 1) / 3)
for i in 0 ..< pieces {
let a = seg[3 * i], b = seg[3 * i + 1], c = seg[3 * i + 2], d = seg[3 * i + 3]
for s in 0 ..< steps {
let t = s / steps
let u = 1 - t
out.add(a * (u * u * u) + b * (3 * u * u * t) + c * (3 * u * t * t) + d * (t * t * t))
}
}
if !closed { out.add(seg[seg.len - 1]) }
return out
}