-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
draw_polyline.rs
49 lines (42 loc) · 1.43 KB
/
draw_polyline.rs
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
use nannou::prelude::*;
fn main() {
nannou::sketch(view).run()
}
fn view(app: &App, frame: Frame) {
// Begin drawing
let draw = app.draw();
// Clear the background.
draw.background().color(BLACK);
let win = app.window_rect();
let t = app.time;
// Decide on a number of points and a weight.
let n_points = 10;
let weight = 8.0;
let hz = ((app.mouse.x + win.right()) / win.w()).powi(4) * 1000.0;
let vertices = (0..n_points)
// A sine wave mapped to the range of the window.
.map(|i| {
let x = map_range(i, 0, n_points - 1, win.left(), win.right());
let fract = i as f32 / n_points as f32;
let amp = (t + fract * hz * TAU).sin();
let y = map_range(amp, -1.0, 1.0, win.bottom() * 0.75, win.top() * 0.75);
pt2(x, y)
})
.enumerate()
// Colour each vertex uniquely based on its index.
.map(|(i, p)| {
let fract = i as f32 / n_points as f32;
let r = (t + fract) % 1.0;
let g = (t + 1.0 - fract) % 1.0;
let b = (t + 0.5 + fract) % 1.0;
let rgba = srgba(r, g, b, 1.0);
(p, rgba)
});
// Draw the polyline as a stroked path.
draw.polyline()
.weight(weight)
.join_round()
.points_colored(vertices);
// Write the result of our drawing to the window's frame.
draw.to_frame(app, &frame).unwrap();
}