From 85c866cfb76d2b031a83225dc39d269d2bc7bf45 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 30 Sep 2020 21:03:29 +0200 Subject: [PATCH 1/3] add the rust version of the ifs --- contents/IFS/IFS.md | 4 ++++ contents/IFS/code/rust/IFS.rs | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 contents/IFS/code/rust/IFS.rs diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index 8b7805ff3..d22cba1e4 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -136,6 +136,8 @@ Here, instead of tracking children of children, we track a single individual tha [import:18-29, lang:"c"](code/c/IFS.c) {% sample lang="coco" %} [import:4-16, lang:"coconut"](code/coconut/IFS.coco) +{% sample lang="rust" %} +[import:9-20, lang:"rust"](code/rust/IFS.rs) {% endmethod %} If we set the initial points to the on the equilateral triangle we saw before, we can see the Sierpinski triangle again after a few thousand iterations, as shown below: @@ -207,6 +209,8 @@ In addition, we have written the chaos game code to take in a set of points so t [import, lang:"c"](code/c/IFS.c) {%sample lang="coco" %} [import, lang:"coconut"](code/coconut/IFS.coco) +{%sample lang="rust" %} +[import, lang:"rust"](code/rust/IFS.rs) {% endmethod %} ### Bibliography diff --git a/contents/IFS/code/rust/IFS.rs b/contents/IFS/code/rust/IFS.rs new file mode 100644 index 000000000..0e1db0249 --- /dev/null +++ b/contents/IFS/code/rust/IFS.rs @@ -0,0 +1,36 @@ +use rand::prelude::*; + +#[derive(Clone, Copy)] +struct Point { + x: f64, + y: f64, +} + +fn chaos_game(iters: usize, shapes: Vec) -> Vec { + 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 as 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("./out.dat", out).unwrap(); +} \ No newline at end of file From d09a528d1a4dcf037a7eb5292f9c861c9f0bb611 Mon Sep 17 00:00:00 2001 From: Julian Date: Tue, 28 Dec 2021 01:26:00 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Dimitri Belopopsky Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com> --- contents/IFS/code/rust/IFS.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/IFS/code/rust/IFS.rs b/contents/IFS/code/rust/IFS.rs index 0e1db0249..476565f0f 100644 --- a/contents/IFS/code/rust/IFS.rs +++ b/contents/IFS/code/rust/IFS.rs @@ -1,4 +1,4 @@ -use rand::prelude::*; +use rand::*; #[derive(Clone, Copy)] struct Point { @@ -12,7 +12,7 @@ fn chaos_game(iters: usize, shapes: Vec) -> Vec { (0..iters).into_iter().map(|_| { let old_point = p; - let tmp = shapes[rng.gen_range(0, shapes.len())]; + 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 @@ -22,7 +22,7 @@ fn chaos_game(iters: usize, shapes: Vec) -> Vec { fn main() { let shapes = vec![ Point{x: 0., y: 0.}, - Point{x: 0.5, y: (0.75 as f64).sqrt()}, + Point{x: 0.5, y: 0.75_f64.sqrt()}, Point{x: 1., y: 0.}, ]; @@ -32,5 +32,5 @@ fn main() { out += format!("{}\t{}\n", point.x, point.y).as_str(); } - std::fs::write("./out.dat", out).unwrap(); + std::fs::write("./sierpinski.dat", out).unwrap(); } \ No newline at end of file From 6fde71c60eab2ffe2cb3fa0be2c13b29a7bde410 Mon Sep 17 00:00:00 2001 From: Julian Date: Tue, 28 Dec 2021 01:30:57 +0100 Subject: [PATCH 3/3] add a cargo toml --- contents/IFS/code/rust/Cargo.toml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 contents/IFS/code/rust/Cargo.toml diff --git a/contents/IFS/code/rust/Cargo.toml b/contents/IFS/code/rust/Cargo.toml new file mode 100644 index 000000000..90fbca22d --- /dev/null +++ b/contents/IFS/code/rust/Cargo.toml @@ -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" \ No newline at end of file