chiark / gitweb /
wip print-opt-orient
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 29 Oct 2022 21:05:47 +0000 (22:05 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 29 Oct 2022 21:06:59 +0000 (22:06 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/lib.rs

index 1a29be461241beac5304f161a03c1dc617ba3162..f911cd9381ee2e5d6ce573c844a1b29c1cc06b0c 100644 (file)
@@ -5,7 +5,7 @@ pub use std::io::Write as _;
 pub use std::iter;
 
 pub use itertools::izip;
-pub use itertools::Itertools as _;
+pub use itertools::Itertools;
 
 pub fn default<T: Default>() -> T { Default::default() }
 
@@ -26,14 +26,19 @@ pub fn read_vertices() -> impl Iterator<Item=Shape> {
       current[i] += d;
       current
     }).collect_vec();
-    for i in 0..DIM {
-      let min = points.iter().map(|p| p[i]).min().unwrap();
-      for p in &mut points { p[i] -= min; }
-    }
+    shape_abut_zero(&mut points);
     points
   })
 }
 
+/// Translate `shape` so that its minimum coordinate in each dimension is 0
+pub fn shape_abut_zero(shape: &mut Shape) {
+  for i in 0..DIM {
+    let min = shape.iter().map(|p| p[i]).min().unwrap();
+    for p in &mut shape[..] { p[i] -= min; }
+  }
+}
+
 pub fn shape_edges(shape: &Shape) -> impl Iterator<Item=[Point; 2]> + '_ {
   shape
     .iter()
@@ -43,6 +48,10 @@ pub fn shape_edges(shape: &Shape) -> impl Iterator<Item=[Point; 2]> + '_ {
     .map(|(&a,&b)| [a,b])
 }
 
+fn flip_coord(do_flip: bool, c: Coord) -> Coord {
+  if do_flip { MAX-c } else { c }
+}
+
 pub fn shape_all_flippings(shape: &Shape) -> impl Iterator<Item=Shape> + '_ {
   iter::repeat([false,true].into_iter())
     .take(DIM)
@@ -50,14 +59,28 @@ pub fn shape_all_flippings(shape: &Shape) -> impl Iterator<Item=Shape> + '_ {
     .map(|inv: Vec<bool>| {
       shape.iter().map(|p| {
         let mut inv = inv.iter();
-        p.map(|c| {
-          if *inv.next().unwrap() { MAX-c } else { c }
-        })
+        p.map(|c| flip_coord(*inv.next().unwrap(), c))
       })
         .collect_vec()
     })
 }
 
+pub fn shape_all_rotations(shape: &Shape) -> impl Iterator<Item=Shape> + '_ {
+  Itertools::cartesian_product(
+    0..DIM,
+    [false,true],
+  ).map(|(rot, flip)| {
+    shape.iter().map(|pi| {
+      let mut po: Point = default();
+      for i in 0..DIM {
+        po[i] = flip_coord(flip, pi[(i + rot) % DIM]);
+      }
+      po
+    })
+      .collect_vec()
+  })
+}
+
 pub fn print_shape(shape: &Shape) -> io::Result<()> {
   let mut o = io::stdout().lock();
   for vx in shape {