summaryrefslogtreecommitdiff
path: root/2019-worms/src/geometry/vec.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2022-04-19 21:29:18 +0200
committerJustin Wernick <justin@worthe-it.co.za>2022-04-19 21:29:18 +0200
commit4d1edc899a86791ec57c0d3b2b3d7427f138463f (patch)
treeed1c7031c54d8f3a3897081c4a1040ace9d8f4ae /2019-worms/src/geometry/vec.rs
parent01a849c6b26afd076989f645c6153d8ce1422281 (diff)
parent3f5492b2bb67326be43cd7c5ba02ccf0ba1ae0e3 (diff)
Merge branch 'worms-main'
Diffstat (limited to '2019-worms/src/geometry/vec.rs')
-rw-r--r--2019-worms/src/geometry/vec.rs62
1 files changed, 62 insertions, 0 deletions
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,
+ }
+ }
+}