-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_fractal_distribution.rs
45 lines (39 loc) · 1.09 KB
/
01_fractal_distribution.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
/// Generate a fractal-like pattern using discrete distance field representation.
/// Simple impeative code style is shown.
use {
space_filling::{
geometry::{Shape, Circle},
sdf::{self, SDF},
solver::Argmax2D,
drawing::Draw,
util
},
anyhow::Result,
image::{Luma, Pixel, RgbaImage}
};
fn fractal_distribution(representation: &mut Argmax2D, image: &mut RgbaImage) {
representation.insert_sdf(sdf::boundary_rect);
for _ in 0..1000 {
let global_max = representation.find_max();
let circle = Circle
.translate(global_max.point.to_vector())
.scale(global_max.distance / 4.0);
representation.insert_sdf_domain(
util::domain_empirical(global_max),
|v| circle.sdf(v)
);
circle
.texture(Luma([255u8]).to_rgba())
.draw(image);
}
}
// profile: 158ms, 1000 circles, Δ = 2^-10
fn main() -> Result<()> {
let path = "out.png";
let mut representation = Argmax2D::new(1024, 16)?;
let mut image = RgbaImage::new(2048, 2048);
fractal_distribution(&mut representation, &mut image);
image.save(path)?;
open::that(path)?;
Ok(())
}