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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#[cfg(feature="nalgebra")] use nalgebra::{
    core::Vector2,
    geometry::Point2
};

use crate::geom::{about_equal, Scalar};
use rand::{
    Rng,
    distributions::{Distribution, Standard}
};
use std::{
    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
    cmp::{Eq, PartialEq},
    fmt
};
#[cfg(not(target_arch = "wasm32"))]
use glutin::dpi::{LogicalPosition, PhysicalPosition, LogicalSize, PhysicalSize};


#[derive(Copy, Clone, Default, Debug, Deserialize, Serialize)]
///A 2D vector with an arbitrary numeric type
pub struct Vector {
    ///The x coordinate of the vector
    pub x: f32,
    ///The y coordinate of the vector
    pub y: f32,
}


impl Vector {
    /// A vector with x = 0, y = 0
    pub const ZERO: Vector = Vector { x: 0f32, y: 0f32 };
    /// A vector with x = 1, y = 0
    pub const X: Vector    = Vector { x: 1f32, y: 0f32 };
    /// A vector with x = 0, y = 1
    pub const Y: Vector    =  Vector { x: 0f32, y: 1f32 };
    /// A vector with x = 1, y = 1
    pub const ONE: Vector  =  Vector { x: 1f32, y: 1f32 };
}

impl Vector {
    ///Create a new vector
    pub fn new(x: impl Scalar, y: impl Scalar) -> Vector {
        Vector { x: x.float(), y: y.float() }
    }

    ///Convert this vector into an nalgebra Vector2
    #[cfg(feature="nalgebra")]
    pub fn into_vector(self) -> Vector2<f32> {
        Vector2::new(self.x, self.y)
    }
   
    ///Convert this vector into an nalgebra Point2
    #[cfg(feature="nalgebra")]
    pub fn into_point(self) -> Point2<f32> {
        Point2::new(self.x, self.y)
    }

    ///Create a unit vector at a given angle
    pub fn from_angle<T: Scalar>(angle: T) -> Vector {
        Vector::new(angle.float().to_radians().cos(), angle.float().to_radians().sin())
    }

    ///Get the squared length of the vector (faster than getting the length)
    pub fn len2(self) -> f32 {
        self.x * self.x + self.y * self.y
    }

    ///Get the length of the vector
    pub fn len(self) -> f32 {
        self.len2().sqrt()
    }

    ///Clamp a vector somewhere between a minimum and a maximum
    #[must_use]
    pub fn clamp(self, min_bound: impl Into<Vector>, max_bound: impl Into<Vector>) -> Vector {
        let (min_bound, max_bound) = (min_bound.into(), max_bound.into());
        Vector::new(
            max_bound.x.min(min_bound.x.max(self.x)),
            max_bound.y.min(min_bound.y.max(self.y)),
        )
    }
    
    ///Get the cross product of a vector
    pub fn cross(self, other: impl Into<Vector>) -> f32 {
        let other = other.into();
        self.x * other.y - self.y * other.x
    }

    ///Get the dot product of a vector
    pub fn dot(self, other: impl Into<Vector>) -> f32 {
        let other = other.into();
        self.x * other.x + self.y * other.y
    }

    ///Normalize the vector's length from [0, 1]
    #[must_use]
    pub fn normalize(self) -> Vector {
        self / self.len()
    }

    ///Get only the X component of the Vector, represented as a vector
    #[must_use]
    pub fn x_comp(self) -> Vector {
        Vector::new(self.x, 0f32)
    }

    ///Get only the Y component of the Vector, represented as a vector
    #[must_use]
    pub fn y_comp(self) -> Vector {
        Vector::new(0f32, self.y)
    }

    ///Get the vector equal to Vector(1 / x, 1 / y)
    #[must_use]
    pub fn recip(self) -> Vector {
        Vector::new(self.x.recip(), self.y.recip())
    }

    ///Multiply the components in the matching places
    #[must_use]
    pub fn times(self, other: impl Into<Vector>) -> Vector {
        let other = other.into();
        Vector::new(self.x * other.x, self.y * other.y)
    }

    ///Get the angle a vector forms with the positive x-axis, counter clockwise
    pub fn angle(self) -> f32 {
        self.y.atan2(self.x).to_degrees()
    }

    ///Create a vector with the same angle and the given length
    #[must_use]
    pub fn with_len(self, length: f32) -> Vector {
        self.normalize() * length
    }

    ///Get the Euclidean distance to another vector
    pub fn distance(self, other: impl Into<Vector>) -> f32 {
        let other = other.into();
        ((other.x - self.x).powi(2) + (other.y - self.y).powi(2)).sqrt()
    }

    ///Get a vector with the minimum of each component of this and another vector
    pub fn min(self, other: impl Into<Vector>) -> Vector {
        let other = other.into();
        Vector::new(
            self.x.min(other.x),
            self.y.min(other.y)
        )
    }

    ///Get a vector with the maximum of each component of this and another vector
    pub fn max(self, other: impl Into<Vector>) -> Vector {
        let other = other.into();
        Vector::new(
            self.x.max(other.x),
            self.y.max(other.y)
        )
    }
}

impl Neg for Vector {
    type Output = Vector;

    fn neg(self) -> Vector {
        Vector::new(-self.x, -self.y)
    }
}

impl Add for Vector {
    type Output = Vector;

    fn add(self, rhs: Vector) -> Vector {
        Vector::new(self.x + rhs.x, self.y + rhs.y)
    }
}

impl AddAssign for Vector {
    fn add_assign(&mut self, rhs: Vector) -> () {
        *self = *self + rhs;
    }
}

impl Sub for Vector {
    type Output = Vector;

    fn sub(self, rhs: Vector) -> Vector {
        self + (-rhs)
    }
}

impl SubAssign for Vector {
    fn sub_assign(&mut self, rhs: Vector) -> () {
        *self = *self - rhs;
    }
}

impl<T: Scalar> Div<T> for Vector {
    type Output = Vector;

    fn div(self, rhs: T) -> Vector {
        let rhs = rhs.float();
        Vector::new(self.x / rhs, self.y / rhs)
    }
}

impl<T: Scalar> DivAssign<T> for Vector {
    fn div_assign(&mut self, rhs: T) -> () {
        let rhs = rhs.float();
        *self = *self / rhs;
    }
}

impl<T: Scalar> Mul<T> for Vector {
    type Output = Vector;

    fn mul(self, rhs: T) -> Vector {
        let rhs = rhs.float();
        Vector::new(self.x * rhs, self.y * rhs)
    }
}

impl<T: Scalar> MulAssign<T> for Vector {
    fn mul_assign(&mut self, rhs: T) -> () {
        let rhs = rhs.float();
        *self = *self * rhs;
    }
}


impl PartialEq for Vector {
    fn eq(&self, other: &Vector) -> bool {
        about_equal(self.x, other.x) && about_equal(self.y, other.y)
    }
}

impl Eq for Vector {}

impl fmt::Display for Vector {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "<{}, {}>", self.x, self.y)
    }
}

impl Distribution<Vector> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rand: &mut R) -> Vector {
        Vector {
            x: self.sample(rand),
            y: self.sample(rand)
        }
    }
}

#[cfg(feature="nalgebra")]
impl From<Vector2<f32>> for Vector {
    fn from(other: Vector2<f32>) -> Vector {
        Vector::new(other.x, other.y)
    }
}

#[cfg(feature="nalgebra")]
impl From<Point2<f32>> for Vector {
    fn from(other: Point2<f32>) -> Vector {
        Vector::new(other.x, other.y)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Vector> for LogicalPosition {
    fn from(other: Vector) -> LogicalPosition {
        LogicalPosition::new(other.x as f64, other.y as f64)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Vector> for PhysicalPosition {
    fn from(other: Vector) -> PhysicalPosition {
        PhysicalPosition::new(other.x as f64, other.y as f64)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<LogicalPosition> for Vector {
    fn from(other: LogicalPosition) -> Vector {
        Vector::new(other.x as f32, other.y as f32)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<PhysicalPosition> for Vector {
    fn from(other: PhysicalPosition) -> Vector {
        Vector::new(other.x as f32, other.y as f32)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Vector> for LogicalSize {
    fn from(other: Vector) -> LogicalSize {
        LogicalSize::new(other.x as f64, other.y as f64)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Vector> for PhysicalSize {
    fn from(other: Vector) -> PhysicalSize {
        PhysicalSize::new(other.x as f64, other.y as f64)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<LogicalSize> for Vector {
    fn from(other: LogicalSize) -> Vector {
        Vector::new(other.width as f32, other.height as f32)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<PhysicalSize> for Vector {
    fn from(other: PhysicalSize) -> Vector {
        Vector::new(other.width as f32, other.height as f32)
    }
}

impl<T: Scalar, U: Scalar> From<(T, U)> for Vector {
    fn from(other: (T, U)) -> Vector {
        Vector::new(other.0, other.1)
    }
}

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

    #[test]
    fn arithmetic() {
        let a = Vector::new(5, 10);
        let b = Vector::new(1, -2);
        assert!((a + b).x == 6f32);
        assert!((a - b).y == 12f32);
    }

    #[test]
    fn equality() {
        assert_eq!(Vector::new(5, 5), Vector::new(5, 5));
        assert_ne!(Vector::new(0, 5), Vector::new(5, 5));
    }

    #[test]
    fn inverse() {
        let vec = Vector::new(3, 5);
        let inverse = vec.recip();
        assert_eq!(Vector::new(1.0 / 3.0, 1.0 / 5.0), inverse);
    }

    #[test]
    fn length() {
        let vec = Vector::X * 5;
        assert!(about_equal(vec.len2(), 25f32));
        assert!(about_equal(vec.len(), 5f32));
    }

    #[test]
    fn scale() {
        let vec = Vector::new(1, 1);
        let doubled = Vector::new(2, 2);
        assert_eq!(vec * 2, doubled);
        let halved = Vector::new(0.5, 0.5);
        assert_eq!(vec / 2, halved);
    }

    #[test]
    fn clamp() {
        let min = Vector::new(-10, -2);
        let max = Vector::new(5, 6);
        let vec = Vector::new(-11, 3);
        let clamped = vec.clamp(min, max);
        let expected = Vector::new(-10, 3);
        assert_eq!(clamped, expected);
    }

    #[test]
    fn dot() {
        assert!(about_equal(Vector::new(6, 5).dot(Vector::new(2, -8)), -28f32));
    }

    #[test]
    fn times() {
        let vec = Vector::new(3, -2);
        let two = Vector::ONE * 2;
        assert_eq!(vec.times(two), vec * 2);
    }

    #[test]
    fn angle() {
        let a = Vector::X;
        let b = Vector::Y;
        let c = a + b;
        assert_eq!(a.angle(), 0.0);
        assert_eq!(b.angle(), 90.0);
        assert_eq!(c.angle(), 45.0);
    }

    #[test]
    fn distance() {
        let a = Vector::X;
        let b = Vector::Y;
        let c = a + b;
        assert_eq!(a.distance(a), 0.0);
        assert_eq!(a.distance(Vector::ZERO), 1.0);
        assert_eq!(b.distance(a), 2_f32.sqrt());
        assert_eq!(c.distance(Vector::ZERO), 2_f32.sqrt());
    }
}