Factor out two-dimensional arithmetic from maze.
parent
610c864cd9
commit
c3ca8888fd
@ -1 +1,5 @@
|
||||
pub mod color;
|
||||
#[macro_use]
|
||||
pub mod point2;
|
||||
#[macro_use]
|
||||
pub mod vec2;
|
||||
|
@ -0,0 +1,66 @@
|
||||
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<Point2> {
|
||||
Vec2::directions().iter().map(|v| self + v).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Vec2> 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<Vec2> 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 }
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
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<Vec2> {
|
||||
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<Vec2> 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 }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue