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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use std::ops;

/// Vect is 2D vector and is used all over the place. I choose to use f32 because

/// opengl also accepts only f32

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Vect{
    pub x: f32,
    pub y: f32,
}

impl Vect {
    pub const ZERO: Self = Self { x: 0f32, y: 0f32 };
    pub const LEFT: Self = Self { x: -1f32, y: 0f32 };
    pub const RIGHT: Self = Self { x: 1f32, y: 0f32 };
    pub const UP: Self = Self { x: 0f32, y: 1f32 };
    pub const DOWN: Self = Self { x: 0f32, y: -1f32 };
    pub const MAX: Self = Self { x: f32::MAX, y: f32::MAX };
    pub const MIN: Self = Self { x: f32::MIN, y: f32::MIN };

    /// average returns average of slice of vectors

    #[inline]
    pub fn average(arr: &[Vect]) -> Self {
        let len = arr.len();
        if len == 0 {
            return Self::ZERO;
        }

        let mut total = Self::ZERO;
        for vec in arr {
            total += *vec
        }

        total / len as f32
    }

    /// new is vector constructor

    #[inline]
    pub fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    /// mirror returns homogenous vector

    #[inline]
    pub fn mirror(m: f32) -> Self {
        Self { x: m, y: m }
    }

    /// unit returns vector of length 1.0 with given angle

    #[inline]
    pub fn unit(a: f32) -> Self {
        Self { x: a.cos(), y: a.sin() }
    }

    /// rad is same as unit but you can also specify length

    #[inline]
    pub fn rad(a: f32, l: f32) -> Self {
        Self::unit(a) * l
    }

    /// clamped clamps a vectors length

    #[inline]
    pub fn clamped(&self, min: f32, max: f32) -> Self {
        Self::rad(self.ang(), clamp!(self.len(), min, max))
    }

    pub fn lerp(self, o: Vect, t: f32) -> Vect {
        o * t + self * (1.0 - t)
    }

    /// ang returns vectors angle

    #[inline]
    pub fn ang(&self) -> f32 {
        self.y.atan2(self.x)
    }

    /// len returns vectors length

    #[inline]
    pub fn len(&self) -> f32 {
        self.x.hypot(self.y)
    }

    /// norm returns normalized vector with length 1.0

    #[inline]
    pub fn norm(self) -> Self {
        let len = self.len();
        if len == 0f32 {
            return Self::ZERO
        }
        self / len
    }

    /// normal on the other hand returns normal vector to vector of same length

    #[inline]
    pub fn normal(&self) -> Self {
        Self {
            x: self.y,
            y: -self.x,
        }
    }

    /// swp swaps x and y of vector

    #[inline]
    pub fn swp(self) -> Self {
        Self { x: self.y, y: self.x }
    }

    /// rot rotates vector by a

    #[inline]
    pub fn rot(self, a: f32) -> Self {
        Self::rad(self.ang() + a, self.len())
    }

    /// dist returns distance to other vector

    #[inline]
    pub fn dist(self, b: Self) -> f32 {
        (self - b).len()
    }

    /// to returns vector from self to b

    #[inline]
    pub fn to(self, b: Self) -> Self {
        b - self
    }

    /// dot returns vectors dot

    #[inline]
    pub fn dot(self, b: Self) -> f32 {
        self.x * b.x + self.y * b.y
    }

    /// ang_to returns smallest angle between two vectors

    #[inline]
    pub fn ang_to(self, b: Self) -> f32 {
        let r = self.norm().dot(b.norm()).acos();
        if r.is_nan() { 0.0 } else { r }
    }

    /// trn applies closure to both x and y of a vector

    #[inline]
    pub fn trn<T: Fn(f32) -> f32>(&self, tr: T) -> Self {
        Self { x: tr(self.x), y: tr(self.y) }
    }

    /// inverted inverts vector

    #[inline]
    pub fn inverted(&self) -> Self {
        Self { x: -self.x, y: -self.y }
    }

    /// round rounds a vector

    #[inline]
    pub fn round(&self) -> Self {
        Self { x: self.x.round(), y: self.y.round() }
    }
}

impl ops::Add<Vect> for Vect {
    type Output = Vect;
    #[inline]
    fn add(self, rhs: Vect) -> Self::Output {
        Self::new(self.x + rhs.x, self.y + rhs.y)
    }
}

impl ops::AddAssign<Vect> for Vect {
    #[inline]
    fn add_assign(&mut self, rhs: Vect) {
        self.x += rhs.x;
        self.y += rhs.y;
    }
}

impl ops::Sub<Vect> for Vect {
    type Output = Vect;
    #[inline]
    fn sub(self, rhs: Vect) -> Self::Output {
        Self::new(self.x - rhs.x, self.y - rhs.y)
    }
}

impl ops::SubAssign<Vect> for Vect {
    #[inline]
    fn sub_assign(&mut self, rhs: Vect) {
        self.x -= rhs.x;
        self.y -= rhs.y;
    }
}

impl ops::Mul<Vect> for Vect {
    type Output = Vect;
    #[inline]
    fn mul(self, rhs: Vect) -> Self::Output {
        Self::new(self.x * rhs.x, self.y * rhs.y)
    }
}

impl ops::MulAssign<Vect> for Vect {
    #[inline]
    fn mul_assign(&mut self, rhs: Vect) {
        self.x *= rhs.x;
        self.y *= rhs.y;
    }
}

impl ops::Mul<f32> for Vect {
    type Output = Vect;
    #[inline]
    fn mul(self, rhs: f32) -> Self::Output {
        Self::new(self.x * rhs, self.y * rhs)
    }
}

impl ops::MulAssign<f32> for Vect {
    #[inline]
    fn mul_assign(&mut self, rhs: f32) {
        self.x *= rhs;
        self.y *= rhs;
    }
}

impl ops::Div<Vect> for Vect {
    type Output = Vect;
    #[inline]
    fn div(self, rhs: Vect) -> Self::Output {
        Self::new(self.x / rhs.x, self.y / rhs.y)
    }
}

impl ops::DivAssign<Vect> for Vect {
    #[inline]
    fn div_assign(&mut self, rhs: Vect) {
        self.x /= rhs.x;
        self.y /= rhs.y;
    }
}

impl ops::Div<f32> for Vect {
    type Output = Vect;
    #[inline]
    fn div(self, rhs: f32) -> Self::Output {
        Self::new(self.x / rhs, self.y / rhs)
    }
}

impl ops::DivAssign<f32> for Vect {
    #[inline]
    fn div_assign(&mut self, rhs: f32) {
        self.x /= rhs;
        self.y /= rhs;
    }
}

impl Default for Vect {
    fn default() -> Self {
        Vect::ZERO
    }
}

#[cfg(test)]
mod tests {
    use std::f32::consts::PI;
    use crate::math::vect::Vect;

    fn round(a: f32, decimals: i32) -> f32 {
        let mul = 10f32.powi(decimals);
        (a * mul).round() / mul
    }

    #[test]
    fn angle_test() {
        assert_eq!(PI, Vect::LEFT.ang())
    }
    #[test]
    fn ang_to_test() {
        assert_eq!(PI, Vect::LEFT.ang_to(Vect::RIGHT))
    }
    #[test]
    fn rot_test() {
        assert_eq!(Vect::LEFT.x,round(Vect::RIGHT.rot(PI).x, 6));
        assert_eq!(Vect::LEFT.y,round(Vect::RIGHT.rot(PI).y, 6));
    }
    #[test]
    fn average_test() {
        let vec = vec![Vect::LEFT, Vect::RIGHT];
        assert_eq!(Vect::average(&vec), Vect::ZERO);
    }
}