109 lines
2.1 KiB
Rust
109 lines
2.1 KiB
Rust
|
use std::{
|
||
|
ops::{Add, AddAssign, Sub, SubAssign},
|
||
|
str::FromStr,
|
||
|
};
|
||
|
|
||
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||
|
pub struct Vec2 {
|
||
|
x: i32, // increases toward right
|
||
|
y: i32, // increases toward top
|
||
|
}
|
||
|
|
||
|
impl Vec2 {
|
||
|
#[must_use]
|
||
|
pub const fn new(x: i32, y: i32) -> Self {
|
||
|
Self { x, y }
|
||
|
}
|
||
|
|
||
|
#[must_use]
|
||
|
pub fn map(self, mut f: impl FnMut(i32) -> i32) -> Self {
|
||
|
Self {
|
||
|
x: f(self.x),
|
||
|
y: f(self.y),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Add for Vec2 {
|
||
|
type Output = Self;
|
||
|
|
||
|
fn add(self, rhs: Self) -> Self::Output {
|
||
|
Self {
|
||
|
x: self.x + rhs.x,
|
||
|
y: self.y + rhs.y,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl AddAssign for Vec2 {
|
||
|
fn add_assign(&mut self, rhs: Self) {
|
||
|
self.x += rhs.x;
|
||
|
self.y += rhs.y;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Sub for Vec2 {
|
||
|
type Output = Self;
|
||
|
|
||
|
fn sub(self, rhs: Self) -> Self::Output {
|
||
|
Self {
|
||
|
x: self.x - rhs.x,
|
||
|
y: self.y - rhs.y,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl SubAssign for Vec2 {
|
||
|
fn sub_assign(&mut self, rhs: Self) {
|
||
|
self.x -= rhs.x;
|
||
|
self.y -= rhs.y;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<(i32, i32)> for Vec2 {
|
||
|
fn from((x, y): (i32, i32)) -> Self {
|
||
|
Self { x, y }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<Vec2> for (i32, i32) {
|
||
|
fn from(v: Vec2) -> Self {
|
||
|
(v.x, v.y)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<Direction> for Vec2 {
|
||
|
/// Creates a unit vector in the given direction.
|
||
|
fn from(dir: Direction) -> Self {
|
||
|
match dir {
|
||
|
Direction::Up => (0, 1),
|
||
|
Direction::Down => (0, -1),
|
||
|
Direction::Left => (-1, 0),
|
||
|
Direction::Right => (1, 0),
|
||
|
}
|
||
|
.into()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||
|
pub enum Direction {
|
||
|
Up,
|
||
|
Down,
|
||
|
Left,
|
||
|
Right,
|
||
|
}
|
||
|
|
||
|
impl FromStr for Direction {
|
||
|
type Err = ();
|
||
|
|
||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||
|
match s {
|
||
|
"U" => Ok(Direction::Up),
|
||
|
"D" => Ok(Direction::Down),
|
||
|
"L" => Ok(Direction::Left),
|
||
|
"R" => Ok(Direction::Right),
|
||
|
_ => Err(()),
|
||
|
}
|
||
|
}
|
||
|
}
|