summaryrefslogtreecommitdiff
path: root/examples/noc/src/the_wave.zig
blob: 86ce10d79c0efd1453b59c146670e38a944fb3d1 (plain) (blame)
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
//! Nature of Code, Example 3.9: The Wave.
const std = @import("std");
const sk = @import("sketch");

pub const name = "the_wave";
pub const width = 640;
pub const height = 360;

pub const State = struct {
    start_angle: f32 = 0,
    angle_velocity: f32 = 0.2,
    clock: sk.Clock = .{},
};

pub fn update(s: *State, in: *const sk.Input) void {
    for (0..s.clock.steps(in)) |_| s.start_angle += 0.02;
}

const pen: sk.draw.Pen = .{ .fill = sk.Color.gray(127).alpha(127), .stroke = .black, .width = 2 };

pub fn draw(s: *const State, f: *sk.Frame) void {
    f.clear(sk.Color.white);
    // Processing advances startAngle after reading it; State already has.
    var angle = s.start_angle - 0.02;
    var x: f32 = 0;
    while (x <= width) : (x += 24) {
        const y = std.math.lerp(0, height, (@sin(angle) + 1) / 2);
        sk.draw.circle(f, .{ x, y }, 24, pen);
        angle += s.angle_velocity;
    }
}