diff options
-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!(); } } |