miscmath/linear/
vector.rs

1use super::CoordSystem::*;
2use super::*;
3
4use rand::Rng;
5use std::ops::{Add, Div, Mul, Range, Sub, AddAssign, SubAssign, MulAssign, DivAssign, Rem, RemAssign};
6
7/* TODO: make vectors generic and duplicate each impl block, one for f32, isize */
8
9/// A two dimensional mathematical vector
10///
11/// # Examples
12///
13/// ```
14/// use miscmath::prelude::*;
15///
16/// let a = Vec2::default();
17///
18/// assert!( ( a.x < 0.00001 ) && ( a.y < 0.00001 ) );
19/// ```
20///
21#[derive(Copy, Clone, Debug)]
22pub struct Vec2 {
23    ///
24    pub x: f32,
25    ///
26    pub y: f32,
27    ///
28    coord_system: CoordSystem,
29}
30
31/// Implements Default for Vec2
32/// Generates a new instance of Vec2 initialized to zero and returns it
33///
34/// # Examples
35///
36/// ```
37/// use miscmath::prelude::*;
38///
39/// let a = Vec2::default();
40///
41/// assert!( ( a.x < 0.00001 ) && ( a.y < 0.00001 ) );
42/// ```
43///
44impl Default for Vec2 {
45    fn default() -> Self {
46        Self {
47            x: 0.0,
48            y: 0.0,
49            coord_system: CARTESIAN,
50        }
51    }
52}
53
54/// Implements Add for Vec2
55///
56/// # Examples
57///
58/// ```
59/// use miscmath::prelude::*;
60/// 
61/// let mut x = Vec2::default();
62/// let y = Vec2::new( &5.0, &7.0);
63/// x += y;
64///
65/// assert_eq!(x, y);
66/// ```
67///
68impl Add for Vec2 {
69    type Output = Self;
70
71    fn add(self, rhs: Self) -> Self::Output {
72        Self {
73            x: self.x + rhs.x,
74            y: self.y + rhs.y,
75            coord_system: self.coord_system,
76        }
77    }
78}
79
80/// Implements AddAssign for Vec2
81///
82/// # Examples
83///
84/// ```
85///
86///
87///
88/// ```
89///
90impl AddAssign for Vec2 {
91
92    fn add_assign(&mut self, rhs: Self) {
93        *self = *self + rhs;
94    }
95}
96
97/// Implements Sub for Vec2
98///
99/// # Examples
100///
101/// ```
102///
103///
104///
105/// ```
106///
107impl Sub for Vec2 {
108    type Output = Self;
109
110    fn sub(self, rhs: Self) -> Self::Output {
111        Self {
112            x: self.x - rhs.x,
113            y: self.y - rhs.y,
114            coord_system: self.coord_system,
115        }
116    }
117}
118
119/// Implements SubAssign for Vec2
120///
121/// # Examples
122///
123/// ```
124///
125///
126///
127/// ```
128///
129impl SubAssign for Vec2 {
130    fn sub_assign(&mut self, rhs: Self) {
131        *self = *self - rhs;
132    }
133}
134
135/// Implements Mul for Vec2
136///
137/// # Examples
138///
139/// ```
140///
141///
142///
143/// ```
144///
145impl Mul for Vec2 {
146    type Output = Self;
147
148    fn mul(self, rhs: Self) -> Self::Output {
149        Self {
150            x: self.x * rhs.x,
151            y: self.y * rhs.y,
152            coord_system: self.coord_system,
153        }
154    }
155}
156
157/// Implements MulAssign for Vec2
158///
159/// # Examples
160///
161/// ```
162///
163///
164///
165/// ```
166///
167impl MulAssign for Vec2 {
168    fn mul_assign(&mut self, rhs: Self) {
169        *self = *self * rhs;
170    }
171}
172
173/// Implements Div for Vec2
174///
175/// # Examples
176///
177/// ```
178///
179///
180///
181/// ```
182///
183impl Div for Vec2 {
184    type Output = Self;
185
186    fn div(self, rhs: Self) -> Self::Output {
187        Self {
188            x: self.x / rhs.x,
189            y: self.y / rhs.y,
190            coord_system: self.coord_system,
191        }
192    }
193}
194
195/// Implements DivAssign for Vec2
196///
197/// # Examples
198///
199/// ```
200///
201///
202///
203/// ```
204///
205impl DivAssign for Vec2 {
206    fn div_assign(&mut self, rhs: Self) {
207        *self = *self / rhs;
208    }
209}
210
211/// Implements Rem for Vec2
212///
213/// # Examples
214///
215/// ```
216///
217///
218///
219/// ```
220///
221impl Rem for Vec2 {
222    type Output = Self;
223
224    fn rem(self, rhs: Self) -> Self::Output {
225        Vec2{
226            x: self.x % rhs.x,
227            y: self.y % rhs.y,
228            coord_system: CARTESIAN,
229        }
230    }
231}
232
233/// Implements RemAssign for Vec2
234///
235/// # Examples
236///
237/// ```
238///
239///
240///
241/// ```
242///
243impl RemAssign for Vec2 {
244    fn rem_assign(&mut self, rhs: Self) {
245        *self = *self % rhs;
246    }
247}
248
249/// Implements PartialEq for Vec2
250///
251/// # Examples
252///
253/// ```
254///  
255///
256///
257/// ```
258///
259impl PartialEq for Vec2 {
260    ///
261    fn eq(&self, rhs: &Self) -> bool {
262        if (*self - *rhs).x.abs() < 0.00001
263            && (*self - *rhs).y.abs() < 0.00001
264            && self.coord_system == rhs.coord_system
265        {
266            true
267        } else {
268            false
269        }
270    }
271
272    ///
273    fn ne(&self, rhs: &Self) -> bool {
274        if (*self - *rhs).x.abs() > 0.00001 && (*self - *rhs).y.abs() > 0.00001 {
275            true
276        } else {
277            false
278        }
279    }
280}
281
282impl Vec2 {
283    /*/// Generates a new instance of Vec2 initialized to zero and returns it
284    ///
285    /// # Examples
286    ///
287    /// ```
288    /// use miscmath::prelude::*;
289    ///
290    /// let a = Vec2::new();
291    ///
292    /// assert!( ( a.x < 0.00001 ) && ( a.y < 0.00001 ) );
293    /// ```
294    ///
295    pub fn new() -> Vec2 {
296        Vec2 {
297            x: 0.0,
298            y: 0.0,
299            coord_system: CARTESIAN,
300        }
301    }*/
302
303    /// Generates a new instance of Vec2 initialized to chosen values and returns it
304    ///
305    /// # Examples
306    ///
307    /// ```
308    /// use miscmath::prelude::*;
309    ///
310    /// let a = Vec2::new( &5.6, &7.2 );
311    ///
312    /// assert!( ( ( a.x - 5.6 ) < 0.00001 ) && ( ( a.y - 7.2 ) < 0.00001 ) );
313    /// ```
314    ///
315    pub fn new(x: &f32, y: &f32) -> Vec2 {
316        let temp = Vec2 {
317            x: *x,
318            y: *y,
319            coord_system: CARTESIAN,
320        };
321        temp
322    }
323
324    /// Generates a new instance of Vec2 initialized to random values in the range entered and returns it
325    ///
326    /// # Examples
327    ///
328    /// ```
329    ///
330    ///
331    ///
332    /// ```
333    ///
334    pub fn create_random(range_x: &Range<f32>) -> Vec2 {
335        let temp = Vec2 {
336            x: rand::thread_rng().gen_range(range_x.clone()),
337            y: rand::thread_rng().gen_range(range_x.clone()),
338            coord_system: CARTESIAN,
339        };
340        temp
341    }
342
343    /// Generates a new instance of Vec2 initialized to random values in the range entered and returns it
344    ///
345    /// # Examples
346    ///
347    /// ```
348    ///
349    ///
350    ///
351    /// ```
352    ///
353    pub fn create_random2(range_x: &Range<f32>, range_y: &Range<f32>) -> Vec2 {
354        let temp = Vec2 {
355            x: rand::thread_rng().gen_range(range_x.clone()),
356            y: rand::thread_rng().gen_range(range_y.clone()),
357            coord_system: CARTESIAN,
358        };
359        temp
360    }
361
362    /// Generates a new instance of Vec2 based on a entered angle with a magnitude of 1
363    ///
364    /// # Examples
365    ///
366    /// ```
367    ///  
368    ///
369    ///   
370    /// ```
371    ///
372    pub fn from_angle(theta: &f32, in_mag: &Option<f32>) -> Vec2 {
373        if let Some(mag) = in_mag {
374            let mut temp = Vec2 {
375                x: *mag,
376                y: *theta,
377                coord_system: POLAR,
378            };
379            temp.swap_system(CARTESIAN);
380            temp
381        } else {
382            let mut temp = Vec2 {
383                x: 1.0,
384                y: *theta,
385                coord_system: POLAR,
386            };
387            temp.swap_system(CARTESIAN);
388            temp
389        }
390    }
391
392    /// Generates a new instance of Vec2 initialized to random values in the range entered and returns it
393    ///
394    /// # Examples
395    ///
396    /// ```
397    ///
398    ///
399    ///
400    /// ```
401    ///
402    pub fn from_rand_angle(range_x: &Range<f32>, mag: &Option<f32>) -> Vec2 {
403        let temp = Vec2::from_angle(&rand::thread_rng().gen_range(range_x.clone()), mag);
404        temp
405    }
406
407    /// Generates a new instance of Vec2 initialized to a magnitude of 1 and a angle of random value in the range entered and returns it
408    ///
409    /// # Examples
410    ///
411    /// ```
412    /// use std::f32::consts::TAU;
413    /// use miscmath::prelude::*;
414    ///
415    /// let mut a = Vec2::random_unit( &( 0.0..TAU ) );
416    ///
417    /// assert!( ( ( a.x - 1.0 ) < 0.00001 ) && ( a.y < TAU ) );
418    /// ```
419    ///
420    pub fn random_unit(range: &Range<f32>) -> Vec2 {
421        let temp = Vec2::from_angle(&rand::thread_rng().gen_range(range.clone()), &None);
422        temp
423    }
424
425    /// Generates a new instance of Vec2 initialized as a unit vector of angle 0 and returns it
426    ///
427    /// # Examples
428    ///
429    /// ```
430    /// use miscmath::prelude::*;
431    ///
432    /// let mut b = Vec2::unit();
433    ///
434    /// assert!( ( b.x - 1.0 < 0.00001 ) && ( b.y < 0.00001 ) );
435    /// ```
436    ///
437    pub fn unit() -> Vec2 {
438        let temp = Vec2::from_angle(&0.0, &Some(1.0));
439        temp
440    }
441
442    /// Adds the components from the rhs Vec2 to the corresponding components of self
443    ///
444    /// # Examples
445    ///
446    /// ```
447    ///  
448    ///
449    ///   
450    /// ```
451    ///
452    pub fn add(&mut self, rhs: &Vec2) {
453        self.swap_system(CARTESIAN);
454        self.x += rhs.x;
455        self.y += rhs.y;
456    }
457
458    /// Calculates the angle between self and a passed in Vec2
459    ///
460    /// # Examples
461    ///
462    /// ```
463    /// use miscmath::prelude::*;
464    ///
465    /// let mut a = Vec2::new( &5.6, &7.2 );
466    /// let mut b = Vec2::unit();
467    ///
468    /// let angle = a.angle_between( &mut b );
469    ///
470    /// assert!( ( angle - 1.4731481877 ) < 0.00001 );
471    /// ```
472    ///
473    pub fn angle_between(&self, rhs: &Vec2) -> f32 {
474        let dot = self.dot(&rhs);
475        (dot / (self.mag() * rhs.mag())).acos()
476    }
477
478    ///
479    ///
480    /// # Examples
481    ///
482    /// ```
483    ///
484    ///
485    ///
486    /// ```
487    ///
488    pub fn constrain(&mut self, x_rng: &Range<f32>, y_rng: &Range<f32>) {
489        self.x = self.x.clamp(x_rng.start, x_rng.end);
490        self.y = self.y.clamp(y_rng.start, y_rng.end);
491    }
492
493    /// Generates a new instance of Vec2 which is perpendicular to the self instance
494    ///
495    /// # Examples
496    ///
497    /// ```
498    ///  
499    ///
500    ///   
501    /// ```
502    ///
503    pub fn cross(&mut self) {
504        self.swap_system(CARTESIAN);
505        let x = self.x;
506        self.x = self.y;
507        self.y = -x;
508    }
509
510    /// Calculates the distance between self and the Vec2 passed in
511    ///
512    /// # Examples
513    ///
514    /// ```
515    ///
516    ///
517    ///
518    /// ```
519    ///
520    pub fn dist(&self, rhs: &Vec2) -> f32 {
521        ((self.x - rhs.x).powf(2.0)) + ((self.y - rhs.y).powf(2.0)).sqrt()
522    }
523
524    /// Calculates the distance squared between self and the Vec2 passed in
525    ///
526    /// # Examples
527    ///
528    /// ```
529    ///  
530    ///
531    ///   
532    /// ```
533    ///
534    pub fn dist_sq(&self, rhs: &Vec2) -> f32 {
535        ((self.x - rhs.x).powf(2.0)) + ((self.y - rhs.y).powf(2.0))
536    }
537
538    /// Prints debug information of self to terminal
539    ///
540    /// # Examples
541    ///
542    /// ```
543    ///  
544    ///
545    ///   
546    /// ```
547    ///
548    pub fn dbg(&self) {
549        dbg!(self);
550    }
551
552    /// Calculates the dot product between self and a passed in Vec2
553    ///
554    /// # Examples
555    ///
556    /// ```
557    ///  
558    ///
559    ///   
560    /// ```
561    ///
562    pub fn dot(&self, rhs: &Vec2) -> f32 {
563        (self.x * rhs.x) + (self.y * rhs.y)
564    }
565
566    /// Divides the components of self by a scalar value rhs
567    ///
568    /// # Examples
569    ///
570    /// ```
571    ///  
572    ///
573    ///   
574    /// ```
575    ///
576    pub fn div(&mut self, rhs: &f32) {
577        self.x /= rhs;
578        self.y /= rhs;
579    }
580    
581    /// Divides the components of self by a Vec2 rhs
582    ///
583    /// # Examples
584    ///
585    /// ```
586    ///
587    ///
588    ///
589    /// ```
590    ///
591    pub fn div2(&mut self, rhs: &Vec2) {
592        self.x /= rhs.x;
593        self.y /= rhs.y;
594    }
595
596    /// Linearly interpolates between self and a passed in Vec2
597    ///
598    /// # Examples
599    ///
600    /// ```
601    /// use miscmath::prelude::*;
602    ///
603    ///	let mut a = Vec2::new( &5.6, &7.2 );
604    /// let mut b = Vec2::unit();
605    ///
606    /// a.lerp( &b, UnitF::new( 0.5 ) );
607    ///
608    /// assert!( ( ( a.x - 3.3 ) < 0.00001 ) && ( ( a.y - 3.6 ) < 0.00001 ) );
609    /// ```
610    ///
611    pub fn lerp(&mut self, rhs: &Vec2, amt: UnitF) {
612        self.swap_system(CARTESIAN);
613        self.x = -(amt.value() - 1.0) * self.x + (amt.value() * rhs.x);
614        self.y = -(amt.value() - 1.0) * self.y + (amt.value() * rhs.y);
615    }
616
617    /// Calculates the magnitude of self
618    ///
619    /// # Examples
620    ///
621    /// ```
622    ///  
623    ///
624    ///   
625    /// ```
626    ///
627    pub fn mag(&self) -> f32 {
628        ((self.x).powf(2.0) + (self.y).powf(2.0)).sqrt()
629    }
630
631    /// Calculates the magnitude squared of self
632    ///
633    /// # Examples
634    ///
635    /// ```
636    ///  
637    ///
638    ///   
639    /// ```
640    ///
641    pub fn mag_sq(&self) -> f32 {
642        (self.x).powf(2.0) + (self.y).powf(2.0)
643    }
644
645    /// Multiplies the components of self by a scalar value rhs
646    ///
647    /// # Examples
648    ///
649    /// ```
650    ///  
651    ///
652    ///   
653    /// ```
654    ///
655    pub fn mult(&mut self, rhs: &f32) {
656        self.swap_system(CARTESIAN);
657        self.x *= rhs;
658        self.y *= rhs;
659    }
660    
661    /// Multiplies the components of self by a Vec2 rhs
662    ///
663    /// # Examples
664    ///
665    /// ```
666    ///
667    ///
668    ///
669    /// ```
670    ///
671    pub fn mult2(&mut self, rhs: &Vec2) {
672        self.swap_system(CARTESIAN);
673        self.x *= rhs.x;
674        self.y *= rhs.y;
675    }
676
677    /// Normalizes the magnitude of self to 1, angle is unchanged
678    ///
679    /// # Examples
680    ///
681    /// ```
682    ///  
683    ///
684    ///   
685    /// ```
686    ///
687    pub fn norm(&mut self) {
688        self.swap_system(POLAR);
689        self.x = 1.0;
690        self.swap_system(CARTESIAN);
691    }
692
693    /// Sets the components of self to the remainder of scalar division by rhs
694    ///
695    /// # Examples
696    ///
697    /// ```
698    ///  
699    ///
700    ///   
701    /// ```
702    ///
703    pub fn rem(&mut self, rhs: &f32) {
704        self.swap_system(CARTESIAN);
705        self.x %= rhs;
706        self.y %= rhs;
707    }
708
709    /// Rotates self by the angle entered
710    ///
711    /// # Examples
712    ///
713    /// ```
714    ///  
715    ///
716    ///   
717    /// ```
718    ///
719    pub fn rotate(&mut self, theta: f32) {
720        self.swap_system(POLAR);
721        self.y += theta;
722        self.swap_system(CARTESIAN);
723    }
724
725    /// Sets components of vector to values entered
726    ///
727    /// # Examples
728    ///
729    /// ```
730    ///  
731    ///
732    ///   
733    /// ```
734    ///
735    pub fn set(&mut self, input1: &f32, input2: &f32, coord_system: &CoordSystem) {
736        if *coord_system == CARTESIAN {
737            self.swap_system(CARTESIAN);
738            self.x = *input1;
739            self.y = *input2;
740        } else {
741            self.swap_system(POLAR);
742            self.x = *input1;
743            self.y = *input2;
744            self.swap_system(CARTESIAN);
745        }
746    }
747
748    /// Sets the magnitude of self
749    ///
750    /// # Examples
751    ///
752    /// ```
753    ///  
754    ///
755    ///   
756    /// ```
757    ///
758    pub fn set_mag(&mut self, input: &f32) {
759        self.swap_system(POLAR);
760        self.x = *input;
761        self.swap_system(CARTESIAN);
762    }
763
764    /// Sets the theta of self
765    ///
766    /// # Examples
767    ///
768    /// ```
769    ///  
770    ///
771    ///   
772    /// ```
773    ///
774    pub fn set_theta(&mut self, input: &f32) {
775        self.swap_system(POLAR);
776        self.y = *input;
777        self.swap_system(CARTESIAN);
778    }
779
780    /// Subtracts the components of the rhs Vec2 from the corresponding components of self
781    ///
782    /// # Examples
783    ///
784    /// ```
785    ///  
786    ///
787    ///   
788    /// ```
789    ///
790    pub fn sub(&mut self, rhs: &Vec2) {
791        self.swap_system(CARTESIAN);
792        self.x -= rhs.x;
793        self.y -= rhs.y;
794    }
795
796    /// Returns the current coordinate system
797    ///
798    /// # Examples
799    ///
800    /// ```
801    ///  
802    ///
803    ///   
804    /// ```
805    ///
806    pub fn system(&self) -> CoordSystem {
807        self.coord_system
808    }
809
810    /// Converts components from cartesian to polar coordinates, and vice versa
811    ///
812    /// # Examples
813    ///
814    /// ```
815    ///  
816    ///
817    ///   
818    /// ```
819    ///
820    fn swap_system(&mut self, new_coord_system: CoordSystem) {
821        if self.coord_system == CARTESIAN && new_coord_system == POLAR {
822            let x = self.x;
823            self.x = (self.x.powi(2) + self.y.powi(2)).sqrt();
824            self.y = self.y.atan2(x);
825            self.coord_system = new_coord_system;
826        } else if self.coord_system == POLAR && new_coord_system == CARTESIAN {
827            let theta = self.y;
828            self.y = self.x * self.y.sin();
829            self.x *= theta.cos();
830            self.coord_system = new_coord_system;
831        }
832    }
833
834    /// Calculates the theta of self
835    ///
836    /// # Examples
837    ///
838    /// ```
839    ///  
840    ///
841    ///   
842    /// ```
843    ///
844    pub fn theta(&self) -> f32 {
845        (self.y).atan2(self.x)
846    }
847}
848
849/// A three dimensional mathematical vector
850///
851/// # Examples
852///
853/// ```
854/// use miscmath::prelude::*;
855///
856/// let a = Vec3::default();
857///
858/// assert!( ( a.x < 0.00001 ) && ( a.y < 0.00001 ) && ( a.z < 0.00001 ) );
859/// ```
860///
861#[derive(Copy, Clone, Debug)]
862pub struct Vec3 {
863    ///
864    pub x: f32,
865    ///
866    pub y: f32,
867    ///
868    pub z: f32,
869    ///
870    coord_system: CoordSystem,
871}
872
873/// Implements Default for Vec3
874/// Generates a new instance of Vec3 initialized to zero and returns it
875///
876/// # Examples
877///
878/// ```
879/// use miscmath::prelude::*;
880///
881/// let a = Vec3::default();
882///
883/// assert!( ( a.x < 0.000001 ) && ( a.y < 0.000001 ) && ( a.z < 0.00001 ) );
884/// ```
885///
886impl Default for Vec3 {
887    fn default() -> Self {
888        Self {
889            x: 0.0,
890            y: 0.0,
891            z: 0.0,
892            coord_system: CARTESIAN,
893        }
894    }
895}
896
897/// Implements Add for Vec3
898///
899/// # Examples
900///
901/// ```
902///
903///
904///
905/// ```
906///
907impl Add for Vec3 {
908    type Output = Self;
909
910    fn add(self, rhs: Self) -> Self::Output {
911        Self {
912            x: self.x + rhs.x,
913            y: self.y + rhs.y,
914            z: self.z + rhs.z,
915            coord_system: self.coord_system,
916        }
917    }
918}
919
920/// Implements AddAssign for Vec3
921///
922/// # Examples
923///
924/// ```
925///
926///
927///
928/// ```
929///
930impl AddAssign for Vec3 {
931    fn add_assign(&mut self, rhs: Self) {
932        *self = *self + rhs;
933    }
934}
935
936/// Implements Sub for Vec3
937///
938/// # Examples
939///
940/// ```
941///
942///
943///
944/// ```
945///
946impl Sub for Vec3 {
947    type Output = Self;
948
949    fn sub(self, rhs: Self) -> Self::Output {
950        Self {
951            x: self.x - rhs.x,
952            y: self.y - rhs.y,
953            z: self.z - rhs.z,
954            coord_system: self.coord_system,
955        }
956    }
957}
958
959/// Implements SubAssign for Vec3
960///
961/// # Examples
962///
963/// ```
964///
965///
966///
967/// ```
968///
969impl SubAssign for Vec3 {
970    fn sub_assign(&mut self, rhs: Self) {
971        *self = *self - rhs;
972    }
973}
974
975/// Implements Mul for Vec3
976///
977/// # Examples
978///
979/// ```
980///
981///
982///
983/// ```
984///
985impl Mul for Vec3 {
986    type Output = Self;
987
988    fn mul(self, rhs: Self) -> Self::Output {
989        Self {
990            x: self.x * rhs.x,
991            y: self.y * rhs.y,
992            z: self.z * rhs.z,
993            coord_system: self.coord_system,
994        }
995    }
996}
997
998/// Implements MulAssign for Vec3
999///
1000/// # Examples
1001///
1002/// ```
1003///
1004///
1005///
1006/// ```
1007///
1008impl MulAssign for Vec3 {
1009    fn mul_assign(&mut self, rhs: Self) {
1010        *self = *self * rhs;
1011    }
1012}
1013
1014/// Implements Div for Vec3
1015///
1016/// # Examples
1017///
1018/// ```
1019///
1020///
1021///
1022/// ```
1023///
1024impl Div for Vec3 {
1025    type Output = Self;
1026
1027    fn div(self, rhs: Self) -> Self::Output {
1028        Self {
1029            x: self.x / rhs.x,
1030            y: self.y / rhs.y,
1031            z: self.z / rhs.z,
1032            coord_system: self.coord_system,
1033        }
1034    }
1035}
1036
1037/// Implements DivAssign for Vec3
1038///
1039/// # Examples
1040///
1041/// ```
1042/// use miscmath::prelude::*;
1043/// 
1044/// let mut x = Vec3::new( &100.0, &50.0, &30.0 );
1045/// let y = Vec3::new( &2.0, &5.0 , &3.0 );
1046/// let z = Vec3::new( &50.0, &10.0, &10.0 );
1047///
1048/// x /= y;
1049/// 
1050/// assert_eq!( x, z );
1051/// ```
1052///
1053impl DivAssign for Vec3 {
1054    fn div_assign(&mut self, rhs: Self) {
1055        *self = *self / rhs;
1056    }
1057}
1058
1059/// Implements Rem for Vec3
1060///
1061/// # Examples
1062///
1063/// ```
1064///
1065///
1066///
1067/// ```
1068///
1069impl Rem for Vec3 {
1070    type Output = Self;
1071
1072    fn rem(self, rhs: Self) -> Self::Output {
1073        Self {
1074            x: self.x % rhs.x,
1075            y: self.y % rhs.y,
1076            z: self.z % rhs.z,
1077            coord_system: self.coord_system,
1078        }
1079    }
1080}
1081
1082/// Implements RemAssign for Vec3
1083///
1084/// # Examples
1085///
1086/// ```
1087///
1088///
1089///
1090/// ```
1091///
1092impl RemAssign for Vec3 {
1093    fn rem_assign(&mut self, rhs: Self) {
1094        *self = *self % rhs;
1095    }
1096}
1097
1098/// Implements PartialEq for Vec3
1099///
1100/// # Examples
1101///
1102/// ```
1103///  
1104///
1105///
1106/// ```
1107///
1108impl PartialEq for Vec3 {
1109    ///
1110    fn eq(&self, rhs: &Self) -> bool {
1111        let temp = *self - *rhs;
1112        if temp.x.abs() < 0.00001
1113            && temp.y.abs() < 0.00001
1114            && temp.z.abs() < 0.00001
1115            && self.coord_system == rhs.coord_system
1116        {
1117            true
1118        } else {
1119            false
1120        }
1121    }
1122
1123    ///
1124    fn ne(&self, rhs: &Self) -> bool {
1125        let temp = *self - *rhs;
1126        if temp.x.abs() > 0.00001 || temp.y.abs() > 0.00001 || temp.z.abs() > 0.00001 {
1127            true
1128        } else {
1129            false
1130        }
1131    }
1132}
1133
1134impl Vec3 {
1135    /*/// Generates a new instance of Vec3 initialized to zero and returns it
1136    ///
1137    /// # Examples
1138    ///
1139    /// ```
1140    /// use miscmath::prelude::*;
1141    ///
1142    /// let a = Vec3::new();
1143    ///
1144    /// assert!( ( a.x < 0.000001 ) && ( a.y < 0.000001 ) && ( a.z < 0.00001 ) );
1145    /// ```
1146    ///
1147    pub fn new() -> Vec3 {
1148        Vec3 {
1149            x: 0.0,
1150            y: 0.0,
1151            z: 0.0,
1152            coord_system: CARTESIAN,
1153        }
1154    }*/
1155
1156    /// Generates a new instance of Vec3 initialized to chosen values and returns it
1157    ///
1158    /// # Examples
1159    ///
1160    /// ```
1161    /// use miscmath::prelude::*;
1162    ///
1163    /// let a = Vec3::new( &5.6, &7.2, &6.8 );
1164    ///
1165    /// assert!( ( ( a.x - 5.6 ) < 0.000001 ) && ( ( a.y - 7.2 ) < 0.000001 ) && ( ( a.z - 6.8 ) < 0.00001 ) );
1166    /// ```
1167    ///
1168    pub fn new(x: &f32, y: &f32, z: &f32) -> Vec3 {
1169        let temp = Vec3 {
1170            x: *x,
1171            y: *y,
1172            z: *z,
1173            coord_system: CARTESIAN,
1174        };
1175        temp
1176    }
1177
1178    /// Generates a new instance of Vec3 initialized to random values in the range entered and returns it
1179    ///
1180    /// # Examples
1181    ///
1182    /// ```
1183    ///
1184    ///
1185    ///
1186    /// ```
1187    ///
1188    pub fn create_random(range_x: &Range<f32>) -> Vec3 {
1189        let temp = Vec3 {
1190            x: rand::thread_rng().gen_range(range_x.clone()),
1191            y: rand::thread_rng().gen_range(range_x.clone()),
1192            z: rand::thread_rng().gen_range(range_x.clone()),
1193            coord_system: CARTESIAN,
1194        };
1195        temp
1196    }
1197
1198    /// Generates a new instance of Vec3 initialized to random values in the range entered and returns it
1199    ///
1200    /// # Examples
1201    ///
1202    /// ```
1203    ///
1204    ///
1205    ///
1206    /// ```
1207    ///
1208    pub fn create_random3(
1209        range_x: &Range<f32>,
1210        range_y: &Range<f32>,
1211        range_z: &Range<f32>,
1212    ) -> Vec3 {
1213        let temp = Vec3 {
1214            x: rand::thread_rng().gen_range(range_x.clone()),
1215            y: rand::thread_rng().gen_range(range_y.clone()),
1216            z: rand::thread_rng().gen_range(range_z.clone()),
1217            coord_system: CARTESIAN,
1218        };
1219        temp
1220    }
1221
1222    /// Generates a new instance of Vec3 based on a entered angles with a magnitude of 1
1223    ///
1224    /// # Examples
1225    ///
1226    /// ```
1227    ///  
1228    ///
1229    ///   
1230    /// ```
1231    ///
1232    pub fn from_angle(theta: &f32, phi: &f32, in_mag: &Option<f32>) -> Vec3 {
1233        if let Some(mag) = in_mag {
1234            let mut temp = Vec3 {
1235                x: *mag,
1236                y: *theta,
1237                z: *phi,
1238                coord_system: POLAR,
1239            };
1240            temp.swap_system(CARTESIAN);
1241            temp
1242        } else {
1243            let mut temp = Vec3 {
1244                x: 1.0,
1245                y: *theta,
1246                z: *phi,
1247                coord_system: POLAR,
1248            };
1249            temp.swap_system(CARTESIAN);
1250            temp
1251        }
1252    }
1253
1254    /// Generates a new instance of Vec3 initialized to random values in the range entered and returns it
1255    ///
1256    /// # Examples
1257    ///
1258    /// ```
1259    ///
1260    ///
1261    ///
1262    /// ```
1263    ///
1264    pub fn from_rand_angle(range: &Range<f32>, mag: &Option<f32>) -> Vec3 {
1265        let temp = Vec3::from_angle(
1266            &rand::thread_rng().gen_range(range.clone()),
1267            &rand::thread_rng().gen_range(range.clone()),
1268            mag,
1269        );
1270        temp
1271    }
1272
1273    /// Generates a new instance of Vec3 initialized to a magnitude of 1 and angles of random value in the range entered and returns it
1274    ///
1275    /// # Examples
1276    ///
1277    /// ```
1278    /// use std::f32::consts::TAU;
1279    /// use miscmath::prelude::*;
1280    ///
1281    /// let mut a = Vec3::random_unit( &( 0.0..TAU ) );
1282    ///
1283    /// assert!( ( a.x - 1.0 < 0.00001 ) && ( a.y < TAU ) && ( a.z < TAU ) );
1284    /// ```
1285    ///
1286    pub fn random_unit(range: &Range<f32>) -> Vec3 {
1287        let temp = Vec3::from_angle(
1288            &rand::thread_rng().gen_range(range.clone()),
1289            &rand::thread_rng().gen_range(range.clone()),
1290            &None,
1291        );
1292        temp
1293    }
1294
1295    /// Generates a new instance of Vec3 initialized as a unit vector with angles of 0 and returns it
1296    ///
1297    /// # Examples
1298    ///
1299    /// ```
1300    /// use miscmath::prelude::*;
1301    ///
1302    /// let mut b = Vec3::unit();
1303    ///
1304    /// assert!( ( b.x < 0.00001 ) && ( b.y < 0.00001 ) && ( b.z - 1.0 < 0.0001 ) );
1305    /// ```
1306    ///
1307    pub fn unit() -> Vec3 {
1308        let temp = Vec3::from_angle(&0.0, &0.0, &Some(1.0));
1309        temp
1310    }
1311
1312    /// Adds the components from the rhs Vec3 to the corresponding components of self
1313    ///
1314    /// # Examples
1315    ///
1316    /// ```
1317    ///  
1318    ///
1319    ///   
1320    /// ```
1321    ///
1322    pub fn add(&mut self, rhs: &Vec3) {
1323        self.swap_system(CARTESIAN);
1324        self.x += rhs.x;
1325        self.y += rhs.y;
1326        self.z += rhs.z;
1327    }
1328
1329    /// Calculates the angle between self and a passed in Vec3
1330    ///
1331    /// # Examples
1332    ///
1333    /// ```
1334    ///
1335    /// ```
1336    ///
1337    pub fn angle_between(&self, rhs: &Vec3) -> f32 {
1338        let dot = self.dot(&rhs);
1339        (dot / (self.mag() * rhs.mag())).acos()
1340    }
1341
1342    ///
1343    ///
1344    /// # Examples
1345    ///
1346    /// ```
1347    ///
1348    ///
1349    ///
1350    /// ```
1351    ///
1352    pub fn constrain(&mut self, x_rng: &Range<f32>, y_rng: &Range<f32>, z_rng: &Range<f32>) {
1353        self.x = self.x.clamp(x_rng.start, x_rng.end);
1354        self.y = self.y.clamp(y_rng.start, y_rng.end);
1355        self.z = self.z.clamp(z_rng.start, z_rng.end);
1356    }
1357
1358    /// Generates a new instance of Vec3 which is perpendicular to the self instance
1359    ///
1360    /// # Examples
1361    ///
1362    /// ```
1363    ///  
1364    ///
1365    ///   
1366    /// ```
1367    ///
1368    pub fn cross(&mut self, rhs: &Vec3) -> Vec3 {
1369        self.swap_system(CARTESIAN);
1370
1371        let x = (self.y * rhs.z) - (self.z * rhs.y);
1372        let y = (self.z * rhs.x) - (self.x * rhs.z);
1373        let z = (self.x * rhs.y) - (self.y * rhs.x);
1374
1375        Vec3 {
1376            x,
1377            y,
1378            z,
1379            coord_system: CARTESIAN,
1380        }
1381    }
1382
1383    /// Calculates the distance between self and the Vec3 passed in
1384    ///
1385    /// # Examples
1386    ///
1387    /// ```
1388    ///
1389    ///
1390    ///
1391    /// ```
1392    ///
1393    pub fn dist(&self, rhs: &Vec3) -> f32 {
1394        let distance = ((self.x - rhs.x).powf(2.0))
1395            + ((self.y - rhs.y).powf(2.0))
1396            + ((self.z - rhs.z).powf(2.0));
1397
1398        distance.sqrt()
1399    }
1400
1401    /// Calculates the distance squared between self and the Vec3 passed in
1402    ///
1403    /// # Examples
1404    ///
1405    /// ```
1406    ///  
1407    ///
1408    ///   
1409    /// ```
1410    ///
1411    pub fn dist_sq(&self, rhs: &Vec3) -> f32 {
1412        let distance = ((self.x - rhs.x).powf(2.0))
1413            + ((self.y - rhs.y).powf(2.0))
1414            + ((self.z - rhs.z).powf(2.0));
1415
1416        distance
1417    }
1418
1419    /// Prints debug information of self to terminal
1420    ///
1421    /// # Examples
1422    ///
1423    /// ```
1424    ///  
1425    ///
1426    ///   
1427    /// ```
1428    ///
1429    pub fn dbg(&self) {
1430        dbg!(self);
1431    }
1432
1433    /// Calculates the dot product between self and a passed in Vec3
1434    ///
1435    /// # Examples
1436    ///
1437    /// ```
1438    ///  
1439    ///
1440    ///   
1441    /// ```
1442    ///
1443    pub fn dot(&self, rhs: &Vec3) -> f32 {
1444        (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
1445    }
1446
1447    /// Divides the components of self by a scalar value rhs
1448    ///
1449    /// # Examples
1450    ///
1451    /// ```
1452    ///  
1453    ///
1454    ///   
1455    /// ```
1456    ///
1457    pub fn div(&mut self, rhs: &f32) {
1458        self.x /= rhs;
1459        self.y /= rhs;
1460        self.z /= rhs;
1461    }
1462    
1463    /// Divides the components of self by a Vec3 rhs
1464    ///
1465    /// # Examples
1466    ///
1467    /// ```
1468    ///
1469    ///
1470    ///
1471    /// ```
1472    ///
1473    pub fn div2(&mut self, rhs: &Vec3) {
1474        self.x /= rhs.x;
1475        self.y /= rhs.y;
1476        self.z /= rhs.z;
1477    }
1478
1479    /// Linearly interpolates between self and a passed in Vec3
1480    ///
1481    /// # Examples
1482    ///
1483    /// ```
1484    /// use miscmath::prelude::*;
1485    ///
1486    ///	let mut a = Vec3::new( &5.6, &7.2, &6.8 );
1487    /// let mut b = Vec3::default();
1488    ///
1489    /// a.lerp( &b, UnitF::new( 0.5 ) );
1490    ///
1491    /// assert!( ( ( a.x - 2.8 ) < 0.00001 ) && ( ( a.y - 3.6 ) < 0.00001 ) && ( a.z - 3.4 ) < 0.00001 );
1492    /// ```
1493    ///
1494    pub fn lerp(&mut self, rhs: &Vec3, amt: UnitF) {
1495        self.swap_system(CARTESIAN);
1496
1497        self.x = -(amt.value() - 1.0) * self.x + (amt.value() * rhs.x);
1498        self.y = -(amt.value() - 1.0) * self.y + (amt.value() * rhs.y);
1499        self.z = -(amt.value() - 1.0) * self.z + (amt.value() * rhs.z);
1500    }
1501
1502    /// Calculates the magnitude of self
1503    ///
1504    /// # Examples
1505    ///
1506    /// ```
1507    ///  
1508    ///
1509    ///   
1510    /// ```
1511    ///
1512    pub fn mag(&self) -> f32 {
1513        (self.x.powf(2.0) + self.y.powf(2.0) + self.z.powf(2.0)).sqrt()
1514    }
1515
1516    /// Calculates the magnitude squared of self
1517    ///
1518    /// # Examples
1519    ///
1520    /// ```
1521    ///  
1522    ///
1523    ///   
1524    /// ```
1525    ///
1526    pub fn mag_sq(&self) -> f32 {
1527        self.x.powf(2.0) + self.y.powf(2.0) + self.z.powf(2.0)
1528    }
1529
1530    /// Multiplies the components of self by a scalar value rhs
1531    ///
1532    /// # Examples
1533    ///
1534    /// ```
1535    ///  
1536    ///
1537    ///   
1538    /// ```
1539    ///
1540    pub fn mult(&mut self, rhs: &f32) {
1541        self.swap_system(CARTESIAN);
1542        self.x *= rhs;
1543        self.y *= rhs;
1544        self.z *= rhs;
1545    }
1546    
1547    /// Multiplies the components of self by a Vec3 rhs
1548    ///
1549    /// # Examples
1550    ///
1551    /// ```
1552    ///
1553    ///
1554    ///
1555    /// ```
1556    ///
1557    pub fn mult2(&mut self, rhs: &Vec3) {
1558        self.swap_system(CARTESIAN);
1559        self.x *= rhs.x;
1560        self.y *= rhs.y;
1561        self.z *= rhs.z;
1562    }
1563
1564    /// Normalizes the magnitude of self to 1, angle is unchanged
1565    ///
1566    /// # Examples
1567    ///
1568    /// ```
1569    ///  
1570    ///
1571    ///   
1572    /// ```
1573    ///
1574    pub fn norm(&mut self) {
1575        self.swap_system(POLAR);
1576        self.x = 1.0;
1577        self.swap_system(CARTESIAN);
1578    }
1579
1580    /// Calculates the theta of self
1581    ///
1582    /// # Examples
1583    ///
1584    /// ```
1585    ///  
1586    ///
1587    ///   
1588    /// ```
1589    ///
1590    pub fn phi(&self) -> f32 {
1591        self.x * (self.y).cos()
1592    }
1593
1594    /// Sets the components of self to the remainder of scalar division by rhs
1595    ///
1596    /// # Examples
1597    ///
1598    /// ```
1599    ///  
1600    ///
1601    ///   
1602    /// ```
1603    ///
1604    pub fn rem(&mut self, rhs: &f32) {
1605        self.swap_system(CARTESIAN);
1606        self.x %= rhs;
1607        self.y %= rhs;
1608        self.z %= rhs;
1609    }
1610
1611    /// Rotates self by the angle entered
1612    ///
1613    /// # Examples
1614    ///
1615    /// ```
1616    ///  
1617    ///
1618    ///   
1619    /// ```
1620    ///
1621    pub fn rotate(&mut self, theta: &f32, phi: &f32) {
1622        self.swap_system(POLAR);
1623        self.y += *theta;
1624        self.z += *phi;
1625        self.swap_system(CARTESIAN);
1626    }
1627
1628    /// Sets components of vector to values entered
1629    ///
1630    /// # Examples
1631    ///
1632    /// ```
1633    ///  
1634    ///
1635    ///   
1636    /// ```
1637    ///
1638    pub fn set(&mut self, input1: &f32, input2: &f32, input3: &f32, coord_system: &CoordSystem) {
1639        if *coord_system == CARTESIAN {
1640            self.swap_system(CARTESIAN);
1641            self.x = *input1;
1642            self.y = *input2;
1643            self.z = *input3;
1644        } else {
1645            self.swap_system(POLAR);
1646            self.x = *input1;
1647            self.y = *input2;
1648            self.z = *input3;
1649            self.swap_system(CARTESIAN);
1650        }
1651    }
1652
1653    /// Sets the magnitude of self
1654    ///
1655    /// # Examples
1656    ///
1657    /// ```
1658    ///  
1659    ///
1660    ///   
1661    /// ```
1662    ///
1663    pub fn set_mag(&mut self, input: &f32) {
1664        self.swap_system(POLAR);
1665        self.x = *input;
1666        self.swap_system(CARTESIAN);
1667    }
1668
1669    /// Sets the theta of self
1670    ///
1671    /// # Examples
1672    ///
1673    /// ```
1674    ///  
1675    ///
1676    ///   
1677    /// ```
1678    ///
1679    pub fn set_theta(&mut self, input: &f32) {
1680        self.swap_system(POLAR);
1681        self.y = *input;
1682        self.swap_system(CARTESIAN);
1683    }
1684
1685    /// Sets the phi of self
1686    ///
1687    /// # Examples
1688    ///
1689    /// ```
1690    ///  
1691    ///
1692    ///   
1693    /// ```
1694    ///
1695    pub fn set_phi(&mut self, input: &f32) {
1696        self.swap_system(POLAR);
1697        self.z = *input;
1698        self.swap_system(CARTESIAN);
1699    }
1700
1701    /// Subtracts the components of the rhs Vec3 from the corresponding components of self
1702    ///
1703    /// # Examples
1704    ///
1705    /// ```
1706    ///  
1707    ///
1708    ///   
1709    /// ```
1710    ///
1711    pub fn sub(&mut self, rhs: &Vec3) {
1712        self.swap_system(CARTESIAN);
1713        self.x -= rhs.x;
1714        self.y -= rhs.y;
1715        self.z -= rhs.z;
1716    }
1717
1718    /// Returns the current coordinate system
1719    ///
1720    /// # Examples
1721    ///
1722    /// ```
1723    ///  
1724    ///
1725    ///   
1726    /// ```
1727    ///
1728    pub fn system(&self) -> CoordSystem {
1729        self.coord_system
1730    }
1731
1732    /// Converts components from cartesian to polar coordinates, and vice versa
1733    ///
1734    /// # Examples
1735    ///
1736    /// ```
1737    ///  
1738    ///
1739    ///   
1740    /// ```
1741    ///
1742    fn swap_system(&mut self, new_coord_system: CoordSystem) {
1743        if self.coord_system == CARTESIAN && new_coord_system == POLAR {
1744            let x = self.x;
1745            let y = self.y;
1746
1747            self.x = (self.x.powf(2.0) + self.y.powf(2.0)).sqrt();
1748            self.y = self.z.atan2(x);
1749            self.z = y.atan2(x);
1750            self.coord_system = new_coord_system;
1751        } else if self.coord_system == POLAR && new_coord_system == CARTESIAN {
1752            let mag = self.x;
1753            let theta = self.y;
1754
1755            self.y = self.x * self.y.sin() * self.z.cos();
1756            self.x = self.x * theta.sin() * self.z.sin();
1757            self.z = mag * theta.cos();
1758            self.coord_system = new_coord_system;
1759        }
1760    }
1761
1762    /// Calculates the theta of self
1763    ///
1764    /// # Examples
1765    ///
1766    /// ```
1767    ///  
1768    ///
1769    ///   
1770    /// ```
1771    ///
1772    pub fn theta(&self) -> f32 {
1773        (self.z).atan2(self.x)
1774    }
1775}