42 lines
1.2 KiB
Text
42 lines
1.2 KiB
Text
extern crate image;
|
|
extern crate rand;
|
|
|
|
use rand::prelude::*;
|
|
use std::f32;
|
|
|
|
fn main() {
|
|
let max_iterations = 50_000;
|
|
let img_side = 800;
|
|
let tri_size = 400.0;
|
|
|
|
// Create a new ImgBuf
|
|
let mut imgbuf = image::ImageBuffer::new(img_side, img_side);
|
|
|
|
// Create triangle vertices
|
|
let mut vertices: [[f32; 2]; 3] = [[0.0, 0.0]; 3];
|
|
for i in 0..vertices.len() {
|
|
vertices[i][0] = (img_side as f32 / 2.0)
|
|
+ (tri_size / 2.0) * (f32::consts::PI * i as f32 * 2.0 / 3.0).cos();
|
|
vertices[i][1] = (img_side as f32 / 2.0)
|
|
+ (tri_size / 2.0) * (f32::consts::PI * i as f32 * 2.0 / 3.0).sin();
|
|
}
|
|
for v in &vertices {
|
|
imgbuf.put_pixel(v[0] as u32, v[1] as u32, image::Luma([255u8]));
|
|
}
|
|
println!("Verticies: {:?}", vertices);
|
|
|
|
// Iterate chaos game
|
|
let mut rng = rand::thread_rng();
|
|
let mut x = img_side as f32 / 2.0;
|
|
let mut y = img_side as f32 / 2.0;
|
|
for _ in 0..max_iterations {
|
|
let choice = rng.gen_range(0..vertices.len());
|
|
x = (x + vertices[choice][0]) / 2.0;
|
|
y = (y + vertices[choice][1]) / 2.0;
|
|
|
|
imgbuf.put_pixel(x as u32, y as u32, image::Luma([255u8]));
|
|
}
|
|
|
|
// Save image
|
|
imgbuf.save("fractal.png").unwrap();
|
|
}
|