summaryrefslogtreecommitdiff
path: root/src/geometry/vec.rs
blob: c99cf5e5396f952330eff39af543febc7ba1063f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::ops::*;
use num_traits::{NumOps, NumAssignOps, Signed};
use num_traits::pow::Pow;
use num_traits::real::Real;

macro_rules! fold_array {
    ($method:ident, { $x:expr }) => { $x };
    ($method:ident, { $x:expr, $y:expr }) => { $x.$method($y) };
    ($method:ident, { $x:expr, $y:expr, $z:expr }) => { $x.$method($y).$method($z) };
}

macro_rules! impl_vector {
    ($VecN:ident { $($field:ident),+ }) => {
        #[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
        pub struct $VecN<T> {
            $(pub $field: T),+
        }
        
        impl<T> $VecN<T> {
            pub const fn new($($field: T),+) -> $VecN<T> {
                $VecN { $($field),+ }
            }
        }

        impl<T: NumOps + Ord + Signed + Copy> $VecN<T> {
            pub fn walking_distance(&self) -> T {
                fold_array!(max, { $(self.$field.abs()),+ })
            }
        }
        
        impl<T: NumOps + Pow<u8, Output=T> + Copy> $VecN<T> {
            pub fn magnitude_squared(&self) -> T {
                fold_array!(add, { $(self.$field.pow(2)),+ })
            }
        }

        impl<T: Real + Pow<u8, Output=T>> $VecN<T> {
            pub fn magnitude(&self) -> T {
                self.magnitude_squared().sqrt()
            }

            pub fn unit(&self) -> $VecN<T> {
                let mag = self.magnitude();
                $VecN {
                    $($field: self.$field / mag),+
                }
            }
        }

        impl<T: NumOps + Copy> $VecN<T> {
            pub fn dot(&self, other: $VecN<T>) -> T {
                fold_array!(add, { $(self.$field * other.$field),+ })
            }
        }

        impl<T: NumOps> Add for $VecN<T> {
            type Output = Self;

            fn add(self, other: Self) -> Self {
                $VecN {
                    $($field: self.$field + other.$field),+
                }
            }
        }

        impl<T: NumAssignOps> AddAssign for $VecN<T> {
            fn add_assign(&mut self, other: Self) {
                $(self.$field += other.$field);+
            }
        }

        impl<T: NumOps> Sub for $VecN<T> {
            type Output = Self;

            fn sub(self, other: Self) -> Self {
                $VecN {
                    $($field: self.$field - other.$field),+
                }
            }
        }

        impl<T: NumOps + Copy> Mul<T> for $VecN<T> {
            type Output = Self;

            fn mul(self, rhs: T) -> Self {
                $VecN {
                    $($field: self.$field * rhs),+
                }
            }
        }

        impl<T: Neg<Output=T>> Neg for $VecN<T> {
            type Output = Self;

            fn neg(self) -> Self {
                $VecN {
                    $($field: -self.$field),+
                }
            }
        }

    }
}

impl_vector!(Vec2d { x, y });
impl_vector!(Vec3d { x, y, z });

impl<T: Real + Pow<u8, Output=T>> Vec2d<T> {
    pub fn angle(&self) -> T {
        self.y.atan2(self.x)
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn right_angles_in_2d_vectors() {
        use std::f32::consts::{FRAC_PI_2, PI};

        assert_eq!(0., Vec2d::new(1., 0.).angle());
        assert_eq!(FRAC_PI_2, Vec2d::new(0., 1.).angle());
        assert_eq!(PI, Vec2d::new(-1., 0.).angle());
        assert_eq!(-FRAC_PI_2, Vec2d::new(0., -1.).angle());
    }

    #[test]
    fn unit_normalizes_2d_vector() {
        let before = Vec2d::new(2., 10.);
        let after = before.unit();

        assert_eq!(1., after.magnitude());
        assert_eq!(before.angle(), after.angle());
    }

    #[test]
    fn unit_normalizes_3d_vector() {
        let before = Vec3d::new(2., 10., -5.);
        let after = before.unit();

        assert_eq!(1., after.magnitude());
        // How to define 3d angle? 2 angles I suppose?
        // assert_eq!(before.angle(), after.angle());
    }
    
    #[test]
    fn dot_product_example_2d() {
        let a = Vec2d::new(-6., 8.);
        let b = Vec2d::new(5., 12.);
        assert_eq!(66., a.dot(b));
    }

    #[test]
    fn magnitude_squared_of_an_integer() {
        let a = Vec2d::new(3, 4);
        assert_eq!(25, a.magnitude_squared());
    }
}