diff options
author | Nicholas Johnson <nick@nicholasjohnson.ch> | 2024-04-19 00:00:00 +0000 |
---|---|---|
committer | Nicholas Johnson <nick@nicholasjohnson.ch> | 2024-04-19 00:00:00 +0000 |
commit | 0638f65020a40273eaaa4451a38ac859783bc4b8210c5305382374c2f8eaec3c (patch) | |
tree | 993646b5c0736070b7bdcbef453892f727d29faab70ec30d4a72621f7efa4333 /src/hitomezashi.rs | |
parent | 17a81937963234817277918ef5ed8fa6f03b74c40e58d8ff8d5e0e6caa220a7c (diff) | |
download | hitomezashi-rs-0638f65020a40273eaaa4451a38ac859783bc4b8210c5305382374c2f8eaec3c.tar.gz hitomezashi-rs-0638f65020a40273eaaa4451a38ac859783bc4b8210c5305382374c2f8eaec3c.zip |
Replace for loop with iterator
Diffstat (limited to 'src/hitomezashi.rs')
-rw-r--r-- | src/hitomezashi.rs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/hitomezashi.rs b/src/hitomezashi.rs index 24a59be..88ba387 100644 --- a/src/hitomezashi.rs +++ b/src/hitomezashi.rs @@ -47,6 +47,12 @@ pub fn hitomezashi(width: usize, height: usize, skew: Option<f64>) { col_bits.push(brn.sample(&mut rng)); } + let mut alt_bits: Vec<bool> = Vec::with_capacity(width - 1); + + for col in 0..width { + alt_bits.push(col % 2 == 1); + } + // each new row of the pattern depends on the bits directly above it let mut above_bits: Vec<bool> = Vec::with_capacity(width); @@ -65,13 +71,17 @@ pub fn hitomezashi(width: usize, height: usize, skew: Option<f64>) { // height-1 because the first row has already been printed for row in 0..(height - 1) { - for col in 0..width { - /* each square in each successive row is derived from the square above it. the row bits - * represent whether there's a stitch between the two squares. if there's a stitch, the - * squares are different, otherwise they are the same. */ - above_bits[col] = above_bits[col] ^ (row_bits[row] ^ (col % 2 == 1)); - print_square(above_bits[col]); - } + /* each square in each successive row is derived from the square above it. the row bits + * represent whether there's a stitch between the two squares. if there's a stitch, the + * squares are different, otherwise they are the same. */ + above_bits + .iter_mut() + .zip(alt_bits.iter()) + .for_each(|(x1, &x2)| { + *x1 ^= x2 ^ row_bits[row]; + print_square(*x1); + }); + println!(); } } |