From 51936d71e83e993a6bfe39056d5f54890f7def36 Mon Sep 17 00:00:00 2001 From: Julian Date: Tue, 28 Dec 2021 09:48:27 +0100 Subject: [PATCH] Rust version of IFS (#755) * add the rust version of the ifs * Apply suggestions from code review Co-authored-by: Dimitri Belopopsky Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com> * add a cargo toml Co-authored-by: Dimitri Belopopsky Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com> --- contents/IFS/IFS.md | 4 ++++ contents/IFS/code/rust/Cargo.toml | 13 +++++++++++ contents/IFS/code/rust/IFS.rs | 36 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 contents/IFS/code/rust/Cargo.toml create mode 100644 contents/IFS/code/rust/IFS.rs diff --git a/contents/IFS/IFS.md b/contents/IFS/IFS.md index f7d017e36..8ba1fb10c 100644 --- a/contents/IFS/IFS.md +++ b/contents/IFS/IFS.md @@ -144,6 +144,8 @@ Here, instead of tracking children of children, we track a single individual tha [import:5-14, lang:"lisp"](code/clisp/ifs.lisp) {% sample lang="coco" %} [import:4-16, lang:"coconut"](code/coconut/IFS.coco) +{% sample lang="rust" %} +[import:9-20, lang:"rust"](code/rust/IFS.rs) {% sample lang="java" %} [import:16-39, lang:"java"](code/java/IFS.java) {% sample lang="ps1" %} @@ -232,6 +234,8 @@ In addition, we have written the chaos game code to take in a set of points so t [import, lang:"lisp"](code/clisp/ifs.lisp) {%sample lang="coco" %} [import, lang:"coconut"](code/coconut/IFS.coco) +{%sample lang="rust" %} +[import, lang:"rust"](code/rust/IFS.rs) {%sample lang="java" %} [import, lang:"java"](code/java/IFS.java) {% sample lang="ps1" %} 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 diff --git a/contents/IFS/code/rust/IFS.rs b/contents/IFS/code/rust/IFS.rs new file mode 100644 index 000000000..476565f0f --- /dev/null +++ b/contents/IFS/code/rust/IFS.rs @@ -0,0 +1,36 @@ +use rand::*; + +#[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_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(); +} \ No newline at end of file