main
annieversary 2021-08-18 23:31:17 +02:00
parent ec6da840be
commit 808fc8e90b
4 changed files with 166 additions and 0 deletions

8
Cargo.lock generated
View File

@ -2372,6 +2372,14 @@ dependencies = [
"utils",
]
[[package]]
name = "subtitled5"
version = "0.1.0"
dependencies = [
"nannou",
"utils",
]
[[package]]
name = "syn"
version = "1.0.74"

View File

@ -0,0 +1,8 @@
[package]
name = "subtitled5"
version = "0.1.0"
edition = "2018"
[dependencies]
nannou = "0.17"
utils = { path = "../utils" }

View File

@ -0,0 +1,44 @@
use nannou::prelude::*;
mod shapes;
use shapes::*;
fn main() {
nannou::app(model).update(update).simple_window(view).run();
}
struct Model {
shape: Shapes,
}
fn model(_app: &App) -> Model {
Model {
shape: Shapes::new(),
}
}
fn update(app: &App, model: &mut Model, _update: Update) {
if app.elapsed_frames() % 60 == 0 {
model.shape = Shapes::new();
}
}
fn view(app: &App, model: &Model, frame: Frame) {
let _t = frame.nth() as f32 / 60.0;
let draw = app.draw();
if frame.nth() == 0 {
draw.background().color(BLACK);
} else if frame.nth() % 60 > 40 {
let win = app.window_rect();
draw.rect().wh(win.wh()).color(srgba(0.0, 0.0, 0.0, 0.2));
}
if frame.nth() % 60 == 0 {
model.shape.draw(draw.clone());
}
draw.to_frame(app, &frame).unwrap();
utils::record::record(app, &frame);
}

View File

@ -0,0 +1,106 @@
use nannou::prelude::*;
pub struct Shapes(Vec<Shape>);
impl Shapes {
pub fn draw(&self, draw: Draw) {
for shape in &self.0 {
match shape {
Shape::Triangle { center, radius } => {
let points = vec![vec2(0., 1.), vec2(0.866, -0.5), vec2(-0.866, -0.5)]
.into_iter()
.map(|v| v * *radius + *center)
.collect::<Vec<_>>();
draw.polyline().weight(3.).points_closed(points).color(PINK);
}
Shape::Circle { center, radius } => {
draw.ellipse()
.radius(*radius)
.no_fill()
.stroke_weight(3.)
.xy(*center)
.stroke_color(PINK);
}
Shape::Line { center, radius } => {}
}
}
}
pub fn new() -> Self {
let root = Shape::random(Vec2::ZERO, 100.0);
let mut vec = vec![];
for _ in 0..3 {
let (c, r) = root.get_random_point_distance();
let s = Shape::random(c, r);
let children = random_range(0, 3);
for _ in 0..children {
let (c, r) = s.get_random_point_distance();
let s = Shape::random(c, r);
vec.push(s);
}
vec.push(s);
}
vec.push(root);
Self(vec)
}
}
pub enum Shape {
Triangle { center: Vec2, radius: f32 },
Circle { center: Vec2, radius: f32 },
Line { center: Vec2, radius: f32 },
}
impl Shape {
fn random(center: Vec2, radius: f32) -> Self {
match random_range(0, 3) {
0 => Self::tri(center, radius),
1 => Self::line(center, radius),
_ => Self::circ(center, radius),
}
}
fn tri(center: Vec2, radius: f32) -> Self {
Self::Triangle { center, radius }
}
fn circ(center: Vec2, radius: f32) -> Self {
Self::Circle { center, radius }
}
fn line(center: Vec2, radius: f32) -> Self {
Self::Line { center, radius }
}
fn get_random_point_distance(&self) -> (Vec2, f32) {
match self {
Shape::Triangle { center, radius } => {
let points = vec![vec2(0., 1.), vec2(0.866, -0.5), vec2(-0.866, -0.5)]
.into_iter()
.map(|v| v * *radius + *center)
.collect::<Vec<_>>();
let denom = random_range(1.0, 5.0).floor();
(points[random_range(0, points.len())], radius / denom)
}
Shape::Circle { center, radius } => {
let point = match random_range(0, 4) {
0 => *center + Vec2::X * *radius,
1 => *center - Vec2::X * *radius,
2 => *center + Vec2::Y * *radius,
_ => *center - Vec2::Y * *radius,
};
let denom = random_range(1.0, 5.0).floor();
(point, radius / denom)
}
Shape::Line { center, radius } => {
let point = todo!("one of the two vertices");
let denom = random_range(1.0, 5.0).floor();
(point, radius / denom)
}
}
}
}