|
@@ -112,6 +112,33 @@ impl AnyGroup {
|
|
|
(x / self.size) + (y / self.size) * self.size
|
|
|
}
|
|
|
|
|
|
+ /// Convert index to x,y offsets.
|
|
|
+ pub fn cell_offset(&self, idx:u8) -> (u8,u8) {
|
|
|
+ (idx % self.size, idx / self.size)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Which index for given cell and (x,y)?
|
|
|
+ pub fn which_cell_index(&self, cell_index:u8, x:u8, y:u8) -> usize {
|
|
|
+ let (sx,sy) = self.cell_start(cell_index);
|
|
|
+ self.pos(sx + x, sy+y)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Where does a given cell index start?
|
|
|
+ pub fn cell_start(&self, cell_index:u8) -> (u8,u8) {
|
|
|
+ ((cell_index % self.size) * self.size, (cell_index/self.size) * self.size)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Return index of cell (x,y)
|
|
|
+ /// - This uses the groups to locate the index of the group.
|
|
|
+ pub fn cell_index(&self, index: u8, x: u8, y: u8) -> usize {
|
|
|
+ debug_assert!( x < self.size);
|
|
|
+ debug_assert!( y < self.size);
|
|
|
+
|
|
|
+ let result: usize =
|
|
|
+ self.groups[Groups::Cell as usize][index as usize * self.width as usize];
|
|
|
+ result + (y * self.width + x) as usize
|
|
|
+ }
|
|
|
+
|
|
|
pub fn calculate(&mut self) {
|
|
|
for y in 0..self.width {
|
|
|
for x in 0..self.width {
|
|
@@ -238,8 +265,6 @@ mod tests {
|
|
|
/// Verify that each index (0..max_index) is defined in each group.
|
|
|
/// - Verify that it is used, and only once.
|
|
|
fn check_dynamic() {
|
|
|
-
|
|
|
-
|
|
|
for size in 3..=5 {
|
|
|
let g = AnyGroup::new(size);
|
|
|
// g.display();
|
|
@@ -290,10 +315,9 @@ mod tests {
|
|
|
y + 1
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
all.fill(0);
|
|
|
for idx in 0..g.width {
|
|
|
let grp = g.row(idx);
|
|
@@ -357,4 +381,10 @@ mod tests {
|
|
|
*/
|
|
|
}
|
|
|
}
|
|
|
+ #[test]
|
|
|
+ fn check_cells() {
|
|
|
+ let size = 3;
|
|
|
+ let g = AnyGroup::new(size);
|
|
|
+ assert_eq!(g.cell_index(0, 0, 0), 0);
|
|
|
+ }
|
|
|
}
|