1use std::ops::{Add, Sub, Neg, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
2
3#[derive(PartialEq, Copy, Clone, Debug)]
8pub struct Vec2 {
9 pub x: f32,
11 pub y: f32,
13}
14
15impl Vec2 {
16 pub fn new(x: f32, y: f32) -> Vec2 {
18 Vec2 { x, y }
19 }
20
21 #[inline]
31 pub fn len(&self) -> f32 {
32 self.sqr_len().sqrt()
33 }
34
35 #[inline]
46 pub fn sqr_len(&self) -> f32 {
47 self.x * self.x + self.y * self.y
48 }
49
50 #[inline]
62 pub fn dot(&self, b: &Vec2) -> f32 {
63 self.x * b.x + self.y * b.y
64 }
65
66 #[inline]
80 pub fn normalized(self) -> Vec2 {
81 let len = self.len();
82 if len == 0.0 {
83 Vec2::ZERO
84 } else {
85 self / len
86 }
87 }
88
89 #[inline]
102 pub fn min(&self, other: &Vec2) -> Vec2 {
103 Vec2::new(self.x.min(other.x), self.y.min(other.y))
104 }
105
106 #[inline]
119 pub fn max(&self, other: &Vec2) -> Vec2 {
120 Vec2::new(self.x.max(other.x), self.y.max(other.y))
121 }
122
123 pub const ZERO: Vec2 = Vec2 { x: 0.0, y: 0.0 };
124
125 pub const UP: Vec2 = Vec2 { x: 0.0, y: 1.0 };
126
127 pub const RIGHT: Vec2 = Vec2 { x: 1.0, y: 0.0 };
128
129 pub const DOWN: Vec2 = Vec2 { x: 0.0, y: -1.0 };
130
131 pub const LEFT: Vec2 = Vec2 { x: -1.0, y: 0.0 };
132
133 pub const ONE: Vec2 = Vec2 { x: 1.0, y: 1.0 };
134}
135
136pub trait Cross<RHS = Self> {
138 type Output;
140
141 fn cross(self, other: RHS) -> Self::Output;
143}
144
145impl Cross for Vec2 {
146 type Output = f32;
147
148 fn cross(self, other: Vec2) -> f32 {
149 self.x * other.y - self.y * other.x
150 }
151}
152
153impl<'a> Cross<Vec2> for &'a Vec2 {
154 type Output = f32;
155
156 fn cross(self, other: Vec2) -> f32 {
157 self.x * other.y - self.y * other.x
158 }
159}
160
161impl<'b> Cross<&'b Vec2> for Vec2 {
162 type Output = f32;
163
164 fn cross(self, other: &'b Vec2) -> f32 {
165 self.x * other.y - self.y * other.x
166 }
167}
168
169impl<'a, 'b> Cross<&'b Vec2> for &'a Vec2 {
170 type Output = f32;
171
172 fn cross(self, other: &'b Vec2) -> f32 {
173 self.x * other.y - self.y * other.x
174 }
175}
176
177impl Cross<f32> for Vec2 {
178 type Output = Vec2;
179
180 fn cross(self, s: f32) -> Vec2 {
181 Vec2::new(s * self.y, -s * self.x)
182 }
183}
184
185impl<'a> Cross<f32> for &'a Vec2 {
186 type Output = Vec2;
187
188 fn cross(self, s: f32) -> Vec2 {
189 Vec2::new(s * self.y, -s * self.x)
190 }
191}
192
193impl<'b> Cross<&'b f32> for Vec2 {
194 type Output = Vec2;
195
196 fn cross(self, s: &'b f32) -> Vec2 {
197 Vec2::new(s * self.y, -s * self.x)
198 }
199}
200
201impl<'a, 'b> Cross<&'b f32> for &'a Vec2 {
202 type Output = Vec2;
203
204 fn cross(self, s: &'b f32) -> Vec2 {
205 Vec2::new(s * self.y, -s * self.x)
206 }
207}
208
209impl Cross<Vec2> for f32 {
210 type Output = Vec2;
211
212 fn cross(self, other: Vec2) -> Vec2 {
213 -other.cross(self)
214 }
215}
216
217impl<'a> Cross<Vec2> for &'a f32 {
218 type Output = Vec2;
219
220 fn cross(self, other: Vec2) -> Vec2 {
221 -other.cross(self)
222 }
223}
224
225impl<'b> Cross<&'b Vec2> for f32 {
226 type Output = Vec2;
227
228 fn cross(self, other: &'b Vec2) -> Vec2 {
229 -other.cross(self)
230 }
231}
232
233impl<'a, 'b> Cross<&'b Vec2> for &'a f32 {
234 type Output = Vec2;
235
236 fn cross(self, other: &'b Vec2) -> Vec2 {
237 -other.cross(self)
238 }
239}
240
241
242impl Neg for Vec2 {
243 type Output = Self;
244
245 fn neg(self) -> Self {
246 Vec2::new(-self.x, -self.y)
247 }
248}
249
250impl<'a> Neg for &'a Vec2 {
251 type Output = Vec2;
252
253 fn neg(self) -> Vec2 {
254 Vec2::new(-self.x, -self.y)
255 }
256}
257
258
259impl Add for Vec2 {
260 type Output = Vec2;
261
262 fn add(self, other: Vec2) -> Vec2 {
263 Vec2::new(self.x + other.x, self.y + other.y)
264 }
265}
266
267impl<'a> Add<Vec2> for &'a Vec2 {
268 type Output = Vec2;
269
270 fn add(self, other: Vec2) -> Vec2 {
271 Vec2::new(self.x + other.x, self.y + other.y)
272 }
273}
274
275impl<'b> Add<&'b Vec2> for Vec2 {
276 type Output = Vec2;
277
278 fn add(self, other: &'b Vec2) -> Vec2 {
279 Vec2::new(self.x + other.x, self.y + other.y)
280 }
281}
282
283impl<'a, 'b> Add<&'b Vec2> for &'a Vec2 {
284 type Output = Vec2;
285
286 fn add(self, other: &'b Vec2) -> Vec2 {
287 Vec2::new(self.x + other.x, self.y + other.y)
288 }
289}
290
291impl AddAssign for Vec2 {
292 fn add_assign(&mut self, other: Vec2) {
293 *self = Vec2 {
294 x: self.x + other.x,
295 y: self.y + other.y,
296 };
297 }
298}
299
300impl<'b> AddAssign<&'b Vec2> for Vec2 {
301 fn add_assign(&mut self, other: &'b Vec2) {
302 *self = Vec2 {
303 x: self.x + other.x,
304 y: self.y + other.y,
305 };
306 }
307}
308
309
310impl Sub for Vec2 {
311 type Output = Vec2;
312
313 fn sub(self, other: Vec2) -> Vec2 {
314 Vec2::new(self.x - other.x, self.y - other.y)
315 }
316}
317
318impl<'a> Sub<Vec2> for &'a Vec2 {
319 type Output = Vec2;
320
321 fn sub(self, other: Vec2) -> Vec2 {
322 Vec2::new(self.x - other.x, self.y - other.y)
323 }
324}
325
326impl<'b> Sub<&'b Vec2> for Vec2 {
327 type Output = Vec2;
328
329 fn sub(self, other: &'b Vec2) -> Vec2 {
330 Vec2::new(self.x - other.x, self.y - other.y)
331 }
332}
333
334impl<'a, 'b> Sub<&'b Vec2> for &'a Vec2 {
335 type Output = Vec2;
336
337 fn sub(self, other: &'b Vec2) -> Vec2 {
338 Vec2::new(self.x - other.x, self.y - other.y)
339 }
340}
341
342impl SubAssign for Vec2 {
343 fn sub_assign(&mut self, other: Vec2) {
344 *self = Vec2 {
345 x: self.x - other.x,
346 y: self.y - other.y,
347 };
348 }
349}
350
351impl<'b> SubAssign<&'b Vec2> for Vec2 {
352 fn sub_assign(&mut self, other: &'b Vec2) {
353 *self = Vec2 {
354 x: self.x - other.x,
355 y: self.y - other.y,
356 };
357 }
358}
359
360
361impl Mul<f32> for Vec2 {
362 type Output = Vec2;
363
364 fn mul(self, s: f32) -> Vec2 {
365 Vec2::new(self.x * s, self.y * s)
366 }
367}
368
369impl<'a> Mul<f32> for &'a Vec2 {
370 type Output = Vec2;
371
372 fn mul(self, s: f32) -> Vec2 {
373 Vec2::new(self.x * s, self.y * s)
374 }
375}
376
377impl<'b> Mul<&'b f32> for Vec2 {
378 type Output = Vec2;
379
380 fn mul(self, s: &'b f32) -> Vec2 {
381 Vec2::new(self.x * s, self.y * s)
382 }
383}
384
385impl<'a, 'b> Mul<&'b f32> for &'a Vec2 {
386 type Output = Vec2;
387
388 fn mul(self, s: &'b f32) -> Vec2 {
389 Vec2::new(self.x * s, self.y * s)
390 }
391}
392
393impl Mul<Vec2> for f32 {
394 type Output = Vec2;
395
396 fn mul(self, v: Vec2) -> Vec2 {
397 Vec2::new(self * v.x, self * v.y)
398 }
399}
400
401impl<'a> Mul<Vec2> for &'a f32 {
402 type Output = Vec2;
403
404 fn mul(self, v: Vec2) -> Vec2 {
405 Vec2::new(self * v.x, self * v.y)
406 }
407}
408
409impl<'b> Mul<&'b Vec2> for f32 {
410 type Output = Vec2;
411
412 fn mul(self, v: &'b Vec2) -> Vec2 {
413 Vec2::new(self * v.x, self * v.y)
414 }
415}
416
417impl<'a, 'b> Mul<&'b Vec2> for &'a f32 {
418 type Output = Vec2;
419
420 fn mul(self, v: &'b Vec2) -> Vec2 {
421 Vec2::new(self * v.x, self * v.y)
422 }
423}
424
425impl MulAssign<f32> for Vec2 {
426 fn mul_assign(&mut self, s: f32) {
427 *self = Vec2 {
428 x: self.x * s,
429 y: self.y * s,
430 };
431 }
432}
433
434impl<'b> MulAssign<&'b f32> for Vec2 {
435 fn mul_assign(&mut self, s: &'b f32) {
436 *self = Vec2 {
437 x: self.x * s,
438 y: self.y * s,
439 };
440 }
441}
442
443
444impl Div<f32> for Vec2 {
445 type Output = Vec2;
446
447 fn div(self, s: f32) -> Vec2 {
448 self * (1.0 / s)
449 }
450}
451
452impl<'a> Div<f32> for &'a Vec2 {
453 type Output = Vec2;
454
455 fn div(self, s: f32) -> Vec2 {
456 self * (1.0 / s)
457 }
458}
459
460impl<'b> Div<&'b f32> for Vec2 {
461 type Output = Vec2;
462
463 fn div(self, s: &'b f32) -> Vec2 {
464 self * (1.0 / s)
465 }
466}
467
468impl<'a, 'b> Div<&'b f32> for &'a Vec2 {
469 type Output = Vec2;
470
471 fn div(self, s: &'b f32) -> Vec2 {
472 self * (1.0 / s)
473 }
474}
475
476impl DivAssign<f32> for Vec2 {
477 fn div_assign(&mut self, s: f32) {
478 *self *= 1.0 / s;
479 }
480}
481
482impl<'b> DivAssign<&'b f32> for Vec2 {
483 fn div_assign(&mut self, s: &'b f32) {
484 *self *= 1.0 / s;
485 }
486}