-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathIFS.rs
36 lines (29 loc) · 814 Bytes
/
IFS.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
use rand::*;
#[derive(Clone, Copy)]
struct Point {
x: f64,
y: f64,
}
fn chaos_game(iters: usize, shapes: Vec<Point>) -> Vec<Point> {
let mut rng = rand::thread_rng();
let mut p = Point{x: rng.gen(), y: rng.gen()};
(0..iters).into_iter().map(|_| {
let old_point = p;
let tmp = shapes[rng.gen_range(0..shapes.len())];
p.x = 0.5 * (p.x + tmp.x);
p.y = 0.5 * (p.y + tmp.y);
old_point
}).collect()
}
fn main() {
let shapes = vec![
Point{x: 0., y: 0.},
Point{x: 0.5, y: 0.75_f64.sqrt()},
Point{x: 1., y: 0.},
];
let mut out = String::new();
for point in chaos_game(10_000, shapes) {
out += format!("{}\t{}\n", point.x, point.y).as_str();
}
std::fs::write("./sierpinski.dat", out).unwrap();
}