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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
//! Nature of Code, Example 8.9: an L-system, drawn by a turtle. Each click
//! rewrites the sentence once more (after 5, back to the axiom).
const std = @import("std");
const sk = @import("sketch");
const vec = sk.math;
pub const name = "lsystem";
pub const width = 640;
pub const height = 360;
const axiom = "F";
/// The book's one rule; every other character stays itself.
const rule = .{ 'F', "FF+[+F-F-F]-[-F+F+F]" };
const angle = std.math.degreesToRadians(25.0);
const max_generation = 5;
/// The sentence follows from the axiom, the rule and how often it ran:
/// State keeps only the count. The sentence itself grows eightfold a
/// generation (11 116 characters at 4, 88 940 at 5), which is Session's
/// business, rebuilt by `Derived` when the count changes.
pub const State = struct {
generation: u8 = 4,
};
pub const Session = struct {
tree: sk.Derived(Tree) = .{ .value = .{} },
};
const Tree = struct {
sentence: std.ArrayList(u8) = .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()`, `generation` times.
fn rewrite(gpa: std.mem.Allocator, sentence: *std.ArrayList(u8), generation: u8) !void {
sentence.clearRetainingCapacity();
try sentence.appendSlice(gpa, axiom);
var next: std.ArrayList(u8) = .empty;
defer next.deinit(gpa);
for (0..generation) |_| {
next.clearRetainingCapacity();
for (sentence.items) |c| {
if (c == rule[0]) try next.appendSlice(gpa, rule[1]) else try next.append(gpa, c);
}
std.mem.swap(std.ArrayList(u8), sentence, &next);
}
}
/// The turtle: `F` draws a step and moves along it, `+`/`-` turn, `[`
/// pushes and `]` pops. A push is a call with a copy of the transform, a
/// pop is its return: no stack to keep.
const Turtle = struct {
picture: *sk.draw.Tessellation,
gpa: std.mem.Allocator,
step: f32,
/// Walks `sentence` from `i.*` until its `]` (or the end), placed by `xf`.
fn walk(t: Turtle, sentence: []const u8, i: *usize, start: vec.Affine) !void {
var xf = start;
while (i.* < sentence.len) {
const c = sentence[i.*];
i.* += 1;
switch (c) {
'F', 'G' => {
try t.picture.strokeLine(t.gpa, .{ 0, 0 }, .{ 0, -t.step }, 1, xf);
xf = xf.mul(.translate(0, -t.step));
},
'+' => xf = xf.mul(.rotate(angle)),
'-' => xf = xf.mul(.rotate(-angle)),
'[' => try t.walk(sentence, i, xf),
']' => return,
else => {},
}
}
}
};
fn build(tree: *Tree, gpa: std.mem.Allocator, generation: u8) !void {
try rewrite(gpa, &tree.sentence, generation);
tree.picture.clearRetainingCapacity();
// Each generation doubles the trunk (F → FF), so the step halves to
// keep the tree the book's size: 6 px at generation 4.
const turtle: Turtle = .{ .picture = &tree.picture, .gpa = gpa, .step = 96 / std.math.pow(f32, 2, @floatFromInt(generation)) };
var i: usize = 0;
try turtle.walk(tree.sentence.items, &i, .translate(width / 2, height));
}
pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
f.clear(sk.Color.white);
if (ss.tree.stale(s.generation, f.in)) build(&ss.tree.value, f.session, s.generation) catch {
ss.tree.forget();
f.host.log("out of session memory: tree dropped");
return;
};
f.shape(&ss.tree.value.picture, .black);
f.print(8, height - 64, 2, sk.Color.gray(100), "generation {d}", .{s.generation});
f.print(8, height - 36, 2, sk.Color.gray(100), "{d} characters", .{ss.tree.value.sentence.items.len});
}
|