forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rust version of IFS (algorithm-archivists#755)
* add the rust version of the ifs * Apply suggestions from code review Co-authored-by: Dimitri Belopopsky <[email protected]> Co-authored-by: stormofice <[email protected]> * add a cargo toml Co-authored-by: Dimitri Belopopsky <[email protected]> Co-authored-by: stormofice <[email protected]>
- Loading branch information
1 parent
fd6b7a6
commit 51936d7
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "rust" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
rand = "0.8.4" | ||
|
||
[[bin]] | ||
path = "./IFS.rs" | ||
name = "main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} |