ascii_forge/
math.rs

1use std::ops::{Add, AddAssign, Sub, SubAssign};
2
3/// A 2d Vector
4/// Can be made from (u16, u16).
5/// Using a single u16.into() will create a vec2 where both values are the same.
6/// Basic Mathematic Operations are supported
7#[derive(Default, Debug, Eq, PartialEq, PartialOrd, Ord, Copy, Clone)]
8pub struct Vec2 {
9    pub x: u16,
10    pub y: u16,
11}
12
13impl From<(u16, u16)> for Vec2 {
14    fn from(value: (u16, u16)) -> Self {
15        vec2(value.0, value.1)
16    }
17}
18
19impl From<u16> for Vec2 {
20    fn from(value: u16) -> Self {
21        vec2(value, value)
22    }
23}
24
25impl<V: Into<Vec2>> Add<V> for Vec2 {
26    type Output = Vec2;
27    fn add(mut self, rhs: V) -> Self::Output {
28        let rhs = rhs.into();
29        self.x += rhs.x;
30        self.y += rhs.y;
31        self
32    }
33}
34
35impl<V: Into<Vec2>> AddAssign<V> for Vec2 {
36    fn add_assign(&mut self, rhs: V) {
37        let rhs = rhs.into();
38        self.x += rhs.x;
39        self.y += rhs.y;
40    }
41}
42
43impl<V: Into<Vec2>> Sub<V> for Vec2 {
44    type Output = Vec2;
45    fn sub(mut self, rhs: V) -> Self::Output {
46        let rhs = rhs.into();
47        self.x -= rhs.x;
48        self.y -= rhs.y;
49        self
50    }
51}
52
53impl<V: Into<Vec2>> SubAssign<V> for Vec2 {
54    fn sub_assign(&mut self, rhs: V) {
55        let rhs = rhs.into();
56        self.x -= rhs.x;
57        self.y -= rhs.y;
58    }
59}
60
61/// Creates a Vec2 from the given inputs.
62pub fn vec2(x: u16, y: u16) -> Vec2 {
63    Vec2 { x, y }
64}