1#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
3pub struct Vector<T = f32> {
4 pub x: T,
6
7 pub y: T,
9}
10
11impl<T> Vector<T> {
12 pub const fn new(x: T, y: T) -> Self {
14 Self { x, y }
15 }
16}
17
18impl Vector {
19 pub const ZERO: Self = Self::new(0.0, 0.0);
21
22 pub fn round(self) -> Self {
24 Self {
25 x: self.x.round(),
26 y: self.y.round(),
27 }
28 }
29}
30
31impl<T> std::ops::Neg for Vector<T>
32where
33 T: std::ops::Neg<Output = T>,
34{
35 type Output = Self;
36
37 fn neg(self) -> Self::Output {
38 Self::new(-self.x, -self.y)
39 }
40}
41
42impl<T> std::ops::Add for Vector<T>
43where
44 T: std::ops::Add<Output = T>,
45{
46 type Output = Self;
47
48 fn add(self, b: Self) -> Self {
49 Self::new(self.x + b.x, self.y + b.y)
50 }
51}
52
53impl<T> std::ops::AddAssign for Vector<T>
54where
55 T: std::ops::AddAssign,
56{
57 fn add_assign(&mut self, b: Self) {
58 self.x += b.x;
59 self.y += b.y;
60 }
61}
62
63impl<T> std::ops::Sub for Vector<T>
64where
65 T: std::ops::Sub<Output = T>,
66{
67 type Output = Self;
68
69 fn sub(self, b: Self) -> Self {
70 Self::new(self.x - b.x, self.y - b.y)
71 }
72}
73
74impl<T> std::ops::SubAssign for Vector<T>
75where
76 T: std::ops::SubAssign,
77{
78 fn sub_assign(&mut self, b: Self) {
79 self.x -= b.x;
80 self.y -= b.y;
81 }
82}
83
84impl<T> std::ops::Mul<T> for Vector<T>
85where
86 T: std::ops::Mul<Output = T> + Copy,
87{
88 type Output = Self;
89
90 fn mul(self, scale: T) -> Self {
91 Self::new(self.x * scale, self.y * scale)
92 }
93}
94
95impl<T> std::ops::MulAssign<T> for Vector<T>
96where
97 T: std::ops::MulAssign + Copy,
98{
99 fn mul_assign(&mut self, scale: T) {
100 self.x *= scale;
101 self.y *= scale;
102 }
103}
104
105impl<T> std::ops::Div<T> for Vector<T>
106where
107 T: std::ops::Div<Output = T> + Copy,
108{
109 type Output = Self;
110
111 fn div(self, scale: T) -> Self {
112 Self::new(self.x / scale, self.y / scale)
113 }
114}
115
116impl<T> std::ops::DivAssign<T> for Vector<T>
117where
118 T: std::ops::DivAssign + Copy,
119{
120 fn div_assign(&mut self, scale: T) {
121 self.x /= scale;
122 self.y /= scale;
123 }
124}
125
126impl<T> From<[T; 2]> for Vector<T> {
127 fn from([x, y]: [T; 2]) -> Self {
128 Self::new(x, y)
129 }
130}
131
132impl<T> From<Vector<T>> for [T; 2]
133where
134 T: Copy,
135{
136 fn from(other: Vector<T>) -> Self {
137 [other.x, other.y]
138 }
139}