Skip to content

Commit 6b9637c

Browse files
rozgo9prady9
authored andcommitted
adds example of conways game of life
1 parent a5256f3 commit 6b9637c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ path = "examples/histogram.rs"
7171
[[example]]
7272
name = "acoustic_wave"
7373
path = "examples/acoustic_wave.rs"
74+
75+
[[example]]
76+
name = "conway"
77+
path = "examples/conway.rs"
78+

examples/conway.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
extern crate arrayfire as af;
2+
3+
use af::*;
4+
5+
fn main() {
6+
set_device(0);
7+
info();
8+
conways_game_of_life();
9+
}
10+
11+
fn normalise(a: &Array<f32>) -> Array<f32> {
12+
(a / (max_all(&abs(a)).0 as f32))
13+
}
14+
15+
fn conways_game_of_life() {
16+
let h_kernel = [1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0];
17+
let kernel = Array::new(&h_kernel, Dim4::new(&[3, 3, 1, 1]));
18+
let s = constant::<f32>(0.5, Dim4::new(&[1, 1, 1, 1]));
19+
let mut state = gt(&randu::<f32>(Dim4::new(&[256, 256, 3, 1])), &s, false);
20+
let c0 = constant::<f32>(2.0, Dim4::new(&[1, 1, 1, 1]));
21+
let c1 = constant::<f32>(3.0, Dim4::new(&[1, 1, 1, 1]));
22+
23+
let win = Window::new(512, 512, "Game of Life".to_string());
24+
while !win.is_closed() {
25+
let n_hood = convolve2(&state, &kernel, ConvMode::DEFAULT, ConvDomain::SPATIAL);
26+
let c0 = &eq(&n_hood, &c0, false);
27+
let c1 = &eq(&n_hood, &c1, false);
28+
state = state * c0 + c1;
29+
win.draw_image(&normalise(&state), None);
30+
}
31+
}

0 commit comments

Comments
 (0)