1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
//! Nature of Code, Example 8.5: the Koch Curve. Each click applies the
//! rules once more: four segments for every one (after 8, back to 0).
const std = @import("std");
const sk = @import("sketch");
const vec = sk.math;
pub const name = "koch";
pub const width = 640;
pub const height = 360;
const max_generation = 8;
/// The whole curve is a function of how often the rules ran, so that is
/// all State keeps: one byte, however many segments it means (65536 at
/// generation 8, 1 MiB as points). Snapshots, replays and seeks stay cheap.
pub const State = struct {
generation: u8 = 5,
};
/// The segments, and their picture, derived from the generation in
/// `draw` and kept here between frames. `Derived` rebuilds them when the
/// generation they were made for is not State's (a click, a seek, a
/// replay) and after a reload.
pub const Session = struct {
curve: sk.Derived(Curve) = .{ .value = .{} },
};
const Segment = struct { start: vec.Vec, end: vec.Vec };
const Curve = struct {
segments: std.ArrayList(Segment) = .empty,
picture: sk.draw.Tessellation = .empty,
};
pub fn update(s: *State, in: *const sk.Input) void {
if (in.mousePressed(.left) != null) s.generation = if (s.generation == max_generation) 0 else s.generation + 1;
}
/// The book's `generate()`: every segment becomes four, the middle two
/// raised into a peak.
fn generate(gpa: std.mem.Allocator, segments: *std.ArrayList(Segment)) !void {
var next: std.ArrayList(Segment) = try .initCapacity(gpa, segments.items.len * 4);
for (segments.items) |line| {
const v = vec.scale(line.end - line.start, 1.0 / 3.0);
const b = line.start + v;
const d = b + v;
// Rotated by -60°, so the peak points up.
const c = b + vec.Affine.rotate(-std.math.pi / 3.0).applyVector(v);
for ([_]vec.Vec{ line.start, b, c, d }, [_]vec.Vec{ b, c, d, line.end }) |from, to| next.appendAssumeCapacity(.{ .start = from, .end = to });
}
segments.deinit(gpa);
segments.* = next;
}
fn build(curve: *Curve, gpa: std.mem.Allocator, generation: u8) !void {
curve.segments.clearRetainingCapacity();
try curve.segments.append(gpa, .{ .start = .{ 0, 300 }, .end = .{ width, 300 } });
for (0..generation) |_| try generate(gpa, &curve.segments);
// The segments join end to start: one line through their points, 2 px
// wide with round joins and ends, looks like the book's separate
// round-capped lines and draws each pixel once.
const points = try gpa.alloc(vec.Vec, curve.segments.items.len + 1);
defer gpa.free(points);
for (curve.segments.items, points[0..curve.segments.items.len]) |line, *p| p.* = line.start;
points[points.len - 1] = curve.segments.items[curve.segments.items.len - 1].end;
curve.picture.clearRetainingCapacity();
try curve.picture.strokePath(gpa, points, .{ .width = 2, .join = .round, .cap = .round }, .identity);
}
pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
f.clear(sk.Color.white);
if (ss.curve.stale(s.generation, f.in)) build(&ss.curve.value, f.session, s.generation) catch {
ss.curve.forget();
f.host.log("out of session memory: curve dropped");
return;
};
f.shape(&ss.curve.value.picture, .black);
f.print(8, 8, 2, sk.Color.gray(100), "generation {d}: {d} segments", .{ s.generation, ss.curve.value.segments.items.len });
}
|