diff --git a/src/bin/maze/main.rs b/src/bin/maze/main.rs index fb92add..f3f7903 100644 --- a/src/bin/maze/main.rs +++ b/src/bin/maze/main.rs @@ -9,9 +9,9 @@ use rand::Rng; use pixelfoo::color::Color; use pixelfoo::p2; -use pixelfoo::point2::Point2; +use pixelfoo::point2d::Point2d; use pixelfoo::v2; -use pixelfoo::vec2::Vec2; +use pixelfoo::vec2d::Vec2d; #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Square { @@ -41,7 +41,7 @@ fn send(w: &mut T, board: &Board) -> std::io::Result<()> { } impl Board { - fn new(board_size: Vec2, maze_size: Vec2) -> Board { + fn new(board_size: Vec2d, maze_size: Vec2d) -> Board { Board( (0..board_size.y) .map(move |y| { @@ -67,21 +67,21 @@ impl Board { .collect::>(), ) } - fn get(&self, pos: Point2) -> Square { + fn get(&self, pos: Point2d) -> Square { self.0[pos.y as usize][pos.x as usize] } - fn set(&mut self, pos: Point2, sq: Square) { + fn set(&mut self, pos: Point2d, sq: Square) { self.0[pos.y as usize][pos.x as usize] = sq; } } struct Move { - from: Point2, - dir: Vec2, + from: Point2d, + dir: Vec2d, prio: i32, } -fn add_move(open: &mut Vec, arg: isize, from: Point2, dir: Vec2) { +fn add_move(open: &mut Vec, arg: isize, from: Point2d, dir: Vec2d) { open.push(Move { from, dir, prio: 0 }); open.sort_unstable_by(|Move { prio: pa, .. }, Move { prio: pb, .. }| pa.cmp(pb)); } @@ -164,7 +164,7 @@ fn main() -> std::io::Result<()> { board.set(p1, Square::Corridor); board.set(p2, Square::Corridor); - for dir1 in Vec2::directions() { + for dir1 in Vec2d::directions() { let p3 = p2 + dir1; let p4 = p3 + dir1; if board.get(p3) == Square::Unknown { diff --git a/src/lib.rs b/src/lib.rs index 0fc4680..7d91fa8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ pub mod color; #[macro_use] -pub mod point2; +pub mod point2d; #[macro_use] -pub mod vec2; +pub mod vec2d; diff --git a/src/point2.rs b/src/point2.rs deleted file mode 100644 index 452839a..0000000 --- a/src/point2.rs +++ /dev/null @@ -1,66 +0,0 @@ -use std::ops::Add; - -use crate::vec2::Vec2; - -#[macro_export] -macro_rules! p2 { - ( $x:expr, $y:expr ) => { - Point2::new($x, $y) - }; -} - -#[derive(Clone, Copy, Debug)] -pub struct Point2 { - pub x: i32, - pub y: i32, -} - -impl Point2 { - pub fn new(x: i32, y: i32) -> Point2 { - Point2 { x, y } - } - - pub fn neighbours(&self) -> Vec { - Vec2::directions().iter().map(|v| self + v).collect() - } -} - -impl Add for Point2 { - type Output = Point2; - - fn add(self, other: Vec2) -> Point2 { - let x = self.x + other.x; - let y = self.y + other.y; - Point2 { x, y } - } -} - -impl<'a> Add<&'a Vec2> for Point2 { - type Output = Point2; - - fn add(self, other: &'a Vec2) -> Point2 { - let x = self.x + other.x; - let y = self.y + other.y; - Point2 { x, y } - } -} - -impl<'a> Add for &'a Point2 { - type Output = Point2; - - fn add(self, other: Vec2) -> Point2 { - let x = self.x + other.x; - let y = self.y + other.y; - Point2 { x, y } - } -} - -impl<'a> Add<&'a Vec2> for &'a Point2 { - type Output = Point2; - - fn add(self, other: &'a Vec2) -> Point2 { - let x = self.x + other.x; - let y = self.y + other.y; - Point2 { x, y } - } -} diff --git a/src/point2d.rs b/src/point2d.rs new file mode 100644 index 0000000..f8cf343 --- /dev/null +++ b/src/point2d.rs @@ -0,0 +1,66 @@ +use std::ops::Add; + +use crate::vec2d::Vec2d; + +#[macro_export] +macro_rules! p2 { + ( $x:expr, $y:expr ) => { + Point2d::new($x, $y) + }; +} + +#[derive(Clone, Copy, Debug)] +pub struct Point2d { + pub x: i32, + pub y: i32, +} + +impl Point2d { + pub fn new(x: i32, y: i32) -> Point2d { + Point2d { x, y } + } + + pub fn neighbours(&self) -> Vec { + Vec2d::directions().iter().map(|v| self + v).collect() + } +} + +impl Add for Point2d { + type Output = Point2d; + + fn add(self, other: Vec2d) -> Point2d { + let x = self.x + other.x; + let y = self.y + other.y; + Point2d { x, y } + } +} + +impl<'a> Add<&'a Vec2d> for Point2d { + type Output = Point2d; + + fn add(self, other: &'a Vec2d) -> Point2d { + let x = self.x + other.x; + let y = self.y + other.y; + Point2d { x, y } + } +} + +impl<'a> Add for &'a Point2d { + type Output = Point2d; + + fn add(self, other: Vec2d) -> Point2d { + let x = self.x + other.x; + let y = self.y + other.y; + Point2d { x, y } + } +} + +impl<'a> Add<&'a Vec2d> for &'a Point2d { + type Output = Point2d; + + fn add(self, other: &'a Vec2d) -> Point2d { + let x = self.x + other.x; + let y = self.y + other.y; + Point2d { x, y } + } +} diff --git a/src/rect2d.rs b/src/rect2d.rs new file mode 100644 index 0000000..0a1db40 --- /dev/null +++ b/src/rect2d.rs @@ -0,0 +1,4 @@ +pub struct Rect2d { + origin: Point2d, + size: Vec2d, +} diff --git a/src/vec2.rs b/src/vec2.rs deleted file mode 100644 index 06fdb36..0000000 --- a/src/vec2.rs +++ /dev/null @@ -1,64 +0,0 @@ -use std::ops::Add; - -#[macro_export] -macro_rules! v2 { - ( $x:expr, $y:expr ) => { - Vec2::new($x, $y) - }; -} - -#[derive(Clone, Copy, Debug)] -pub struct Vec2 { - pub x: i32, - pub y: i32, -} - -impl Vec2 { - pub fn new(x: i32, y: i32) -> Vec2 { - Vec2 { x, y } - } - - pub fn directions() -> Vec { - vec![v2!(1, 0), v2!(0, 1), v2!(-1, 0), v2!(0, -1)] - } -} - -impl Add for Vec2 { - type Output = Vec2; - - fn add(self, other: Vec2) -> Vec2 { - let x = self.x + other.x; - let y = self.y + other.y; - Vec2 { x, y } - } -} - -impl<'a> Add<&'a Vec2> for Vec2 { - type Output = Vec2; - - fn add(self, other: &'a Vec2) -> Vec2 { - let x = self.x + other.x; - let y = self.y + other.y; - Vec2 { x, y } - } -} - -impl<'a> Add for &'a Vec2 { - type Output = Vec2; - - fn add(self, other: Vec2) -> Vec2 { - let x = self.x + other.x; - let y = self.y + other.y; - Vec2 { x, y } - } -} - -impl<'a> Add<&'a Vec2> for &'a Vec2 { - type Output = Vec2; - - fn add(self, other: &'a Vec2) -> Vec2 { - let x = self.x + other.x; - let y = self.y + other.y; - Vec2 { x, y } - } -} diff --git a/src/vec2d.rs b/src/vec2d.rs new file mode 100644 index 0000000..23ec55a --- /dev/null +++ b/src/vec2d.rs @@ -0,0 +1,64 @@ +use std::ops::Add; + +#[macro_export] +macro_rules! v2 { + ( $x:expr, $y:expr ) => { + Vec2d::new($x, $y) + }; +} + +#[derive(Clone, Copy, Debug)] +pub struct Vec2d { + pub x: i32, + pub y: i32, +} + +impl Vec2d { + pub fn new(x: i32, y: i32) -> Vec2d { + Vec2d { x, y } + } + + pub fn directions() -> Vec { + vec![v2!(1, 0), v2!(0, 1), v2!(-1, 0), v2!(0, -1)] + } +} + +impl Add for Vec2d { + type Output = Vec2d; + + fn add(self, other: Vec2d) -> Vec2d { + let x = self.x + other.x; + let y = self.y + other.y; + Vec2d { x, y } + } +} + +impl<'a> Add<&'a Vec2d> for Vec2d { + type Output = Vec2d; + + fn add(self, other: &'a Vec2d) -> Vec2d { + let x = self.x + other.x; + let y = self.y + other.y; + Vec2d { x, y } + } +} + +impl<'a> Add for &'a Vec2d { + type Output = Vec2d; + + fn add(self, other: Vec2d) -> Vec2d { + let x = self.x + other.x; + let y = self.y + other.y; + Vec2d { x, y } + } +} + +impl<'a> Add<&'a Vec2d> for &'a Vec2d { + type Output = Vec2d; + + fn add(self, other: &'a Vec2d) -> Vec2d { + let x = self.x + other.x; + let y = self.y + other.y; + Vec2d { x, y } + } +}