1use std::ops::{Add, Div, Mul, Sub};
2
3#[derive(Clone, Copy, Debug, PartialEq, Hash)]
4pub struct Vector2 {
5 pub x: isize,
6 pub y: isize,
7}
8
9impl Vector2 {
10 pub fn new(x: isize, y: isize) -> Self {
11 Vector2 { x, y }
12 }
13}
14
15impl<T> From<(T, T)> for Vector2
16where
17 T: TryInto<isize>,
18 <T as TryInto<isize>>::Error: std::fmt::Debug,
19{
20 fn from(value: (T, T)) -> Self {
21 Vector2 {
22 x: value.0.try_into().expect("Conversion to isize failed"),
23 y: value.1.try_into().expect("Conversion to isize failed"),
24 }
25 }
26}
27
28impl Add for Vector2 {
29 type Output = Self;
30
31 fn add(self, rhs: Self) -> Self::Output {
32 Vector2 {
33 x: self.x + rhs.x,
34 y: self.y + rhs.y,
35 }
36 }
37}
38
39impl Sub for Vector2 {
40 type Output = Self;
41
42 fn sub(self, rhs: Self) -> Self::Output {
43 Vector2 {
44 x: self.x - rhs.x,
45 y: self.y - rhs.y,
46 }
47 }
48}
49
50impl Mul for Vector2 {
51 type Output = Self;
52
53 fn mul(self, rhs: Self) -> Self::Output {
54 Vector2 {
55 x: self.x * rhs.x,
56 y: self.y * rhs.y,
57 }
58 }
59}
60
61impl Div for Vector2 {
62 type Output = Self;
63
64 fn div(self, rhs: Self) -> Self::Output {
65 Vector2 {
66 x: self.x / rhs.x,
67 y: self.y / rhs.y,
68 }
69 }
70}
71
72#[derive(Clone, Copy, Debug, PartialEq, Hash)]
73pub struct Vector3 {
74 pub x: isize,
75 pub y: isize,
76 pub z: isize,
77}
78
79impl Vector3 {
80 pub fn new(x: isize, y: isize, z: isize) -> Self {
81 Vector3 { x, y, z }
82 }
83}
84
85impl<T> From<(T, T, T)> for Vector3
86where
87 T: TryInto<isize>,
88 <T as TryInto<isize>>::Error: std::fmt::Debug,
89{
90 fn from(value: (T, T, T)) -> Self {
91 Vector3 {
92 x: value.0.try_into().expect("Conversion to isize failed"),
93 y: value.1.try_into().expect("Conversion to isize failed"),
94 z: value.2.try_into().expect("Conversion to isize failed"),
95 }
96 }
97}
98
99impl Add for Vector3 {
100 type Output = Self;
101
102 fn add(self, rhs: Self) -> Self::Output {
103 Vector3 {
104 x: self.x + rhs.x,
105 y: self.y + rhs.y,
106 z: self.z + rhs.z,
107 }
108 }
109}
110
111impl Sub for Vector3 {
112 type Output = Self;
113
114 fn sub(self, rhs: Self) -> Self::Output {
115 Vector3 {
116 x: self.x - rhs.x,
117 y: self.y - rhs.y,
118 z: self.z - rhs.z,
119 }
120 }
121}
122
123impl Mul for Vector3 {
124 type Output = Self;
125
126 fn mul(self, rhs: Self) -> Self::Output {
127 Vector3 {
128 x: self.x * rhs.x,
129 y: self.y * rhs.y,
130 z: self.z * rhs.z,
131 }
132 }
133}
134
135impl Div for Vector3 {
136 type Output = Self;
137
138 fn div(self, rhs: Self) -> Self::Output {
139 Vector3 {
140 x: self.x / rhs.x,
141 y: self.y / rhs.y,
142 z: self.z / rhs.z,
143 }
144 }
145}
146
147#[cfg(test)]
148mod tests {
149 use crate::vector::{Vector2, Vector3};
150
151 #[test]
152 fn test_vector2_from() {
153 let point = Vector2::from((8, 56));
154 assert_eq!(point, Vector2::new(8, 56));
155 }
156
157 #[test]
158 fn test_vector3_from() {
159 let point = Vector3::from((8, 56, -43));
160 assert_eq!(point, Vector3::new(8, 56, -43));
161 }
162
163 #[test]
164 fn test_vector2_add() {
165 let point_a = Vector2::from((2, 3));
166 let point_b = Vector2::from((4, 7));
167 assert_eq!(point_a + point_b, Vector2::new(6, 10));
168 }
169
170 #[test]
171 fn test_vector3_add() {
172 let point_a = Vector3::from((2, 3, 8));
173 let point_b = Vector3::from((4, 7, 1));
174 assert_eq!(point_a + point_b, Vector3::new(6, 10, 9));
175 }
176
177 #[test]
178 fn test_vector2_sub() {
179 let point_a = Vector2::from((2, 3));
180 let point_b = Vector2::from((4, 7));
181 assert_eq!(point_a - point_b, Vector2::new(-2, -4));
182 }
183
184 #[test]
185 fn test_vector3_sub() {
186 let point_a = Vector3::from((2, 3, 8));
187 let point_b = Vector3::from((4, 7, 1));
188 assert_eq!(point_a - point_b, Vector3::new(-2, -4, 7));
189 }
190
191 #[test]
192 fn test_vector2_mul() {
193 let point_a = Vector2::from((2, 3));
194 let point_b = Vector2::from((4, 7));
195 assert_eq!(point_a * point_b, Vector2::new(8, 21));
196 }
197
198 #[test]
199 fn test_vector_3_mul() {
200 let point_a = Vector3::from((2, 3, 8));
201 let point_b = Vector3::from((4, 7, 1));
202 assert_eq!(point_a * point_b, Vector3::new(8, 21, 8));
203 }
204
205 #[test]
206 fn test_vector2_div() {
207 let point_a = Vector2::from((2, 3));
208 let point_b = Vector2::from((4, 7));
209 assert_eq!(point_b / point_a, Vector2::new(2, 2));
210 }
211
212 #[test]
213 fn test_vector3_div() {
214 let point_a = Vector3::from((2, 3, 8));
215 let point_b = Vector3::from((4, 7, 1));
216 assert_eq!(point_b / point_a, Vector3::new(2, 2, 0));
217 }
218}