-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.rs
178 lines (156 loc) · 3.75 KB
/
main.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use clap::Parser;
use instant::Instant;
use livedraw::*;
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::{f64::consts::PI, time::Duration};
use svg::node::element::path::Data;
// TODO rotate to be delta rotation
// TODO replace xy by just a head angle
// TODO angle component
#[derive(Debug, Parser, Clone, Copy)]
#[clap()]
struct Args {
#[clap(long, default_value_t = 0.0)]
seed: f64,
#[clap(long, default_value_t = 105.0)]
width: f64,
#[clap(long, default_value_t = 148.5)]
height: f64,
#[clap(long, default_value_t = 5.0)]
padding: f64,
#[clap(long)]
simulation: bool,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
struct RangeValue {
value: f64,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
struct ArtInput {
speed: RangeValue,
scale: RangeValue,
rotate: RangeValue,
}
#[derive(Clone)]
struct Art {
args: Args,
start_time: Instant,
pos: (f64, f64),
ang: f64,
}
impl Art {
fn new(args: Args) -> Self {
Art {
args,
start_time: Instant::now(),
pos: (args.width / 2.0, args.height / 3.0),
ang: PI / 2.0,
}
}
}
impl LivedrawArt for Art {
fn get_dimension(&self) -> (f64, f64) {
(self.args.width, self.args.height)
}
fn estimate_total_increments(&self) -> usize {
500
}
fn actions_before_increment(&self, i: usize) -> Vec<ArtAction> {
if i == 0 {
return vec![ArtAction::Pause(
String::from("Get ready for 20 minutes!"),
20.0,
)];
}
return vec![];
}
fn get_predictive_max_next_increments(&self) -> Option<usize> {
Some(40)
}
fn draw_increment(&mut self, value: &Value, index: usize) -> ArtIncrement {
if self.start_time.elapsed().as_secs() > 20 * 60 || index > 500 {
return ArtIncrement::End;
}
let input: ArtInput = serde_json::from_value(value.clone()).unwrap();
let r = input.scale.value;
let p = r + self.args.padding;
self.ang += input.rotate.value;
self.pos = (
repeat_between(
p,
self.args.width - p,
self.pos.0 + input.speed.value * self.ang.cos(),
),
repeat_between(
p,
self.args.height - p,
self.pos.1 + input.speed.value * self.ang.sin(),
),
);
let routes = vec![circle_route(self.pos, r, 4, self.ang)];
if routes.len() == 0 {
return ArtIncrement::Continue;
}
let data = routes.iter().fold(Data::new(), livedraw::render_route);
let layers =
vec![svg_layer("black").add(svg_base_path("black", 0.35, data))];
return ArtIncrement::SVG(layers);
}
}
impl LivedrawArtSimulation for Art {
fn simulate_input(&mut self, _index: usize) -> Value {
let mut rng = rand::thread_rng();
return json!(ArtInput {
speed: RangeValue {
value: rng.gen_range(0.0, 20.0)
},
scale: RangeValue {
value: rng.gen_range(1.0, 20.0)
},
rotate: RangeValue {
value: rng.gen_range(-1.0, 1.0)
},
});
}
}
fn main() {
let args = Args::parse();
println!("{:#?}", args);
let mut art = Art::new(args.clone());
if args.simulation {
livedraw_start_simulation(&mut art);
} else {
livedraw_start(&mut art);
}
}
fn circle_route(
center: (f64, f64),
r: f64,
count: usize,
rotation: f64,
) -> Vec<(f64, f64)> {
let mut route = Vec::new();
for i in 0..(count + 1) {
let a = rotation + 2. * PI * i as f64 / (count as f64);
let x = center.0 + r * a.cos();
let y = center.1 + r * a.sin();
route.push((x, y));
}
return route;
}
fn repeat_between(min: f64, max: f64, value: f64) -> f64 {
let range = max - min;
if range <= 0.0 {
return value;
}
let mut result = value;
while result < min {
result += range;
}
while result > max {
result -= range;
}
result
}