1use std::fmt;
2use std::fmt::Debug;
3use std::ops::{Add, Sub, Mul, Div, Neg};
4use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign};
5use crate::vec::{Vec, Math, Interpolation};
6use crate::vec3::Vec3;
7use crate::vec4::Vec4;
8use crate::consts::{Zero, One, UnitX, UnitY};
9
10#[repr(C)]
11#[derive(Debug, Copy, Clone, Default, Hash, Eq, PartialEq)]
12pub struct Vec2<T>
13{
14 pub x: T,
15 pub y: T,
16}
17
18impl<T> Neg for Vec2<T> where T:Neg<Output=T>
19{
20 type Output = Self;
21
22 fn neg(self) -> Self
23 {
24 Vec2
25 {
26 x: -self.x,
27 y: -self.y
28 }
29 }
30}
31
32impl<T> Add for Vec2<T> where T:Add<Output=T>
33{
34 type Output = Self;
35
36 fn add(self, other: Self) -> Self
37 {
38 Self
39 {
40 x: self.x + other.x,
41 y: self.y + other.y
42 }
43 }
44}
45
46impl<T> Sub for Vec2<T> where T:Sub<Output=T>
47{
48 type Output = Self;
49
50 fn sub(self, other: Self) -> Self
51 {
52 Self
53 {
54 x: self.x - other.x,
55 y: self.y - other.y
56 }
57 }
58}
59
60impl<T> Mul<T> for Vec2<T> where T:Mul<Output=T> + Copy
61{
62 type Output = Self;
63
64 fn mul(self, s: T) -> Self
65 {
66 Self
67 {
68 x:self.x * s,
69 y:self.y * s,
70 }
71 }
72}
73
74impl<T> Mul for Vec2<T> where T:Mul<Output=T>
75{
76 type Output = Self;
77
78 fn mul(self, other: Self) -> Self
79 {
80 Self
81 {
82 x: self.x * other.x,
83 y: self.y * other.y
84 }
85 }
86}
87
88impl<T> Div<T> for Vec2<T> where T:Div<Output=T> + Copy
89{
90 type Output = Self;
91
92 fn div(self, s: T) -> Self
93 {
94 Self
95 {
96 x:self.x / s,
97 y:self.y / s,
98 }
99 }
100}
101
102impl<T> Div for Vec2<T> where T:Div<Output=T>
103{
104 type Output = Self;
105
106 fn div(self, other: Self) -> Self
107 {
108 Self
109 {
110 x: self.x / other.x,
111 y: self.y / other.y
112 }
113 }
114}
115
116impl<T> AddAssign for Vec2<T> where T:AddAssign<T>
117{
118 fn add_assign(&mut self, other: Self)
119 {
120 self.x += other.x;
121 self.y += other.y;
122 }
123}
124
125impl<T> SubAssign for Vec2<T> where T:SubAssign<T>
126{
127 fn sub_assign(&mut self, other: Self)
128 {
129 self.x -= other.x;
130 self.y -= other.y;
131 }
132}
133
134impl<T> MulAssign for Vec2<T> where T: MulAssign<T>
135{
136 fn mul_assign(&mut self, other: Self)
137 {
138 self.x *= other.x;
139 self.y *= other.y;
140 }
141}
142
143impl<T> MulAssign<T> for Vec2<T> where T:MulAssign<T> + Copy
144{
145 fn mul_assign(&mut self, s: T)
146 {
147 self.x *= s;
148 self.y *= s;
149 }
150}
151
152impl<'a, T> MulAssign<&'a T> for Vec2<T> where T:MulAssign<T> + Copy
153{
154 fn mul_assign(&mut self, other: &'a T)
155 {
156 self.x *= *other;
157 self.y *= *other;
158 }
159}
160
161impl<T> DivAssign for Vec2<T> where T:DivAssign<T>
162{
163 fn div_assign(&mut self, other: Self)
164 {
165 self.x /= other.x;
166 self.y /= other.y;
167 }
168}
169
170impl<T> DivAssign<T> for Vec2<T> where T:DivAssign<T> + Copy
171{
172 fn div_assign(&mut self, s: T)
173 {
174 self.x /= s;
175 self.y /= s;
176 }
177}
178
179impl<'a, T> DivAssign<&'a T> for Vec2<T> where T:DivAssign<T> + Copy
180{
181 fn div_assign(&mut self, s: &'a T)
182 {
183 self.x /= *s;
184 self.y /= *s;
185 }
186}
187
188impl<T> Vec2<T> where T: Copy
189{
190 #[inline(always)]
192 pub fn new(x: T, y: T) -> Self { Self { x, y } }
193
194 #[inline(always)]
196 pub fn len() -> usize
197 {
198 return 2;
199 }
200
201 #[inline(always)]
202 pub fn to_tuple(&self) -> (T, T)
203 {
204 (self.x, self.y)
205 }
206
207 #[inline(always)]
208 pub fn xx(&self) -> Self { Self::new(self.x, self.x) }
209 #[inline(always)]
210 pub fn xy(&self) -> Self { Self::new(self.x, self.y) }
211 #[inline(always)]
212 pub fn yx(&self) -> Self { Self::new(self.y, self.x) }
213 #[inline(always)]
214 pub fn yy(&self) -> Self { Self::new(self.y, self.y) }
215 #[inline(always)]
216 pub fn xxx(&self) -> Vec3<T> { Vec3::new(self.x, self.x, self.x) }
217 #[inline(always)]
218 pub fn xxy(&self) -> Vec3<T> { Vec3::new(self.x, self.x, self.y) }
219 #[inline(always)]
220 pub fn xyx(&self) -> Vec3<T> { Vec3::new(self.x, self.y, self.x) }
221 #[inline(always)]
222 pub fn xyy(&self) -> Vec3<T> { Vec3::new(self.x, self.y, self.y) }
223 #[inline(always)]
224 pub fn yxx(&self) -> Vec3<T> { Vec3::new(self.y, self.x, self.x) }
225 #[inline(always)]
226 pub fn yxy(&self) -> Vec3<T> { Vec3::new(self.y, self.x, self.y) }
227 #[inline(always)]
228 pub fn yyx(&self) -> Vec3<T> { Vec3::new(self.y, self.y, self.x) }
229 #[inline(always)]
230 pub fn yyy(&self) -> Vec3<T> { Vec3::new(self.y, self.y, self.y) }
231 #[inline(always)]
232 pub fn xxxx(&self) -> Vec4<T> { Vec4::new(self.x, self.x, self.x, self.x) }
233 #[inline(always)]
234 pub fn xxyx(&self) -> Vec4<T> { Vec4::new(self.x, self.x, self.y, self.x) }
235 #[inline(always)]
236 pub fn xyxx(&self) -> Vec4<T> { Vec4::new(self.x, self.y, self.x, self.x) }
237 #[inline(always)]
238 pub fn xyyx(&self) -> Vec4<T> { Vec4::new(self.x, self.y, self.y, self.x) }
239 #[inline(always)]
240 pub fn xxxy(&self) -> Vec4<T> { Vec4::new(self.x, self.x, self.x, self.y) }
241 #[inline(always)]
242 pub fn xxyy(&self) -> Vec4<T> { Vec4::new(self.x, self.x, self.y, self.y) }
243 #[inline(always)]
244 pub fn xyxy(&self) -> Vec4<T> { Vec4::new(self.x, self.y, self.x, self.y) }
245 #[inline(always)]
246 pub fn xyyy(&self) -> Vec4<T> { Vec4::new(self.x, self.y, self.y, self.y) }
247 #[inline(always)]
248 pub fn yxxx(&self) -> Vec4<T> { Vec4::new(self.y, self.x, self.x, self.x) }
249 #[inline(always)]
250 pub fn yxyx(&self) -> Vec4<T> { Vec4::new(self.y, self.x, self.y, self.x) }
251 #[inline(always)]
252 pub fn yyxx(&self) -> Vec4<T> { Vec4::new(self.y, self.y, self.x, self.x) }
253 #[inline(always)]
254 pub fn yyyx(&self) -> Vec4<T> { Vec4::new(self.y, self.y, self.y, self.x) }
255 #[inline(always)]
256 pub fn yxxy(&self) -> Vec4<T> { Vec4::new(self.y, self.x, self.x, self.y) }
257 #[inline(always)]
258 pub fn yxyy(&self) -> Vec4<T> { Vec4::new(self.y, self.x, self.y, self.y) }
259 #[inline(always)]
260 pub fn yyxy(&self) -> Vec4<T> { Vec4::new(self.y, self.y, self.x, self.y) }
261 #[inline(always)]
262 pub fn yyyy(&self) -> Vec4<T> { Vec4::new(self.y, self.y, self.y, self.y) }
263}
264
265impl<T> Vec2<T> where T:Vec + Math
266{
267 #[inline]
268 pub fn dot(&self, b: Self) -> T
269 {
270 return self.x * b.x + self.y * b.y;
271 }
272
273 #[inline]
274 pub fn cross(&self, b: Self) -> Self
275 {
276 Self
277 {
278 x:self.y * b.x - self.x * b.y,
279 y:self.x * b.y - self.y * b.x
280 }
281 }
282
283 #[inline]
284 pub fn length2(&self) -> T
285 {
286 return self.dot(*self);
287 }
288
289 #[inline]
290 pub fn length(&self) -> T
291 {
292 return self.length2().sqrt();
293 }
294
295 #[inline]
296 pub fn distance(&self, b: Self) -> T
297 {
298 return (*self - b).length();
299 }
300
301 #[inline]
302 pub fn normalize(&self) -> Self
303 {
304 let mag_sq = self.length2();
305 if mag_sq.gt(T::zero())
306 {
307 let inv_sqrt = T::one() / mag_sq.sqrt();
308 return *self * inv_sqrt;
309 }
310
311 return *self;
312 }
313}
314
315impl<T> Math for Vec2<T> where T:Copy + Math
316{
317 #[inline]
318 fn abs(self) -> Self
319 {
320 let mx = self.x.abs();
321 let my = self.y.abs();
322 Self { x: mx, y: my }
323 }
324
325 #[inline]
326 fn recip(self) -> Self
327 {
328 let mx = self.x.recip();
329 let my = self.y.recip();
330 Self { x: mx, y: my }
331 }
332
333 #[inline]
334 fn sqrt(self) -> Self
335 {
336 let mx = self.x.sqrt();
337 let my = self.y.sqrt();
338 Self { x: mx, y: my }
339 }
340
341 #[inline]
342 fn rsqrt(self) -> Self
343 {
344 let mx = self.x.rsqrt();
345 let my = self.y.rsqrt();
346 Self { x: mx, y: my }
347 }
348
349 #[inline]
350 fn sin(self) -> Self
351 {
352 let mx = self.x.sin();
353 let my = self.y.sin();
354 Self { x: mx, y: my }
355 }
356
357 #[inline]
358 fn cos(self) -> Self
359 {
360 let mx = self.x.cos();
361 let my = self.y.cos();
362 Self { x: mx, y: my }
363 }
364
365 #[inline]
366 fn tan(self) -> Self
367 {
368 let mx = self.x.tan();
369 let my = self.y.tan();
370 Self { x: mx, y: my }
371 }
372
373 #[inline]
374 fn sincos(self) -> (Self, Self)
375 {
376 let mx = self.x.sincos();
377 let my = self.y.sincos();
378 (
379 Self { x: mx.0, y: my.0 },
380 Self { x: mx.1, y: my.1 }
381 )
382 }
383
384 #[inline]
385 fn acos(self) -> Self
386 {
387 let mx = self.x.acos();
388 let my = self.y.acos();
389 Self { x: mx, y: my }
390 }
391
392 #[inline]
393 fn asin(self) -> Self
394 {
395 let mx = self.x.asin();
396 let my = self.y.asin();
397 Self { x: mx, y: my }
398 }
399
400 #[inline]
401 fn atan(self) -> Self
402 {
403 let mx = self.x.atan();
404 let my = self.y.atan();
405 Self { x: mx, y: my }
406 }
407
408 #[inline]
409 fn exp(self) -> Self
410 {
411 let mx = self.x.exp();
412 let my = self.y.exp();
413 Self { x: mx, y: my }
414 }
415
416 #[inline]
417 fn exp2(self) -> Self
418 {
419 let mx = self.x.exp2();
420 let my = self.y.exp2();
421 Self { x: mx, y: my }
422 }
423
424 #[inline]
425 fn log(self, _rhs:Self) -> Self
426 {
427 let mx = self.x.log(_rhs.x);
428 let my = self.y.log(_rhs.y);
429 Self { x: mx, y: my }
430 }
431
432 #[inline]
433 fn log2(self) -> Self
434 {
435 let mx = self.x.log2();
436 let my = self.y.log2();
437 Self { x: mx, y: my }
438 }
439
440 #[inline]
441 fn log10(self) -> Self
442 {
443 let mx = self.x.log10();
444 let my = self.y.log10();
445 Self { x: mx, y: my }
446 }
447
448 #[inline]
449 fn to_radians(self) -> Self
450 {
451 let mx = self.x.to_radians();
452 let my = self.y.to_radians();
453 Self { x: mx, y: my }
454 }
455
456 #[inline]
457 fn to_degrees(self) -> Self
458 {
459 let mx = self.x.to_degrees();
460 let my = self.y.to_degrees();
461 Self { x: mx, y: my }
462 }
463
464 #[inline]
465 fn min(self, _rhs: Self) -> Self
466 {
467 let mx = self.x.min(_rhs.x);
468 let my = self.y.min(_rhs.y);
469 Self { x: mx, y: my }
470 }
471
472 #[inline]
473 fn max(self, _rhs: Self) -> Self
474 {
475 let mx = self.x.max(_rhs.x);
476 let my = self.y.max(_rhs.y);
477 Self { x: mx, y: my }
478 }
479
480 #[inline]
481 fn saturate(self) -> Self
482 {
483 let mx = self.x.saturate();
484 let my = self.y.saturate();
485 Self { x: mx, y: my }
486 }
487
488 #[inline]
489 fn snorm2unorm(self) -> Self
490 {
491 let mx = self.x.snorm2unorm();
492 let my = self.y.snorm2unorm();
493 Self { x: mx, y: my }
494 }
495
496 #[inline]
497 fn unorm2snorm(self) -> Self
498 {
499 let mx = self.x.unorm2snorm();
500 let my = self.y.unorm2snorm();
501 Self { x: mx, y: my }
502 }
503
504 #[inline]
505 fn clamp(self, minval: Self, maxval: Self) -> Self
506 {
507 let mx = self.x.clamp(minval.x, maxval.x);
508 let my = self.y.clamp(minval.y, maxval.y);
509 Self { x: mx, y: my }
510 }
511}
512
513impl<T> Interpolation<T> for Vec2<T> where T: Copy + One + Mul<Output=T> + Add<Output=T> + Sub<Output=T>
514{
515 #[inline(always)]
516 fn lerp(self, b: Self, t: T) -> Self
517 {
518 return self*(T::one() - t) + b*t;
519 }
520}
521
522impl<T> Zero for Vec2<T> where T:Zero
523{
524 #[inline(always)]
525 fn zero() -> Self
526 {
527 Self
528 {
529 x: T::zero(), y: T::zero()
530 }
531 }
532}
533
534impl<T> One for Vec2<T> where T:One
535{
536 #[inline(always)]
537 fn one() -> Self
538 {
539 Self
540 {
541 x: T::one(), y: T::one()
542 }
543 }
544}
545
546impl<T> UnitX for Vec2<T> where T:One + Zero
547{
548 #[inline(always)]
549 fn unit_x() -> Self
550 {
551 Self
552 {
553 x: T::one(), y: T::zero()
554 }
555 }
556}
557
558impl<T> UnitY for Vec2<T> where T:One + Zero
559{
560 #[inline(always)]
561 fn unit_y() -> Self
562 {
563 Self
564 {
565 x: T::zero(), y: T::one()
566 }
567 }
568}
569
570impl<T> fmt::Display for Vec2<T> where T:Debug
571{
572 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
573 {
574 write!(f, "({:?}, {:?})", self.x, self.y)
575 }
576}
577
578impl<T> fmt::Binary for Vec2<T> where T:Vec + Math
579{
580 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
581 {
582 let len = self.length();
583 let decimals = f.precision().unwrap_or(3);
584 let string = format!("{:.*?}", decimals, len);
585 f.pad_integral(true, "", &string)
586 }
587}
588
589impl<T> From<[T;2]> for Vec2<T> where T:Copy
590{
591 fn from(v:[T;2]) -> Self
592 {
593 Self
594 {
595 x:v[0],
596 y:v[1]
597 }
598 }
599}
600
601impl<T> From<(T,T)> for Vec2<T> where T:Copy
602{
603 fn from(v:(T,T)) -> Self
604 {
605 Self
606 {
607 x:v.0,
608 y:v.1,
609 }
610 }
611}
612
613impl<T> AsRef<Vec2<T>> for Vec2<T>
614{
615 fn as_ref(&self) -> &Vec2<T>
616 {
617 self
618 }
619}
620
621impl<T> AsMut<Vec2<T>> for Vec2<T>
622{
623 fn as_mut(&mut self) -> &mut Vec2<T>
624 {
625 self
626 }
627}