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::Gray(8)) {
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 mut field: &mut [&mut [u8]] = field_base.as_mut_slice();
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 x_spawn_range = Range::new(1usize, width - 1);
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:
}
}


let mut x=rng.gen_range(1usize..width-1);
// Spawn mote
let mut x = x_spawn_range.ind_sample(&mut rng);
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] + field[x + 1][y] +
field[x - 1][y + 1] + field[x][y + 1] +
field[x - 1][y + 1] + field[x][y + 1] +
field[x + 1][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 = walk_range.ind_sample(&mut rng) + x as i32;
let xw = rng.gen_range(-1..2) + x as i32;
let yw = walk_range.ind_sample(&mut rng) + y as i32;
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 65k motes, run time is 25 s on ~2011 hardware (Phenom II X4).
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>