diff options
author | Dennis Kobert <d-kobert@web.de> | 2019-05-31 07:35:55 +0200 |
---|---|---|
committer | Dennis Kobert <d-kobert@web.de> | 2019-05-31 07:35:55 +0200 |
commit | 3527f10f8767b3b62b876f8a3f3bc5bd3ba56b2d (patch) | |
tree | 3d785c5e8b1b6ca62dc0a34c687a228c4c374ad3 | |
parent | 031ba8a21d3899d4e24bd1423ec738ebf95ab947 (diff) |
Add Vec2
-rw-r--r-- | game_server/src/maths.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs new file mode 100644 index 0000000..635eed6 --- /dev/null +++ b/game_server/src/maths.rs @@ -0,0 +1,54 @@ +pub struct Vec2 { + pub x: f32, + pub y: f32, +} + +impl std::ops::Add for Vec2 { + type Output = Self; + fn add(self, other: Self) -> Self { + Self { + x: x + other.x, + y: y + other.y + } + } +} + +impl std::ops::Sub for Vec2 { + type Output = Self; + fn sub(self, other: Self) -> Self { + Self { + x: x - other.x, + y: y - other.y + } + } +} + +impl std::ops::Neg for Vec2 { + type Output = Self; + fn sub(self) -> Self { + Self { + x: -x, + y: -y + } + } +} + +impl std::cmp::PartialEq for Vec2 { + type Output = bool; + fn eq(&self, other: &Self) -> bool { + f32::abs(self.x - other.x) < 1e-8 + && f32::abs(self.y - other.y) < 1e-8 + } +} + +impl std::cmp::Eq for Vec2 {} + +impl Vec2 { + pub fn distance(&self) -> f32 { + f32::sqrt(self.distance2) + } + + pub fn distance2(&self) -> f32 { + self.x * self.x + self.y * self.y + } +} |