summaryrefslogtreecommitdiff
path: root/libhitomezashi/src/lib.rs
diff options
context:
space:
mode:
authorArjun Satarkar <me@arjunsatarkar.net>2023-06-01 13:31:24 +0000
committerArjun Satarkar <me@arjunsatarkar.net>2023-06-01 13:33:56 +0000
commit3a2fef94230f35d948634ffef83660afdffa0bad (patch)
treee4fb261e6fcf6d0b4b3d4b688aa94757700f45ab /libhitomezashi/src/lib.rs
parent316382d74ed4a705c43e2380e7af20a500c24fa9 (diff)
downloadhitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.tar
hitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.tar.gz
hitomezashi-3a2fef94230f35d948634ffef83660afdffa0bad.zip
Complete libhitomezashi, reset project
Diffstat (limited to 'libhitomezashi/src/lib.rs')
-rw-r--r--libhitomezashi/src/lib.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/libhitomezashi/src/lib.rs b/libhitomezashi/src/lib.rs
new file mode 100644
index 0000000..b07057a
--- /dev/null
+++ b/libhitomezashi/src/lib.rs
@@ -0,0 +1,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));
+ }
+}