Brownian tree: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: IupCloseOnEscape no longer needed) |
(→{{header|Rust}}: Update library to rand = "^0.8") |
||
Line 4,126: | Line 4,126: | ||
use image::ColorType; |
use image::ColorType; |
||
use rand::distributions::{IndependentSample, Range}; |
|||
use std::cmp::{min, max}; |
use std::cmp::{min, max}; |
||
use std::env; |
use std::env; |
||
use std::path::Path; |
use std::path::Path; |
||
use std::process; |
use std::process; |
||
use rand::Rng; |
|||
fn help() { |
fn help() { |
||
Line 4,172: | Line 4,173: | ||
width as u32, |
width as u32, |
||
height as u32, |
height as u32, |
||
ColorType:: |
ColorType::L8) { |
||
Err(e) => println!("Error writing output image:\n{}", e), |
Err(e) => println!("Error writing output image:\n{}", e), |
||
Ok(_) => println!("Output written to:\n{}", output_path.to_str().unwrap()), |
Ok(_) => println!("Output written to:\n{}", output_path.to_str().unwrap()), |
||
Line 4,183: | Line 4,184: | ||
let mut field_base: Vec<_> = raw.as_mut_slice().chunks_mut(width).collect(); |
let mut field_base: Vec<_> = raw.as_mut_slice().chunks_mut(width).collect(); |
||
// Addressable 2d vector |
// Addressable 2d vector |
||
let |
let field: &mut [&mut [u8]] = field_base.as_mut_slice(); |
||
// Seed mote |
// Seed mote |
||
field[width / 2][height / 2] = 1; |
field[width / 2][height / 2] = 1; |
||
let walk_range = Range::new(-1i32, 2i32); |
|||
⚫ | |||
let y_spawn_range = Range::new(1usize, height - 1); |
|||
let mut rng = rand::thread_rng(); |
let mut rng = rand::thread_rng(); |
||
Line 4,198: | Line 4,196: | ||
} |
} |
||
⚫ | |||
// Spawn mote |
|||
let mut |
let mut y=rng.gen_range(1usize..height-1); |
||
let mut y = y_spawn_range.ind_sample(&mut rng); |
|||
// Increment field value when motes spawn on top of the structure |
// Increment field value when motes spawn on top of the structure |
||
Line 4,210: | Line 4,207: | ||
loop { |
loop { |
||
let contacts = field[x - 1][y - 1] + field[x][y - 1] + field[x + 1][y - 1] + |
let contacts = field[x - 1][y - 1] + field[x][y - 1] + field[x + 1][y - 1] + |
||
field[x - 1][y] + field[x + 1][y] + |
|||
field[x - 1][y + 1] + field[x][y + 1] + |
|||
field[x + 1][y + 1]; |
|||
if contacts > 0 { |
if contacts > 0 { |
||
Line 4,218: | Line 4,215: | ||
break; |
break; |
||
} else { |
} else { |
||
let xw = |
let xw = rng.gen_range(-1..2) + x as i32; |
||
let yw = |
let yw = rng.gen_range(-1..2) + y as i32; |
||
if xw < 1 || xw >= (width as i32 - 1) || yw < 1 || yw >= (height as i32 - 1) { |
if xw < 1 || xw >= (width as i32 - 1) || yw < 1 || yw >= (height as i32 - 1) { |
||
break; |
break; |
||
Line 4,229: | Line 4,226: | ||
} |
} |
||
}</lang> |
}</lang> |
||
For a 512 x 512 field and |
For a 512 x 512 field and 100k motes, run time is around 200 s on ~2019 hardware (Ryzen 5 3600X). |
||
<center>[[File:Rust-Brownian-512-20k.png]]</center> |
<center>[[File:Rust-Brownian-512-20k.png]]</center> |
||