mirror of
https://github.com/drewcassidy/vector-victor.git
synced 2024-06-11 02:06:53 +00:00
35 lines
743 B
Rust
35 lines
743 B
Rust
pub trait Index2D: Copy {
|
|
fn to_1d(&self, width: usize) -> usize {
|
|
let (r, c) = self.to_2d(width);
|
|
r * width + c
|
|
}
|
|
|
|
fn to_2d(&self, width: usize) -> (usize, usize);
|
|
|
|
fn to_2d_offset(
|
|
&self,
|
|
width: usize,
|
|
height: usize,
|
|
r: usize,
|
|
c: usize,
|
|
) -> Option<(usize, usize)> {
|
|
let (row, col) = self.to_2d(width);
|
|
if row >= height || col >= width {
|
|
return None;
|
|
};
|
|
Some((row + r, col + c))
|
|
}
|
|
}
|
|
|
|
impl Index2D for usize {
|
|
fn to_2d(&self, width: usize) -> (usize, usize) {
|
|
(*self / width, *self % width)
|
|
}
|
|
}
|
|
|
|
impl Index2D for (usize, usize) {
|
|
fn to_2d(&self, _: usize) -> (usize, usize) {
|
|
*self
|
|
}
|
|
}
|