1use std::ops::{
2 Add,
3 AddAssign,
4
5 Div,
6 DivAssign,
7
8 Mul,
9 MulAssign,
10
11 Neg,
12 Sub,
13 SubAssign,
14};
15
16use std::convert::From;
17
18use serde::{
19 Deserialize,
20 Serialize,
21};
22
23#[derive(Copy, Clone, Default, Debug, PartialEq, PartialOrd, Serialize, Deserialize)]
24pub struct Vector2 {
25 pub x: f32,
26 pub y: f32,
27}
28
29impl Vector2 {
30 pub const ZERO: Self = Self::new(0.0, 0.0);
31 pub const ONE: Self = Self::new(1.0, 1.0);
32
33 pub const RIGHT: Self = Self::new(1.0, 0.0);
34 pub const UP: Self = Self::new(0.0, 1.0);
35
36 pub const INFINITY: Self = Self::new(f32::INFINITY, f32::INFINITY);
37
38 pub const fn new(x: f32, y: f32) -> Self {
39 Self { x: x, y: y }
40 }
41
42 pub fn from_rad(theta: f32) -> Self {
43 Self {
44 x: theta.sin(),
45 y: theta.cos(),
46 }
47 }
48
49 pub const fn dot(self, rhs: Self) -> f32 {
50 self.x * rhs.x + self.y * rhs.y
51 }
52
53 pub const fn cross(self, rhs: Self) -> f32 {
54 self.x * rhs.y - self.y * rhs.x
55 }
56
57 pub const fn perp(self) -> Self {
58 Self::new(self.y, -self.x)
59 }
60
61 pub const fn len_sq(self) -> f32 {
62 self.dot(self)
63 }
64
65 pub fn len(self) -> f32 {
66 self.len_sq().sqrt()
67 }
68
69 pub fn abs(self) -> Self {
70 Vector2::new(self.x.abs(), self.y.abs())
71 }
72
73 pub const fn is_finite(self) -> bool {
74 self.x.is_finite() && self.y.is_finite()
75 }
76
77 pub const fn is_nan(self) -> bool {
78 self.x.is_nan() || self.y.is_nan()
79 }
80
81 pub const fn min(self, other: Self) -> Self {
82 let x = if self.x < other.x { self.x } else { other.x };
83
84 let y = if self.y < other.y { self.y } else { other.y };
85
86 Self::new(x, y)
87 }
88
89 pub const fn max(self, other: Self) -> Self {
90 let x = if self.x > other.x { self.x } else { other.x };
91
92 let y = if self.y > other.y { self.y } else { other.y };
93
94 Self::new(x, y)
95 }
96
97 pub const fn min_elem(self) -> f32 {
98 if self.x < self.y {
99 self.x
100 } else {
101 self.y
102 }
103 }
104
105 pub const fn max_elem(self) -> f32 {
106 if self.x > self.y {
107 self.x
108 } else {
109 self.y
110 }
111 }
112}
113
114impl Add for Vector2 {
115 type Output = Self;
116
117 fn add(self, rhs: Self) -> Self::Output {
118 Self {
119 x: self.x + rhs.x,
120 y: self.y + rhs.y,
121 }
122 }
123}
124
125impl Add<f32> for Vector2 {
126 type Output = Self;
127
128 fn add(self, rhs: f32) -> Self::Output {
129 Self {
130 x: self.x + rhs,
131 y: self.y + rhs,
132 }
133 }
134}
135
136impl AddAssign for Vector2 {
137 fn add_assign(&mut self, rhs: Self) {
138 self.x += rhs.x;
139 self.y += rhs.y;
140 }
141}
142
143impl AddAssign<f32> for Vector2 {
144 fn add_assign(&mut self, rhs: f32) {
145 self.x += rhs;
146 self.y += rhs;
147 }
148}
149
150impl Sub for Vector2 {
151 type Output = Self;
152
153 fn sub(self, rhs: Self) -> Self::Output {
154 Self {
155 x: self.x - rhs.x,
156 y: self.y - rhs.y,
157 }
158 }
159}
160
161impl Sub<f32> for Vector2 {
162 type Output = Self;
163
164 fn sub(self, rhs: f32) -> Self::Output {
165 Self {
166 x: self.x - rhs,
167 y: self.y - rhs,
168 }
169 }
170}
171
172impl SubAssign for Vector2 {
173 fn sub_assign(&mut self, rhs: Self) {
174 self.x -= rhs.x;
175 self.y -= rhs.y;
176 }
177}
178
179impl SubAssign<f32> for Vector2 {
180 fn sub_assign(&mut self, rhs: f32) {
181 self.x -= rhs;
182 self.y -= rhs;
183 }
184}
185
186impl Mul for Vector2 {
187 type Output = Self;
188
189 fn mul(self, rhs: Self) -> Self::Output {
190 Self {
191 x: self.x * rhs.x,
192 y: self.y * rhs.y,
193 }
194 }
195}
196
197impl Mul<f32> for Vector2 {
198 type Output = Self;
199
200 fn mul(self, rhs: f32) -> Self::Output {
201 Self {
202 x: self.x * rhs,
203 y: self.y * rhs,
204 }
205 }
206}
207
208impl MulAssign for Vector2 {
209 fn mul_assign(&mut self, rhs: Self) {
210 self.x *= rhs.x;
211 self.y *= rhs.y;
212 }
213}
214
215impl MulAssign<f32> for Vector2 {
216 fn mul_assign(&mut self, rhs: f32) {
217 self.x *= rhs;
218 self.y *= rhs;
219 }
220}
221
222impl Div for Vector2 {
223 type Output = Self;
224
225 fn div(self, rhs: Self) -> Self::Output {
226 Self {
227 x: self.x / rhs.x,
228 y: self.y / rhs.y,
229 }
230 }
231}
232
233impl Div<f32> for Vector2 {
234 type Output = Self;
235
236 fn div(self, rhs: f32) -> Self::Output {
237 Self {
238 x: self.x / rhs,
239 y: self.y / rhs,
240 }
241 }
242}
243
244impl DivAssign for Vector2 {
245 fn div_assign(&mut self, rhs: Self) {
246 self.x /= rhs.x;
247 self.y /= rhs.y;
248 }
249}
250
251impl DivAssign<f32> for Vector2 {
252 fn div_assign(&mut self, rhs: f32) {
253 self.x /= rhs;
254 self.y /= rhs;
255 }
256}
257
258impl Neg for Vector2 {
259 type Output = Self;
260 fn neg(self) -> Self::Output {
261 Self {
262 x: -self.x,
263 y: -self.y,
264 }
265 }
266}
267
268impl From<(f32, f32)> for Vector2 {
269 fn from(xy: (f32, f32)) -> Self {
270 let (x, y) = xy;
271 Self { x, y }
272 }
273}
274
275impl From<[f32; 2]> for Vector2 {
276 fn from(xy: [f32; 2]) -> Self {
277 Self { x: xy[0], y: xy[1] }
278 }
279}