Skip to content

Commit 51936d7

Browse files
JulianShadowMitiastormofice
authored
Rust version of IFS (#755)
* add the rust version of the ifs * Apply suggestions from code review Co-authored-by: Dimitri Belopopsky <ShadowMitia@users.noreply.github.com> Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com> * add a cargo toml Co-authored-by: Dimitri Belopopsky <ShadowMitia@users.noreply.github.com> Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com>
1 parent fd6b7a6 commit 51936d7

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

contents/IFS/IFS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ Here, instead of tracking children of children, we track a single individual tha
144144
[import:5-14, lang:"lisp"](code/clisp/ifs.lisp)
145145
{% sample lang="coco" %}
146146
[import:4-16, lang:"coconut"](code/coconut/IFS.coco)
147+
{% sample lang="rust" %}
148+
[import:9-20, lang:"rust"](code/rust/IFS.rs)
147149
{% sample lang="java" %}
148150
[import:16-39, lang:"java"](code/java/IFS.java)
149151
{% 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
232234
[import, lang:"lisp"](code/clisp/ifs.lisp)
233235
{%sample lang="coco" %}
234236
[import, lang:"coconut"](code/coconut/IFS.coco)
237+
{%sample lang="rust" %}
238+
[import, lang:"rust"](code/rust/IFS.rs)
235239
{%sample lang="java" %}
236240
[import, lang:"java"](code/java/IFS.java)
237241
{% sample lang="ps1" %}

contents/IFS/code/rust/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
rand = "0.8.4"
10+
11+
[[bin]]
12+
path = "./IFS.rs"
13+
name = "main"

contents/IFS/code/rust/IFS.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use rand::*;
2+
3+
#[derive(Clone, Copy)]
4+
struct Point {
5+
x: f64,
6+
y: f64,
7+
}
8+
9+
fn chaos_game(iters: usize, shapes: Vec<Point>) -> Vec<Point> {
10+
let mut rng = rand::thread_rng();
11+
let mut p = Point{x: rng.gen(), y: rng.gen()};
12+
13+
(0..iters).into_iter().map(|_| {
14+
let old_point = p;
15+
let tmp = shapes[rng.gen_range(0..shapes.len())];
16+
p.x = 0.5 * (p.x + tmp.x);
17+
p.y = 0.5 * (p.y + tmp.y);
18+
old_point
19+
}).collect()
20+
}
21+
22+
fn main() {
23+
let shapes = vec![
24+
Point{x: 0., y: 0.},
25+
Point{x: 0.5, y: 0.75_f64.sqrt()},
26+
Point{x: 1., y: 0.},
27+
];
28+
29+
let mut out = String::new();
30+
31+
for point in chaos_game(10_000, shapes) {
32+
out += format!("{}\t{}\n", point.x, point.y).as_str();
33+
}
34+
35+
std::fs::write("./sierpinski.dat", out).unwrap();
36+
}

0 commit comments

Comments
 (0)