-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
draw_polygon.rs
54 lines (48 loc) · 1.43 KB
/
draw_polygon.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
50
51
52
53
54
use nannou::prelude::*;
fn main() {
nannou::sketch(view).run()
}
fn view(app: &App, frame: Frame) {
// Begin drawing
let win = app.window_rect();
let t = app.time;
let draw = app.draw();
// Clear the background to black.
draw.background().color(BLACK);
// Create an `ngon` of points.
let n_points = 5;
let radius = win.w().min(win.h()) * 0.25;
let points = (0..n_points).map(|i| {
let fract = i as f32 / n_points as f32;
let phase = fract;
let x = radius * (TAU * phase).cos();
let y = radius * (TAU * phase).sin();
pt2(x, y)
});
draw.polygon()
.x(-win.w() * 0.25)
.color(WHITE)
.rotate(-t * 0.1)
.stroke(PINK)
.stroke_weight(20.0)
.join_round()
.points(points);
// Do the same, but give each point a unique colour.
let n_points = 7;
let points_colored = (0..n_points).map(|i| {
let fract = i as f32 / n_points as f32;
let phase = fract;
let x = radius * (TAU * phase).cos();
let y = radius * (TAU * phase).sin();
let r = fract;
let g = 1.0 - fract;
let b = (0.5 + fract) % 1.0;
(pt2(x, y), rgb(r, g, b))
});
draw.polygon()
.x(win.w() * 0.25)
.rotate(t * 0.2)
.points_colored(points_colored);
// Write the result of our drawing to the window's frame.
draw.to_frame(app, &frame).unwrap();
}