1use std::{fmt::Display, ops};
2
3#[repr(C)]
4#[derive(Debug, Clone, Copy)]
5pub struct Vector2 {
6 pub x: f32,
7 pub y: f32,
8}
9
10#[repr(C)]
11#[derive(Debug, Clone, Copy)]
12pub struct Vector2i {
13 pub x: i32,
14 pub y: i32,
15}
16
17impl Vector2 {
18 pub fn new(x: f32, y: f32) -> Self {
19 Self { x, y }
20 }
21
22 pub fn zero() -> Self {
23 Self { x: 0., y: 0. }
24 }
25
26 pub fn normalized(&self) -> Self {
27 let mut result = *self;
28 result.normalize();
29 result
30 }
31
32 pub fn normalize(&mut self) {
33 let length = ((self.x * self.x) + (self.y * self.y)).sqrt();
34
35 if length > 0. {
36 let ilength = 1. / length;
37 self.x *= ilength;
38 self.y *= ilength;
39 } else {
40 self.x = 0.;
41 self.y = 0.;
42 }
43 }
44
45 pub fn to_vector2i(&self) -> Vector2i {
46 Vector2i {
47 x: self.x as i32,
48 y: self.y as i32,
49 }
50 }
51}
52
53impl Vector2i {
54 pub fn new(x: i32, y: i32) -> Self {
55 Self { x, y }
56 }
57
58 pub fn zero() -> Self {
59 Self { x: 0, y: 0 }
60 }
61
62 pub fn to_vector2(&self) -> Vector2 {
63 Vector2 {
64 x: self.x as f32,
65 y: self.y as f32,
66 }
67 }
68}
69
70impl Display for Vector2 {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 f.write_str(format!("Vector2{{x: {}, y: {}}}", self.x, self.y).as_str())
73 }
74}
75
76impl Display for Vector2i {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 f.write_str(format!("Vector2i{{x: {}, y: {}}}", self.x, self.y).as_str())
79 }
80}
81
82impl From<Vector2i> for Vector2 {
83 fn from(value: Vector2i) -> Self {
84 Self {
85 x: value.x as f32,
86 y: value.y as f32,
87 }
88 }
89}
90
91impl From<&Vector2i> for Vector2 {
92 fn from(value: &Vector2i) -> Self {
93 value.to_owned().into()
94 }
95}
96
97impl From<Vector2> for Vector2i {
98 fn from(value: Vector2) -> Self {
99 Self {
100 x: value.x as i32,
101 y: value.y as i32,
102 }
103 }
104}
105
106impl From<&Vector2> for Vector2i {
107 fn from(value: &Vector2) -> Self {
108 value.to_owned().into()
109 }
110}
111
112impl ops::Add<Vector2> for Vector2 {
113 type Output = Vector2;
114
115 fn add(self, rhs: Vector2) -> Self::Output {
116 Self::Output {
117 x: self.x + rhs.x,
118 y: self.y + rhs.y,
119 }
120 }
121}
122
123impl ops::Add<Vector2i> for Vector2 {
124 type Output = Vector2;
125
126 fn add(self, rhs: Vector2i) -> Self::Output {
127 Self::Output {
128 x: self.x + rhs.x as f32,
129 y: self.y + rhs.y as f32,
130 }
131 }
132}
133
134impl ops::Add<f32> for Vector2 {
135 type Output = Vector2;
136
137 fn add(self, rhs: f32) -> Self::Output {
138 Self::Output {
139 x: self.x + rhs,
140 y: self.y + rhs,
141 }
142 }
143}
144
145impl ops::Add<i32> for Vector2 {
146 type Output = Vector2;
147
148 fn add(self, rhs: i32) -> Self::Output {
149 Self::Output {
150 x: self.x + rhs as f32,
151 y: self.y + rhs as f32,
152 }
153 }
154}
155
156impl ops::Add<Vector2> for f32 {
157 type Output = Vector2;
158
159 fn add(self, rhs: Vector2) -> Self::Output {
160 Self::Output {
161 x: self + rhs.x,
162 y: self + rhs.y,
163 }
164 }
165}
166
167impl ops::Add<Vector2> for i32 {
168 type Output = Vector2;
169
170 fn add(self, rhs: Vector2) -> Self::Output {
171 Self::Output {
172 x: self as f32 + rhs.x,
173 y: self as f32 + rhs.y,
174 }
175 }
176}
177
178impl ops::Sub<Vector2> for Vector2 {
179 type Output = Vector2;
180
181 fn sub(self, rhs: Vector2) -> Self::Output {
182 Self::Output {
183 x: self.x - rhs.x,
184 y: self.y - rhs.y,
185 }
186 }
187}
188
189impl ops::Sub<Vector2i> for Vector2 {
190 type Output = Vector2;
191
192 fn sub(self, rhs: Vector2i) -> Self::Output {
193 Self::Output {
194 x: self.x - rhs.x as f32,
195 y: self.y - rhs.y as f32,
196 }
197 }
198}
199
200impl ops::Sub<f32> for Vector2 {
201 type Output = Vector2;
202
203 fn sub(self, rhs: f32) -> Self::Output {
204 Self::Output {
205 x: self.x - rhs,
206 y: self.y - rhs,
207 }
208 }
209}
210
211impl ops::Sub<i32> for Vector2 {
212 type Output = Vector2;
213
214 fn sub(self, rhs: i32) -> Self::Output {
215 Self::Output {
216 x: self.x - rhs as f32,
217 y: self.y - rhs as f32,
218 }
219 }
220}
221
222impl ops::Sub<Vector2> for f32 {
223 type Output = Vector2;
224
225 fn sub(self, rhs: Vector2) -> Self::Output {
226 Self::Output {
227 x: self - rhs.x,
228 y: self - rhs.y,
229 }
230 }
231}
232
233impl ops::Sub<Vector2> for i32 {
234 type Output = Vector2;
235
236 fn sub(self, rhs: Vector2) -> Self::Output {
237 Self::Output {
238 x: self as f32 - rhs.x,
239 y: self as f32 - rhs.y,
240 }
241 }
242}
243
244impl ops::Mul<Vector2> for Vector2 {
245 type Output = Vector2;
246
247 fn mul(self, rhs: Vector2) -> Self::Output {
248 Self::Output {
249 x: self.x * rhs.x,
250 y: self.y * rhs.y,
251 }
252 }
253}
254
255impl ops::Mul<Vector2i> for Vector2 {
256 type Output = Vector2;
257
258 fn mul(self, rhs: Vector2i) -> Self::Output {
259 Self::Output {
260 x: self.x * rhs.x as f32,
261 y: self.y * rhs.y as f32,
262 }
263 }
264}
265
266impl ops::Mul<f32> for Vector2 {
267 type Output = Vector2;
268
269 fn mul(self, rhs: f32) -> Self::Output {
270 Self::Output {
271 x: self.x * rhs,
272 y: self.y * rhs,
273 }
274 }
275}
276
277impl ops::Mul<i32> for Vector2 {
278 type Output = Vector2;
279
280 fn mul(self, rhs: i32) -> Self::Output {
281 Self::Output {
282 x: self.x * rhs as f32,
283 y: self.y * rhs as f32,
284 }
285 }
286}
287
288impl ops::Mul<Vector2> for f32 {
289 type Output = Vector2;
290
291 fn mul(self, rhs: Vector2) -> Self::Output {
292 Self::Output {
293 x: self * rhs.x,
294 y: self * rhs.y,
295 }
296 }
297}
298
299impl ops::Mul<Vector2> for i32 {
300 type Output = Vector2;
301
302 fn mul(self, rhs: Vector2) -> Self::Output {
303 Self::Output {
304 x: self as f32 * rhs.x,
305 y: self as f32 * rhs.y,
306 }
307 }
308}
309
310impl ops::Div<Vector2> for Vector2 {
311 type Output = Vector2;
312
313 fn div(self, rhs: Vector2) -> Self::Output {
314 Self::Output {
315 x: self.x / rhs.x,
316 y: self.y / rhs.y,
317 }
318 }
319}
320
321impl ops::Div<Vector2i> for Vector2 {
322 type Output = Vector2;
323
324 fn div(self, rhs: Vector2i) -> Self::Output {
325 Self::Output {
326 x: self.x / rhs.x as f32,
327 y: self.y / rhs.y as f32,
328 }
329 }
330}
331
332impl ops::Div<f32> for Vector2 {
333 type Output = Vector2;
334
335 fn div(self, rhs: f32) -> Self::Output {
336 Self::Output {
337 x: self.x / rhs,
338 y: self.y / rhs,
339 }
340 }
341}
342
343impl ops::Div<i32> for Vector2 {
344 type Output = Vector2;
345
346 fn div(self, rhs: i32) -> Self::Output {
347 Self::Output {
348 x: self.x / rhs as f32,
349 y: self.y / rhs as f32,
350 }
351 }
352}
353
354impl ops::Div<Vector2> for f32 {
355 type Output = Vector2;
356
357 fn div(self, rhs: Vector2) -> Self::Output {
358 Self::Output {
359 x: self / rhs.x,
360 y: self / rhs.y,
361 }
362 }
363}
364
365impl ops::Div<Vector2> for i32 {
366 type Output = Vector2;
367
368 fn div(self, rhs: Vector2) -> Self::Output {
369 Self::Output {
370 x: self as f32 / rhs.x,
371 y: self as f32 / rhs.y,
372 }
373 }
374}
375
376impl ops::AddAssign<Vector2> for Vector2 {
377 fn add_assign(&mut self, rhs: Vector2) {
378 self.x += rhs.x;
379 self.y += rhs.y;
380 }
381}
382
383impl ops::AddAssign<Vector2i> for Vector2 {
384 fn add_assign(&mut self, rhs: Vector2i) {
385 self.x += rhs.x as f32;
386 self.y += rhs.y as f32;
387 }
388}
389
390impl ops::AddAssign<f32> for Vector2 {
391 fn add_assign(&mut self, rhs: f32) {
392 self.x += rhs;
393 self.y += rhs;
394 }
395}
396
397impl ops::AddAssign<i32> for Vector2 {
398 fn add_assign(&mut self, rhs: i32) {
399 self.x += rhs as f32;
400 self.y += rhs as f32;
401 }
402}
403
404impl ops::SubAssign<Vector2> for Vector2 {
405 fn sub_assign(&mut self, rhs: Vector2) {
406 self.x -= rhs.x;
407 self.y -= rhs.y;
408 }
409}
410
411impl ops::SubAssign<Vector2i> for Vector2 {
412 fn sub_assign(&mut self, rhs: Vector2i) {
413 self.x -= rhs.x as f32;
414 self.y -= rhs.y as f32;
415 }
416}
417
418impl ops::SubAssign<f32> for Vector2 {
419 fn sub_assign(&mut self, rhs: f32) {
420 self.x -= rhs;
421 self.y -= rhs;
422 }
423}
424
425impl ops::SubAssign<i32> for Vector2 {
426 fn sub_assign(&mut self, rhs: i32) {
427 self.x -= rhs as f32;
428 self.y -= rhs as f32;
429 }
430}
431
432impl ops::MulAssign<Vector2> for Vector2 {
433 fn mul_assign(&mut self, rhs: Vector2) {
434 self.x *= rhs.x;
435 self.y *= rhs.y;
436 }
437}
438
439impl ops::MulAssign<Vector2i> for Vector2 {
440 fn mul_assign(&mut self, rhs: Vector2i) {
441 self.x *= rhs.x as f32;
442 self.y *= rhs.y as f32;
443 }
444}
445
446impl ops::MulAssign<f32> for Vector2 {
447 fn mul_assign(&mut self, rhs: f32) {
448 self.x *= rhs;
449 self.y *= rhs;
450 }
451}
452
453impl ops::MulAssign<i32> for Vector2 {
454 fn mul_assign(&mut self, rhs: i32) {
455 self.x *= rhs as f32;
456 self.y *= rhs as f32;
457 }
458}
459
460impl ops::DivAssign<Vector2> for Vector2 {
461 fn div_assign(&mut self, rhs: Vector2) {
462 self.x /= rhs.x;
463 self.y /= rhs.y;
464 }
465}
466
467impl ops::DivAssign<Vector2i> for Vector2 {
468 fn div_assign(&mut self, rhs: Vector2i) {
469 self.x /= rhs.x as f32;
470 self.y /= rhs.y as f32;
471 }
472}
473
474impl ops::DivAssign<f32> for Vector2 {
475 fn div_assign(&mut self, rhs: f32) {
476 self.x /= rhs;
477 self.y /= rhs;
478 }
479}
480
481impl ops::DivAssign<i32> for Vector2 {
482 fn div_assign(&mut self, rhs: i32) {
483 self.x /= rhs as f32;
484 self.y /= rhs as f32;
485 }
486}
487
488impl ops::Add<Vector2> for Vector2i {
489 type Output = Vector2i;
490
491 fn add(self, rhs: Vector2) -> Self::Output {
492 Self::Output {
493 x: self.x + rhs.x as i32,
494 y: self.y + rhs.y as i32,
495 }
496 }
497}
498
499impl ops::Add<Vector2i> for Vector2i {
500 type Output = Vector2i;
501
502 fn add(self, rhs: Vector2i) -> Self::Output {
503 Self::Output {
504 x: self.x + rhs.x,
505 y: self.y + rhs.y,
506 }
507 }
508}
509
510impl ops::Add<f32> for Vector2i {
511 type Output = Vector2i;
512
513 fn add(self, rhs: f32) -> Self::Output {
514 Self::Output {
515 x: self.x + rhs as i32,
516 y: self.y + rhs as i32,
517 }
518 }
519}
520
521impl ops::Add<i32> for Vector2i {
522 type Output = Vector2i;
523
524 fn add(self, rhs: i32) -> Self::Output {
525 Self::Output {
526 x: self.x + rhs,
527 y: self.y + rhs,
528 }
529 }
530}
531
532impl ops::Add<Vector2i> for f32 {
533 type Output = Vector2i;
534
535 fn add(self, rhs: Vector2i) -> Self::Output {
536 Self::Output {
537 x: self as i32 + rhs.x,
538 y: self as i32 + rhs.y,
539 }
540 }
541}
542
543impl ops::Add<Vector2i> for i32 {
544 type Output = Vector2i;
545
546 fn add(self, rhs: Vector2i) -> Self::Output {
547 Self::Output {
548 x: self + rhs.x,
549 y: self + rhs.y,
550 }
551 }
552}
553
554impl ops::Sub<Vector2> for Vector2i {
555 type Output = Vector2i;
556
557 fn sub(self, rhs: Vector2) -> Self::Output {
558 Self::Output {
559 x: self.x - rhs.x as i32,
560 y: self.y - rhs.y as i32,
561 }
562 }
563}
564
565impl ops::Sub<Vector2i> for Vector2i {
566 type Output = Vector2i;
567
568 fn sub(self, rhs: Vector2i) -> Self::Output {
569 Self::Output {
570 x: self.x - rhs.x,
571 y: self.y - rhs.y,
572 }
573 }
574}
575
576impl ops::Sub<f32> for Vector2i {
577 type Output = Vector2i;
578
579 fn sub(self, rhs: f32) -> Self::Output {
580 Self::Output {
581 x: self.x - rhs as i32,
582 y: self.y - rhs as i32,
583 }
584 }
585}
586
587impl ops::Sub<i32> for Vector2i {
588 type Output = Vector2i;
589
590 fn sub(self, rhs: i32) -> Self::Output {
591 Self::Output {
592 x: self.x - rhs,
593 y: self.y - rhs,
594 }
595 }
596}
597
598impl ops::Sub<Vector2i> for f32 {
599 type Output = Vector2i;
600
601 fn sub(self, rhs: Vector2i) -> Self::Output {
602 Self::Output {
603 x: self as i32 - rhs.x,
604 y: self as i32 - rhs.y,
605 }
606 }
607}
608
609impl ops::Sub<Vector2i> for i32 {
610 type Output = Vector2i;
611
612 fn sub(self, rhs: Vector2i) -> Self::Output {
613 Self::Output {
614 x: self - rhs.x,
615 y: self - rhs.y,
616 }
617 }
618}
619
620impl ops::Mul<Vector2> for Vector2i {
621 type Output = Vector2i;
622
623 fn mul(self, rhs: Vector2) -> Self::Output {
624 Self::Output {
625 x: self.x * rhs.x as i32,
626 y: self.y * rhs.y as i32,
627 }
628 }
629}
630
631impl ops::Mul<Vector2i> for Vector2i {
632 type Output = Vector2i;
633
634 fn mul(self, rhs: Vector2i) -> Self::Output {
635 Self::Output {
636 x: self.x * rhs.x,
637 y: self.y * rhs.y,
638 }
639 }
640}
641
642impl ops::Mul<f32> for Vector2i {
643 type Output = Vector2i;
644
645 fn mul(self, rhs: f32) -> Self::Output {
646 Self::Output {
647 x: self.x * rhs as i32,
648 y: self.y * rhs as i32,
649 }
650 }
651}
652
653impl ops::Mul<i32> for Vector2i {
654 type Output = Vector2i;
655
656 fn mul(self, rhs: i32) -> Self::Output {
657 Self::Output {
658 x: self.x * rhs,
659 y: self.y * rhs,
660 }
661 }
662}
663
664impl ops::Mul<Vector2i> for f32 {
665 type Output = Vector2i;
666
667 fn mul(self, rhs: Vector2i) -> Self::Output {
668 Self::Output {
669 x: self as i32 * rhs.x,
670 y: self as i32 * rhs.y,
671 }
672 }
673}
674
675impl ops::Mul<Vector2i> for i32 {
676 type Output = Vector2i;
677
678 fn mul(self, rhs: Vector2i) -> Self::Output {
679 Self::Output {
680 x: self * rhs.x,
681 y: self * rhs.y,
682 }
683 }
684}
685
686impl ops::Div<Vector2> for Vector2i {
687 type Output = Vector2i;
688
689 fn div(self, rhs: Vector2) -> Self::Output {
690 Self::Output {
691 x: self.x / rhs.x as i32,
692 y: self.y / rhs.y as i32,
693 }
694 }
695}
696
697impl ops::Div<Vector2i> for Vector2i {
698 type Output = Vector2i;
699
700 fn div(self, rhs: Vector2i) -> Self::Output {
701 Self::Output {
702 x: self.x / rhs.x,
703 y: self.y / rhs.y,
704 }
705 }
706}
707
708impl ops::Div<f32> for Vector2i {
709 type Output = Vector2i;
710
711 fn div(self, rhs: f32) -> Self::Output {
712 Self::Output {
713 x: self.x / rhs as i32,
714 y: self.y / rhs as i32,
715 }
716 }
717}
718
719impl ops::Div<i32> for Vector2i {
720 type Output = Vector2i;
721
722 fn div(self, rhs: i32) -> Self::Output {
723 Self::Output {
724 x: self.x / rhs,
725 y: self.y / rhs,
726 }
727 }
728}
729
730impl ops::Div<Vector2i> for f32 {
731 type Output = Vector2i;
732
733 fn div(self, rhs: Vector2i) -> Self::Output {
734 Self::Output {
735 x: self as i32 / rhs.x,
736 y: self as i32 / rhs.y,
737 }
738 }
739}
740
741impl ops::Div<Vector2i> for i32 {
742 type Output = Vector2i;
743
744 fn div(self, rhs: Vector2i) -> Self::Output {
745 Self::Output {
746 x: self / rhs.x,
747 y: self / rhs.y,
748 }
749 }
750}
751
752impl ops::AddAssign<Vector2> for Vector2i {
753 fn add_assign(&mut self, rhs: Vector2) {
754 self.x += rhs.x as i32;
755 self.y += rhs.y as i32;
756 }
757}
758
759impl ops::AddAssign<Vector2i> for Vector2i {
760 fn add_assign(&mut self, rhs: Vector2i) {
761 self.x += rhs.x;
762 self.y += rhs.y;
763 }
764}
765
766impl ops::AddAssign<f32> for Vector2i {
767 fn add_assign(&mut self, rhs: f32) {
768 self.x += rhs as i32;
769 self.y += rhs as i32;
770 }
771}
772
773impl ops::AddAssign<i32> for Vector2i {
774 fn add_assign(&mut self, rhs: i32) {
775 self.x += rhs;
776 self.y += rhs;
777 }
778}
779
780impl ops::SubAssign<Vector2> for Vector2i {
781 fn sub_assign(&mut self, rhs: Vector2) {
782 self.x -= rhs.x as i32;
783 self.y -= rhs.y as i32;
784 }
785}
786
787impl ops::SubAssign<Vector2i> for Vector2i {
788 fn sub_assign(&mut self, rhs: Vector2i) {
789 self.x -= rhs.x;
790 self.y -= rhs.y;
791 }
792}
793
794impl ops::SubAssign<f32> for Vector2i {
795 fn sub_assign(&mut self, rhs: f32) {
796 self.x -= rhs as i32;
797 self.y -= rhs as i32;
798 }
799}
800
801impl ops::SubAssign<i32> for Vector2i {
802 fn sub_assign(&mut self, rhs: i32) {
803 self.x -= rhs;
804 self.y -= rhs;
805 }
806}
807
808impl ops::MulAssign<Vector2> for Vector2i {
809 fn mul_assign(&mut self, rhs: Vector2) {
810 self.x *= rhs.x as i32;
811 self.y *= rhs.y as i32;
812 }
813}
814
815impl ops::MulAssign<Vector2i> for Vector2i {
816 fn mul_assign(&mut self, rhs: Vector2i) {
817 self.x *= rhs.x;
818 self.y *= rhs.y;
819 }
820}
821
822impl ops::MulAssign<f32> for Vector2i {
823 fn mul_assign(&mut self, rhs: f32) {
824 self.x *= rhs as i32;
825 self.y *= rhs as i32;
826 }
827}
828
829impl ops::MulAssign<i32> for Vector2i {
830 fn mul_assign(&mut self, rhs: i32) {
831 self.x *= rhs;
832 self.y *= rhs;
833 }
834}
835
836impl ops::DivAssign<Vector2> for Vector2i {
837 fn div_assign(&mut self, rhs: Vector2) {
838 self.x /= rhs.x as i32;
839 self.y /= rhs.y as i32;
840 }
841}
842
843impl ops::DivAssign<Vector2i> for Vector2i {
844 fn div_assign(&mut self, rhs: Vector2i) {
845 self.x /= rhs.x;
846 self.y /= rhs.y;
847 }
848}
849
850impl ops::DivAssign<f32> for Vector2i {
851 fn div_assign(&mut self, rhs: f32) {
852 self.x /= rhs as i32;
853 self.y /= rhs as i32;
854 }
855}
856
857impl ops::DivAssign<i32> for Vector2i {
858 fn div_assign(&mut self, rhs: i32) {
859 self.x /= rhs;
860 self.y /= rhs;
861 }
862}
863
864#[cfg(test)]
865mod tests {
866 use crate::vector::{Vector2, Vector2i};
867
868 #[test]
869 fn vector2_add_vector2() {
870 let result = Vector2::new(0., 0.) + Vector2::new(1., 1.);
871 assert_eq!(result.x, 1.);
872 assert_eq!(result.y, 1.);
873 }
874
875 #[test]
876 fn vector2_add_vector2i() {
877 let result = Vector2::new(0., 0.) + Vector2i::new(1, 1);
878 assert_eq!(result.x, 1.);
879 assert_eq!(result.y, 1.);
880 }
881
882 #[test]
883 fn vector2_add_f32() {
884 let result = Vector2::new(0., 0.) + 1.;
885 assert_eq!(result.x, 1.);
886 assert_eq!(result.y, 1.);
887 }
888
889 #[test]
890 fn vector2_add_i32() {
891 let result = Vector2::new(0., 0.) + 1;
892 assert_eq!(result.x, 1.);
893 assert_eq!(result.y, 1.);
894 }
895
896 #[test]
897 fn f32_add_vector2() {
898 let result = 1. + Vector2::new(0., 0.);
899 assert_eq!(result.x, 1.);
900 assert_eq!(result.y, 1.);
901 }
902
903 #[test]
904 fn i32_add_vector2() {
905 let result = 1 + Vector2::new(0., 0.);
906 assert_eq!(result.x, 1.);
907 assert_eq!(result.y, 1.);
908 }
909
910 #[test]
911 fn vector2_sub_vector2() {
912 let result = Vector2::new(1., 1.) - Vector2::new(1., 1.);
913 assert_eq!(result.x, 0.);
914 assert_eq!(result.y, 0.);
915 }
916
917 #[test]
918 fn vector2_sub_vector2i() {
919 let result = Vector2::new(1., 1.) - Vector2i::new(1, 1);
920 assert_eq!(result.x, 0.);
921 assert_eq!(result.y, 0.);
922 }
923
924 #[test]
925 fn vector2_sub_f32() {
926 let result = Vector2::new(1., 1.) - 1.;
927 assert_eq!(result.x, 0.);
928 assert_eq!(result.y, 0.);
929 }
930
931 #[test]
932 fn vector2_sub_i32() {
933 let result = Vector2::new(1., 1.) - 1;
934 assert_eq!(result.x, 0.);
935 assert_eq!(result.y, 0.);
936 }
937
938 #[test]
939 fn f32_sub_vector2() {
940 let result = 0. - Vector2::new(1., 1.);
941 assert_eq!(result.x, -1.);
942 assert_eq!(result.y, -1.);
943 }
944
945 #[test]
946 fn i32_sub_vector2() {
947 let result = 0 - Vector2::new(1., 1.);
948 assert_eq!(result.x, -1.);
949 assert_eq!(result.y, -1.);
950 }
951
952 #[test]
953 fn vector2_mul_vector2() {
954 let result = Vector2::new(1., 1.) * Vector2::new(2., 2.);
955 assert_eq!(result.x, 2.);
956 assert_eq!(result.y, 2.);
957 }
958
959 #[test]
960 fn vector2_mul_vector2i() {
961 let result = Vector2::new(1., 1.) * Vector2i::new(2, 2);
962 assert_eq!(result.x, 2.);
963 assert_eq!(result.y, 2.);
964 }
965
966 #[test]
967 fn vector2_mul_f32() {
968 let result = Vector2::new(1., 1.) * 2.;
969 assert_eq!(result.x, 2.);
970 assert_eq!(result.y, 2.);
971 }
972
973 #[test]
974 fn vector2_mul_i32() {
975 let result = Vector2::new(1., 1.) * 2;
976 assert_eq!(result.x, 2.);
977 assert_eq!(result.y, 2.);
978 }
979
980 #[test]
981 fn f32_mul_vector2() {
982 let result = 1. * Vector2::new(2., 2.);
983 assert_eq!(result.x, 2.);
984 assert_eq!(result.y, 2.);
985 }
986
987 #[test]
988 fn i32_mul_vector2() {
989 let result = 1 * Vector2::new(2., 2.);
990 assert_eq!(result.x, 2.);
991 assert_eq!(result.y, 2.);
992 }
993
994 #[test]
995 fn vector2_div_vector2() {
996 let result = Vector2::new(2., 2.) / Vector2::new(2., 2.);
997 assert_eq!(result.x, 1.);
998 assert_eq!(result.y, 1.);
999 }
1000
1001 #[test]
1002 fn vector2_div_vector2i() {
1003 let result = Vector2::new(2., 2.) / Vector2i::new(2, 2);
1004 assert_eq!(result.x, 1.);
1005 assert_eq!(result.y, 1.);
1006 }
1007
1008 #[test]
1009 fn vector2_div_f32() {
1010 let result = Vector2::new(2., 2.) / 2.;
1011 assert_eq!(result.x, 1.);
1012 assert_eq!(result.y, 1.);
1013 }
1014
1015 #[test]
1016 fn vector2_div_i32() {
1017 let result = Vector2::new(2., 2.) / 2;
1018 assert_eq!(result.x, 1.);
1019 assert_eq!(result.y, 1.);
1020 }
1021
1022 #[test]
1023 fn f32_div_vector2() {
1024 let result = 1. / Vector2::new(2., 2.);
1025 assert_eq!(result.x, 0.5);
1026 assert_eq!(result.y, 0.5);
1027 }
1028
1029 #[test]
1030 fn i32_div_vector2() {
1031 let result = 1 / Vector2::new(2., 2.);
1032 assert_eq!(result.x, 0.5);
1033 assert_eq!(result.y, 0.5);
1034 }
1035
1036 #[test]
1037 fn vector2_add_assign_vector2() {
1038 let mut result = Vector2::new(0., 0.);
1039 result += Vector2::new(1., 1.);
1040 assert_eq!(result.x, 1.);
1041 assert_eq!(result.y, 1.);
1042 }
1043
1044 #[test]
1045 fn vector2_add_assign_vector2i() {
1046 let mut result = Vector2::new(0., 0.);
1047 result += Vector2i::new(1, 1);
1048 assert_eq!(result.x, 1.);
1049 assert_eq!(result.y, 1.);
1050 }
1051
1052 #[test]
1053 fn vector2_add_assign_f32() {
1054 let mut result = Vector2::new(0., 0.);
1055 result += 1.;
1056 assert_eq!(result.x, 1.);
1057 assert_eq!(result.y, 1.);
1058 }
1059
1060 #[test]
1061 fn vector2_add_assign_i32() {
1062 let mut result = Vector2::new(0., 0.);
1063 result += 1;
1064 assert_eq!(result.x, 1.);
1065 assert_eq!(result.y, 1.);
1066 }
1067
1068 #[test]
1069 fn vector2_sub_assign_vector2() {
1070 let mut result = Vector2::new(2., 2.);
1071 result -= Vector2::new(1., 1.);
1072 assert_eq!(result.x, 1.);
1073 assert_eq!(result.y, 1.);
1074 }
1075
1076 #[test]
1077 fn vector2_sub_assign_vector2i() {
1078 let mut result = Vector2::new(2., 2.);
1079 result -= Vector2i::new(1, 1);
1080 assert_eq!(result.x, 1.);
1081 assert_eq!(result.y, 1.);
1082 }
1083
1084 #[test]
1085 fn vector2_sub_assign_f32() {
1086 let mut result = Vector2::new(2., 2.);
1087 result -= 1.;
1088 assert_eq!(result.x, 1.);
1089 assert_eq!(result.y, 1.);
1090 }
1091
1092 #[test]
1093 fn vector2_sub_assign_i32() {
1094 let mut result = Vector2::new(2., 2.);
1095 result -= 1;
1096 assert_eq!(result.x, 1.);
1097 assert_eq!(result.y, 1.);
1098 }
1099
1100 #[test]
1101 fn vector2_mul_assign_vector2() {
1102 let mut result = Vector2::new(2., 2.);
1103 result *= Vector2::new(1., 1.);
1104 assert_eq!(result.x, 2.);
1105 assert_eq!(result.y, 2.);
1106 }
1107
1108 #[test]
1109 fn vector2_mul_assign_vector2i() {
1110 let mut result = Vector2::new(2., 2.);
1111 result *= Vector2i::new(1, 1);
1112 assert_eq!(result.x, 2.);
1113 assert_eq!(result.y, 2.);
1114 }
1115
1116 #[test]
1117 fn vector2_mul_assign_f32() {
1118 let mut result = Vector2::new(2., 2.);
1119 result *= 1.;
1120 assert_eq!(result.x, 2.);
1121 assert_eq!(result.y, 2.);
1122 }
1123
1124 #[test]
1125 fn vector2_mul_assign_i32() {
1126 let mut result = Vector2::new(2., 2.);
1127 result *= 1;
1128 assert_eq!(result.x, 2.);
1129 assert_eq!(result.y, 2.);
1130 }
1131
1132 #[test]
1133 fn vector2_div_assign_vector2() {
1134 let mut result = Vector2::new(2., 2.);
1135 result /= Vector2::new(1., 1.);
1136 assert_eq!(result.x, 2.);
1137 assert_eq!(result.y, 2.);
1138 }
1139
1140 #[test]
1141 fn vector2_div_assign_vector2i() {
1142 let mut result = Vector2::new(2., 2.);
1143 result /= Vector2i::new(1, 1);
1144 assert_eq!(result.x, 2.);
1145 assert_eq!(result.y, 2.);
1146 }
1147
1148 #[test]
1149 fn vector2_div_assign_f32() {
1150 let mut result = Vector2::new(2., 2.);
1151 result /= 1.;
1152 assert_eq!(result.x, 2.);
1153 assert_eq!(result.y, 2.);
1154 }
1155
1156 #[test]
1157 fn vector2_div_assign_i32() {
1158 let mut result = Vector2::new(2., 2.);
1159 result /= 1;
1160 assert_eq!(result.x, 2.);
1161 assert_eq!(result.y, 2.);
1162 }
1163
1164 #[test]
1165 fn vector2i_add_vector2() {
1166 let result = Vector2i::new(0, 0) + Vector2::new(1., 1.);
1167 assert_eq!(result.x, 1);
1168 assert_eq!(result.y, 1);
1169 }
1170
1171 #[test]
1172 fn vector2i_add_vector2i() {
1173 let result = Vector2i::new(0, 0) + Vector2i::new(1, 1);
1174 assert_eq!(result.x, 1);
1175 assert_eq!(result.y, 1);
1176 }
1177
1178 #[test]
1179 fn vector2i_add_f32() {
1180 let result = Vector2i::new(0, 0) + 1.;
1181 assert_eq!(result.x, 1);
1182 assert_eq!(result.y, 1);
1183 }
1184
1185 #[test]
1186 fn vector2i_add_i32() {
1187 let result = Vector2i::new(0, 0) + 1;
1188 assert_eq!(result.x, 1);
1189 assert_eq!(result.y, 1);
1190 }
1191
1192 #[test]
1193 fn f32_add_vector2i() {
1194 let result = 1. + Vector2i::new(1, 1);
1195 assert_eq!(result.x, 2);
1196 assert_eq!(result.y, 2);
1197 }
1198
1199 #[test]
1200 fn i32_add_vector2i() {
1201 let result = 1 + Vector2i::new(1, 1);
1202 assert_eq!(result.x, 2);
1203 assert_eq!(result.y, 2);
1204 }
1205
1206 #[test]
1207 fn vector2i_sub_vector2() {
1208 let result = Vector2i::new(1, 1) - Vector2::new(1., 1.);
1209 assert_eq!(result.x, 0);
1210 assert_eq!(result.y, 0);
1211 }
1212
1213 #[test]
1214 fn vector2i_sub_vector2i() {
1215 let result = Vector2i::new(1, 1) - Vector2i::new(1, 1);
1216 assert_eq!(result.x, 0);
1217 assert_eq!(result.y, 0);
1218 }
1219
1220 #[test]
1221 fn vector2i_sub_f32() {
1222 let result = Vector2i::new(1, 1) - 1.;
1223 assert_eq!(result.x, 0);
1224 assert_eq!(result.y, 0);
1225 }
1226
1227 #[test]
1228 fn vector2i_sub_i32() {
1229 let result = Vector2i::new(1, 1) - 1;
1230 assert_eq!(result.x, 0);
1231 assert_eq!(result.y, 0);
1232 }
1233
1234 #[test]
1235 fn f32_sub_vector2i() {
1236 let result = 0. - Vector2i::new(1, 1);
1237 assert_eq!(result.x, -1);
1238 assert_eq!(result.y, -1);
1239 }
1240
1241 #[test]
1242 fn i32_sub_vector2i() {
1243 let result = 0 - Vector2i::new(1, 1);
1244 assert_eq!(result.x, -1);
1245 assert_eq!(result.y, -1);
1246 }
1247
1248 #[test]
1249 fn vector2i_mul_vector2() {
1250 let result = Vector2i::new(1, 1) * Vector2::new(2., 2.);
1251 assert_eq!(result.x, 2);
1252 assert_eq!(result.y, 2);
1253 }
1254
1255 #[test]
1256 fn vector2i_mul_vector2i() {
1257 let result = Vector2i::new(1, 1) * Vector2i::new(2, 2);
1258 assert_eq!(result.x, 2);
1259 assert_eq!(result.y, 2);
1260 }
1261
1262 #[test]
1263 fn vector2i_mul_f32() {
1264 let result = Vector2i::new(1, 1) * 2.;
1265 assert_eq!(result.x, 2);
1266 assert_eq!(result.y, 2);
1267 }
1268
1269 #[test]
1270 fn vector2i_mul_i32() {
1271 let result = Vector2i::new(1, 1) * 2;
1272 assert_eq!(result.x, 2);
1273 assert_eq!(result.y, 2);
1274 }
1275
1276 #[test]
1277 fn f32_mul_vector2i() {
1278 let result = -1. * Vector2i::new(1, 1);
1279 assert_eq!(result.x, -1);
1280 assert_eq!(result.y, -1);
1281 }
1282
1283 #[test]
1284 fn i32_mul_vector2i() {
1285 let result = -1 * Vector2i::new(1, 1);
1286 assert_eq!(result.x, -1);
1287 assert_eq!(result.y, -1);
1288 }
1289
1290 #[test]
1291 fn vector2i_div_vector2() {
1292 let result = Vector2i::new(2, 2) / Vector2::new(2., 2.);
1293 assert_eq!(result.x, 1);
1294 assert_eq!(result.y, 1);
1295 }
1296
1297 #[test]
1298 fn vector2i_div_vector2i() {
1299 let result = Vector2i::new(2, 2) / Vector2i::new(2, 2);
1300 assert_eq!(result.x, 1);
1301 assert_eq!(result.y, 1);
1302 }
1303
1304 #[test]
1305 fn vector2i_div_f32() {
1306 let result = Vector2i::new(2, 2) / 2.;
1307 assert_eq!(result.x, 1);
1308 assert_eq!(result.y, 1);
1309 }
1310
1311 #[test]
1312 fn vector2i_div_i32() {
1313 let result = Vector2i::new(2, 2) / 2;
1314 assert_eq!(result.x, 1);
1315 assert_eq!(result.y, 1);
1316 }
1317
1318 #[test]
1319 fn f32_div_vector2i() {
1320 let result = -4. / Vector2i::new(2, 2);
1321 assert_eq!(result.x, -2);
1322 assert_eq!(result.y, -2);
1323 }
1324
1325 #[test]
1326 fn i32_div_vector2i() {
1327 let result = -4 / Vector2i::new(2, 2);
1328 assert_eq!(result.x, -2);
1329 assert_eq!(result.y, -2);
1330 }
1331
1332 #[test]
1333 fn vector2i_add_assign_vector2() {
1334 let mut result = Vector2i::new(0, 0);
1335 result += Vector2::new(1., 1.);
1336 assert_eq!(result.x, 1);
1337 assert_eq!(result.y, 1);
1338 }
1339
1340 #[test]
1341 fn vector2i_add_assign_vector2i() {
1342 let mut result = Vector2i::new(0, 0);
1343 result += Vector2i::new(1, 1);
1344 assert_eq!(result.x, 1);
1345 assert_eq!(result.y, 1);
1346 }
1347
1348 #[test]
1349 fn vector2i_add_assign_f32() {
1350 let mut result = Vector2i::new(0, 0);
1351 result += 1.;
1352 assert_eq!(result.x, 1);
1353 assert_eq!(result.y, 1);
1354 }
1355
1356 #[test]
1357 fn vector2i_add_assign_i32() {
1358 let mut result = Vector2i::new(0, 0);
1359 result += 1;
1360 assert_eq!(result.x, 1);
1361 assert_eq!(result.y, 1);
1362 }
1363
1364 #[test]
1365 fn vector2i_sub_assign_vector2() {
1366 let mut result = Vector2i::new(2, 2);
1367 result -= Vector2::new(1., 1.);
1368 assert_eq!(result.x, 1);
1369 assert_eq!(result.y, 1);
1370 }
1371
1372 #[test]
1373 fn vector2i_sub_assign_vector2i() {
1374 let mut result = Vector2i::new(2, 2);
1375 result -= Vector2i::new(1, 1);
1376 assert_eq!(result.x, 1);
1377 assert_eq!(result.y, 1);
1378 }
1379
1380 #[test]
1381 fn vector2i_sub_assign_f32() {
1382 let mut result = Vector2i::new(2, 2);
1383 result -= 1.;
1384 assert_eq!(result.x, 1);
1385 assert_eq!(result.y, 1);
1386 }
1387
1388 #[test]
1389 fn vector2i_sub_assign_i32() {
1390 let mut result = Vector2i::new(2, 2);
1391 result -= 1;
1392 assert_eq!(result.x, 1);
1393 assert_eq!(result.y, 1);
1394 }
1395
1396 #[test]
1397 fn vector2i_mul_assign_vector2() {
1398 let mut result = Vector2i::new(2, 2);
1399 result *= Vector2::new(1., 1.);
1400 assert_eq!(result.x, 2);
1401 assert_eq!(result.y, 2);
1402 }
1403
1404 #[test]
1405 fn vector2i_mul_assign_vector2i() {
1406 let mut result = Vector2i::new(2, 2);
1407 result *= Vector2i::new(1, 1);
1408 assert_eq!(result.x, 2);
1409 assert_eq!(result.y, 2);
1410 }
1411
1412 #[test]
1413 fn vector2i_mul_assign_f32() {
1414 let mut result = Vector2i::new(2, 2);
1415 result *= 1.;
1416 assert_eq!(result.x, 2);
1417 assert_eq!(result.y, 2);
1418 }
1419
1420 #[test]
1421 fn vector2i_mul_assign_i32() {
1422 let mut result = Vector2i::new(2, 2);
1423 result *= 1;
1424 assert_eq!(result.x, 2);
1425 assert_eq!(result.y, 2);
1426 }
1427
1428 #[test]
1429 fn vector2i_div_assign_vector2() {
1430 let mut result = Vector2i::new(2, 2);
1431 result /= Vector2::new(1., 1.);
1432 assert_eq!(result.x, 2);
1433 assert_eq!(result.y, 2);
1434 }
1435
1436 #[test]
1437 fn vector2i_div_assign_vector2i() {
1438 let mut result = Vector2i::new(2, 2);
1439 result /= Vector2i::new(1, 1);
1440 assert_eq!(result.x, 2);
1441 assert_eq!(result.y, 2);
1442 }
1443
1444 #[test]
1445 fn vector2i_div_assign_f32() {
1446 let mut result = Vector2i::new(2, 2);
1447 result /= 1.;
1448 assert_eq!(result.x, 2);
1449 assert_eq!(result.y, 2);
1450 }
1451
1452 #[test]
1453 fn vector2i_div_assign_i32() {
1454 let mut result = Vector2i::new(2, 2);
1455 result /= 1;
1456 assert_eq!(result.x, 2);
1457 assert_eq!(result.y, 2);
1458 }
1459
1460 #[test]
1461 fn vector2_normalize() {
1462 let mut result = Vector2::new(1., 0.);
1463 result.normalize();
1464 assert_eq!(result.x, 1.);
1465 assert_eq!(result.y, 0.);
1466 }
1467
1468 #[test]
1469 fn vector2_normalize_2() {
1470 let mut result = Vector2::new(0.2, 0.);
1471 result.normalize();
1472 assert_eq!(result.x, 1.);
1473 assert_eq!(result.y, 0.);
1474 }
1475
1476 #[test]
1477 fn vector2_normalize_3() {
1478 let mut result = Vector2::new(0.2, 0.2);
1479 result.normalize();
1480 assert_eq!(result.x, 0.70710677);
1481 assert_eq!(result.y, 0.70710677);
1482 }
1483
1484 #[test]
1485 fn vector2_normalized() {
1486 let result = Vector2::new(1., 0.).normalized();
1487 assert_eq!(result.x, 1.);
1488 assert_eq!(result.y, 0.);
1489 }
1490
1491 #[test]
1492 fn vector2_normalized_2() {
1493 let result = Vector2::new(0.2, 0.).normalized();
1494 assert_eq!(result.x, 1.);
1495 assert_eq!(result.y, 0.);
1496 }
1497
1498 #[test]
1499 fn vector2_normalized_3() {
1500 let result = Vector2::new(0.2, 0.2).normalized();
1501 assert_eq!(result.x, 0.70710677);
1502 assert_eq!(result.y, 0.70710677);
1503 }
1504}