118 lines
3.0 KiB
Rust
118 lines
3.0 KiB
Rust
|
|
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 {
|
|
pub x : f32,
|
|
pub y : f32,
|
|
pub width : f32,
|
|
pub height : f32,
|
|
|
|
pub pos_x: u32,
|
|
pub pos_y: u32,
|
|
}
|
|
|
|
pub struct CellGrid {
|
|
x_axis: CellAxis,
|
|
y_axis: CellAxis,
|
|
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,
|
|
curr: Cell {x, y, width, height, pos_x: 0, pos_y: 0}
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
} |