simplify CellGrid

Signed-off-by: Fabrizio Iannetti <fabrizio.iannetti@gmail.com>
This commit is contained in:
Fabrizio Iannetti 2022-12-26 07:52:11 +01:00
parent 7c682495d7
commit f2f4e36c20
1 changed files with 50 additions and 86 deletions

View File

@ -1,118 +1,82 @@
struct CellAxis {
min: f32,
max: f32,
num: u32,
idx: u32
}
impl CellAxis {
pub fn new(min: f32, len: f32, num: u32) -> Self {
Self {
min,
max: min + len,
num,
idx: 0
}
}
pub fn reset(&mut self) {
self.idx = 0;
}
}
impl Iterator for CellAxis {
type Item = (f32, f32);
fn next(&mut self) -> Option<Self::Item> {
if self.idx < self.num {
let beg: f32 = self.min + ((self.max - self.min) * self.idx as f32) / self.num as f32;
let end: f32 = self.min + ((self.max - self.min) * (self.idx + 1) as f32) / self.num as f32;
self.idx += 1;
return Some((beg.trunc(), end.trunc()));
}
return None;
}
}
#[derive(Copy, Clone)]
pub struct Cell {
// display coordinates of the cell
pub x : f32,
pub y : f32,
pub width : f32,
pub height : f32,
// position of the cell in the grid (0-based)
pub pos_x: u32,
pub pos_y: u32,
}
pub struct CellGrid {
x_axis: CellAxis,
y_axis: CellAxis,
// display coordinates of the grid
x: f32,
y: f32,
width: f32,
height: f32,
// size fo the grid, in number of cells
num_x: u32,
num_y: u32,
// current position when iterating
pos_x: u32,
pos_y: u32,
// current cell when iterating
curr: Cell,
}
impl CellGrid {
pub fn new(x: f32, y: f32, width: f32, height: f32, num_x: u32, num_y: u32) -> Self {
let x_axis = CellAxis::new(x, width, num_x);
let y_axis = CellAxis::new(y, height, num_y);
Self {
x_axis,
y_axis,
x,
y,
width,
height,
num_x,
num_y,
pos_x: 0,
pos_y: 0,
curr: Cell {x, y, width, height, pos_x: 0, pos_y: 0}
}
}
fn compute_cell(&mut self) -> () {
self.curr.pos_x = self.pos_x;
self.curr.pos_y = self.pos_y;
let beg_x: f32 = self.x + (self.width * self.curr.pos_x as f32) / self.num_x as f32;
let end_x: f32 = self.x + (self.width * (self.curr.pos_x + 1) as f32) / self.num_x as f32;
self.curr.x = beg_x.trunc();
self.curr.width = end_x.trunc() - self.curr.x;
let beg_y: f32 = self.y + (self.height * self.curr.pos_y as f32) / self.num_y as f32;
let end_y: f32 = self.y + (self.height * (self.curr.pos_y + 1) as f32) / self.num_y as f32;
self.curr.y = beg_y.trunc();
self.curr.height = end_y.trunc() - self.curr.y;
}
}
impl Iterator for CellGrid {
type Item = Cell;
fn next(&mut self) -> Option<Self::Item> {
if self.y_axis.idx == 0 && self.x_axis.idx == 0 {
if self.y_axis.num == 0 || self.x_axis.num == 0 {
return None
}
self.curr.pos_y = self.y_axis.idx;
let y_next = self.y_axis.next().unwrap();
self.curr.y = y_next.0;
self.curr.height = y_next.1 - y_next.0;
}
self.curr.pos_x = self.x_axis.idx;
let x_next = self.x_axis.next();
match x_next {
None => {
// end of x-axis, move down the y-axis
self.curr.pos_y = self.y_axis.idx;
let y_next = self.y_axis.next();
match y_next {
None => {
None
},
Some(y_val) => {
// set new vertical span
self.curr.y = y_val.0;
self.curr.height = y_val.1 - y_val.0;
// reset iteration on x axis
self.x_axis.reset();
self.curr.pos_x = 0;
// get first horizontal span
let x_val = self.x_axis.next().unwrap();
self.curr.x = x_val.0;
self.curr.width = x_val.1 - x_val.0;
Some(self.curr)
}
}
},
Some(x_val) => {
self.curr.x = x_val.0;
self.curr.width = x_val.1 - x_val.0;
Some(self.curr)
if self.pos_y >= self.num_y {
None
} else {
self.compute_cell();
self.pos_x += 1;
if self.pos_x >= self.num_x {
self.pos_x = 0;
self.pos_y += 1;
}
Some(self.curr)
}
}
}