diff --git a/Cargo.toml b/Cargo.toml index 309ff3ba5..ab2e67ee1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,3 +71,8 @@ path = "examples/histogram.rs" [[example]] name = "acoustic_wave" path = "examples/acoustic_wave.rs" + +[[example]] +name = "conway" +path = "examples/conway.rs" + diff --git a/examples/conway.rs b/examples/conway.rs new file mode 100644 index 000000000..6aa45829a --- /dev/null +++ b/examples/conway.rs @@ -0,0 +1,31 @@ +extern crate arrayfire as af; + +use af::*; + +fn main() { + set_device(0); + info(); + conways_game_of_life(); +} + +fn normalise(a: &Array) -> Array { + (a / (max_all(&abs(a)).0 as f32)) +} + +fn conways_game_of_life() { + let h_kernel = [1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0]; + let kernel = Array::new(&h_kernel, Dim4::new(&[3, 3, 1, 1])); + let s = constant::(0.5, Dim4::new(&[1, 1, 1, 1])); + let mut state = gt(&randu::(Dim4::new(&[256, 256, 3, 1])), &s, false); + let c0 = constant::(2.0, Dim4::new(&[1, 1, 1, 1])); + let c1 = constant::(3.0, Dim4::new(&[1, 1, 1, 1])); + + let win = Window::new(512, 512, "Game of Life".to_string()); + while !win.is_closed() { + let n_hood = convolve2(&state, &kernel, ConvMode::DEFAULT, ConvDomain::SPATIAL); + let c0 = &eq(&n_hood, &c0, false); + let c1 = &eq(&n_hood, &c1, false); + state = state * c0 + c1; + win.draw_image(&normalise(&state), None); + } +}