aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/graphics/vector2.rs
diff options
context:
space:
mode:
Diffstat (limited to 'games/rstnode/rst-client/src/graphics/vector2.rs')
-rw-r--r--games/rstnode/rst-client/src/graphics/vector2.rs82
1 files changed, 82 insertions, 0 deletions
diff --git a/games/rstnode/rst-client/src/graphics/vector2.rs b/games/rstnode/rst-client/src/graphics/vector2.rs
new file mode 100644
index 000000000000..5204e77fa443
--- /dev/null
+++ b/games/rstnode/rst-client/src/graphics/vector2.rs
@@ -0,0 +1,82 @@
+use std::fmt::{self, Display, Formatter};
+use std::ops::{Add, Mul, Sub, SubAssign};
+
+/// Just a vector
+#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
+pub struct Vector2 {
+ pub x: f32,
+ pub y: f32,
+}
+
+impl Display for Vector2 {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ write!(f, "[{}, {}]", self.x, self.y)
+ }
+}
+
+impl Vector2 {
+ pub fn new(x: f32, y: f32) -> Self {
+ Self { x, y }
+ }
+
+ pub fn abs(self) -> Self {
+ Self {
+ x: self.x.abs(),
+ y: self.y.abs(),
+ }
+ }
+}
+
+impl Sub for Vector2 {
+ type Output = Vector2;
+
+ fn sub(self, o: Vector2) -> Self::Output {
+ Vector2 {
+ x: self.x - o.x,
+ y: self.y - o.y,
+ }
+ }
+}
+
+impl SubAssign for Vector2 {
+ fn sub_assign(&mut self, o: Self) {
+ *self = Self {
+ x: self.x - o.x,
+ y: self.y - o.y,
+ }
+ }
+}
+
+impl Add for Vector2 {
+ type Output = Vector2;
+
+ fn add(self, o: Vector2) -> Self::Output {
+ Vector2 {
+ x: self.x + o.x,
+ y: self.y + o.y,
+ }
+ }
+}
+
+impl Mul for Vector2 {
+ type Output = Vector2;
+
+ fn mul(self, o: Vector2) -> Self::Output {
+ Vector2 {
+ x: self.x * o.x,
+ y: self.y * o.y,
+ }
+ }
+}
+
+impl From<Vector2> for mint::Point2<f32> {
+ fn from(v: Vector2) -> Self {
+ [v.x, v.y].into()
+ }
+}
+
+impl From<mint::Point2<f32>> for Vector2 {
+ fn from(v: mint::Point2<f32>) -> Self {
+ Self { x: v.x, y: v.y }
+ }
+}