summaryrefslogtreecommitdiff
path: root/2019-worms/src/geometry
diff options
context:
space:
mode:
Diffstat (limited to '2019-worms/src/geometry')
-rw-r--r--2019-worms/src/geometry/direction.rs67
-rw-r--r--2019-worms/src/geometry/point.rs37
-rw-r--r--2019-worms/src/geometry/vec.rs62
3 files changed, 166 insertions, 0 deletions
diff --git a/2019-worms/src/geometry/direction.rs b/2019-worms/src/geometry/direction.rs
new file mode 100644
index 0000000..e37f750
--- /dev/null
+++ b/2019-worms/src/geometry/direction.rs
@@ -0,0 +1,67 @@
+use crate::geometry::vec::Vec2d;
+use std::fmt;
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub enum Direction {
+ North,
+ NorthEast,
+ East,
+ SouthEast,
+ South,
+ SouthWest,
+ West,
+ NorthWest,
+}
+
+impl fmt::Display for Direction {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ use Direction::*;
+ let s = match self {
+ North => "N",
+ NorthEast => "NE",
+ East => "E",
+ SouthEast => "SE",
+ South => "S",
+ SouthWest => "SW",
+ West => "W",
+ NorthWest => "NW",
+ };
+ f.write_str(s)
+ }
+}
+
+impl Direction {
+ pub fn is_diagonal(&self) -> bool {
+ use Direction::*;
+
+ match self {
+ NorthEast | SouthEast | SouthWest | NorthWest => true,
+ _ => false,
+ }
+ }
+
+ pub fn as_vec(&self) -> Vec2d {
+ use Direction::*;
+ match self {
+ North => Vec2d::new(0, -1),
+ NorthEast => Vec2d::new(1, -1),
+ East => Vec2d::new(1, 0),
+ SouthEast => Vec2d::new(1, 1),
+ South => Vec2d::new(0, 1),
+ SouthWest => Vec2d::new(-1, 1),
+ West => Vec2d::new(-1, 0),
+ NorthWest => Vec2d::new(-1, -1),
+ }
+ }
+
+ pub const ALL: [Direction; 8] = [
+ Direction::North,
+ Direction::NorthEast,
+ Direction::East,
+ Direction::SouthEast,
+ Direction::South,
+ Direction::SouthWest,
+ Direction::West,
+ Direction::NorthWest,
+ ];
+}
diff --git a/2019-worms/src/geometry/point.rs b/2019-worms/src/geometry/point.rs
new file mode 100644
index 0000000..1ab9b36
--- /dev/null
+++ b/2019-worms/src/geometry/point.rs
@@ -0,0 +1,37 @@
+use crate::geometry::vec::*;
+
+use std::ops::*;
+
+#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
+pub struct Point2d {
+ pub x: i8,
+ pub y: i8,
+}
+
+impl Point2d {
+ pub fn new(x: i8, y: i8) -> Point2d {
+ Point2d { x, y }
+ }
+}
+
+impl Add<Vec2d> for Point2d {
+ type Output = Self;
+
+ fn add(self, other: Vec2d) -> Self {
+ Point2d {
+ x: self.x.saturating_add(other.x),
+ y: self.y.saturating_add(other.y),
+ }
+ }
+}
+
+impl Sub for Point2d {
+ type Output = Vec2d;
+
+ fn sub(self, other: Self) -> Vec2d {
+ Vec2d {
+ x: self.x.saturating_sub(other.x),
+ y: self.y.saturating_sub(other.y),
+ }
+ }
+}
diff --git a/2019-worms/src/geometry/vec.rs b/2019-worms/src/geometry/vec.rs
new file mode 100644
index 0000000..375a0f9
--- /dev/null
+++ b/2019-worms/src/geometry/vec.rs
@@ -0,0 +1,62 @@
+use std::ops::*;
+
+#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
+pub struct Vec2d {
+ pub x: i8,
+ pub y: i8,
+}
+
+impl Vec2d {
+ pub const fn new(x: i8, y: i8) -> Vec2d {
+ Vec2d { x, y }
+ }
+ pub fn magnitude_squared(&self) -> i8 {
+ self.x
+ .saturating_pow(2)
+ .saturating_add(self.y.saturating_pow(2))
+ }
+}
+
+impl Add for Vec2d {
+ type Output = Self;
+
+ fn add(self, other: Self) -> Self {
+ Vec2d {
+ x: self.x.saturating_add(other.x),
+ y: self.y.saturating_add(other.y),
+ }
+ }
+}
+
+impl Sub for Vec2d {
+ type Output = Self;
+
+ fn sub(self, other: Self) -> Self {
+ Vec2d {
+ x: self.x.saturating_sub(other.x),
+ y: self.y.saturating_sub(other.y),
+ }
+ }
+}
+
+impl Mul<i8> for Vec2d {
+ type Output = Self;
+
+ fn mul(self, other: i8) -> Self {
+ Vec2d {
+ x: self.x.saturating_mul(other),
+ y: self.y.saturating_mul(other),
+ }
+ }
+}
+
+impl Neg for Vec2d {
+ type Output = Self;
+
+ fn neg(self) -> Self {
+ Vec2d {
+ x: -self.x,
+ y: -self.y,
+ }
+ }
+}