summaryrefslogtreecommitdiff
path: root/libhitomezashi/src/lib.rs
blob: b07057a8943dcda0ef1f48f0d80ca90e794da139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
mod pixel_buffer;
pub use bitvec::vec::BitVec;
use pixel_buffer::PixelBuffer;

pub fn get_hitomezashi_pattern(
    x_pattern: BitVec,
    y_pattern: BitVec,
    gap: usize,
    line_thickness: usize,
) -> PixelBuffer {
    let mut buf = PixelBuffer::new(x_pattern.len() * gap, y_pattern.len() * gap);
    // Draw y pattern (horizontal) lines
    for i in 0..y_pattern.len() {
        for j in 0..x_pattern.len() {
            if (j % 2 != 0) == y_pattern[i] {
                let x = j * gap;
                let y = i * gap;
                let width = gap;
                let height = line_thickness;
                buf.fill_rect(x, y, width, height);
            }
        }
    }
    // Draw x pattern (vertical) lines
    for i in 0..x_pattern.len() {
        for j in 0..y_pattern.len() {
            if (j % 2 != 0) == x_pattern[i] {
                let x = i * gap;
                let y = j * gap;
                let width = line_thickness;
                let height = gap;
                buf.fill_rect(x, y, width, height)
            }
        }
    }

    // Join up the lines to avoid leaving holes at the intersections
    // The best way to understand how this works is to try commenting it out
    for x in (gap..buf.width).step_by(gap) {
        for y in (gap..buf.height).step_by(gap) {
            let width = line_thickness;
            let height = line_thickness;
            buf.fill_rect(x, y, width, height);
        }
    }
    buf
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let mut x_pattern = BitVec::new();
        x_pattern.resize(40, false);
        x_pattern.set(1, true);
        let mut y_pattern = BitVec::new();
        y_pattern.resize(40, false);
        y_pattern.set(1, true);
        println!("{}", get_hitomezashi_pattern(x_pattern, y_pattern, 3, 1));
    }
}