1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::{
    cmp::PartialEq,
    fmt::{Display, Result},
    ops::{Add, AddAssign, Rem, RemAssign, Sub, SubAssign},
};

/// Raw Vector2 type
#[derive(Debug, Copy, PartialEq)]
pub struct Vector2<T: Clone> {
    pub x: T,
    pub y: T,
}

impl<T: Clone> Vector2<T> {
    pub const fn new(x: T, y: T) -> Self {
        Vector2 { x, y }
    }

    pub fn as_tuple(&self) -> (T, T) {
        (self.x.clone(), self.y.clone())
    }
}

impl<T: Clone> Clone for Vector2<T> {
    fn clone(&self) -> Self {
        Self {
            x: self.x.clone(),
            y: self.y.clone(),
        }
    }
}

impl<T: Clone + Add<Output = T>> Add<Vector2<T>> for Vector2<T> {
    type Output = Vector2<T>;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl<T: Clone + AddAssign> AddAssign<Vector2<T>> for Vector2<T> {
    fn add_assign(&mut self, rhs: Self) {
        self.x += rhs.x;
        self.y += rhs.y;
    }
}

impl<T: Clone + Sub<Output = T>> Sub<Vector2<T>> for Vector2<T> {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

impl<T: Clone + SubAssign> SubAssign<Vector2<T>> for Vector2<T> {
    fn sub_assign(&mut self, rhs: Self) {
        self.x -= rhs.x;
        self.y -= rhs.y;
    }
}

impl<T: Clone> From<(T, T)> for Vector2<T> {
    fn from(value: (T, T)) -> Self {
        Self {
            x: value.0,
            y: value.1,
        }
    }
}

/// A pair of `isize` used for coordinates, size or direction on a 2D plane
pub type Vec2D = Vector2<isize>;

impl Vec2D {
    /// A Vec2D of (0,0)
    pub const ZERO: Vec2D = Vec2D::new(0, 0);
}

impl Rem for Vec2D {
    type Output = Self;
    fn rem(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x.rem_euclid(rhs.x),
            y: self.y.rem_euclid(rhs.y),
        }
    }
}

impl Display for Vec2D {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
        write!(f, "Vec2D({}, {})", self.x, self.y)
    }
}

impl RemAssign for Vec2D {
    fn rem_assign(&mut self, rhs: Self) {
        self.x = self.x.rem_euclid(rhs.x);
        self.y = self.y.rem_euclid(rhs.y);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn add_vec2() {
        assert_eq!(
            Vector2::new(15, -3),
            Vector2::new(13, 4) + Vector2::new(2, -7)
        );
    }

    #[test]
    fn subtract_vec2() {
        assert_eq!(
            Vector2::new(2, -10),
            Vector2::new(17, 4) - Vector2::new(15, 14)
        );
    }

    #[test]
    fn rem_vec2_over() {
        assert_eq!(
            Vector2::new(4, 1),
            Vector2::new(9, 11) % Vector2::new(5, 10)
        )
    }

    #[test]
    fn rem_vec2_under() {
        assert_eq!(
            Vector2::new(4, 1),
            Vector2::new(-1, -109) % Vector2::new(5, 10)
        )
    }

    #[test]
    fn eq_vec2_both() {
        assert_eq!(Vector2::new(5, 4), Vector2::new(5, 4))
    }

    #[test]
    fn eq_vec2_only_one() {
        assert_ne!(Vector2::new(5, 2), Vector2::new(5, 4))
    }

    #[test]
    fn eq_vec2_neither() {
        assert_ne!(Vector2::new(17, 2), Vector2::new(5, 4))
    }
}