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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright (c) 2018-2020 Thomas Kramer.
// SPDX-FileCopyrightText: 2018-2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Two dimensional vectors are a core data type for Euclidean geometry in the plane.
//! `Vector`s consist of an `x` and `y` coordinate value and describe a translation in the plane.

use std::fmt;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

pub use num_traits::Zero;
use num_traits::{Float, NumCast};

use crate::point::Point;
use crate::traits::{MapPointwise, TryCastCoord};
pub use crate::traits::{Mirror, RotateOrtho};
pub use crate::types::Angle;
pub use crate::types::Orientation;
pub use crate::CoordinateType;

/// [`Vector`] defines a two dimensional vector with x and y components in the Euclidean plane.
#[derive(Copy, Clone, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Vector<T> {
    /// `x` coordinate.
    pub x: T,
    /// `y` coordinate.
    pub y: T,
}

/// Shorthand notation for creating a vector.
///
/// # Example
/// ```
/// # #[macro_use]
/// # extern crate iron_shapes;
/// # fn main() {
/// use iron_shapes::prelude::*;
/// let v = vector!(1, 2);
/// assert_eq!(v, Vector::new(1, 2));
/// # }
/// ```
#[macro_export]
macro_rules! vector {
    ($x:expr, $y:expr) => {
        Vector::new($x, $y)
    };
}

impl<T: Copy> From<Vector<T>> for (T, T) {
    #[inline]
    fn from(v: Vector<T>) -> Self {
        (v.x, v.y)
    }
}

impl<T: Copy> From<&Vector<T>> for (T, T) {
    #[inline]
    fn from(v: &Vector<T>) -> Self {
        (v.x, v.y)
    }
}

impl<T: Copy> From<(T, T)> for Vector<T> {
    #[inline]
    fn from(coords: (T, T)) -> Self {
        Vector::new(coords.0, coords.1)
    }
}

impl<'a, T: Copy> From<&'a (T, T)> for Vector<T> {
    #[inline]
    fn from(coords: &'a (T, T)) -> Self {
        Vector::new(coords.0, coords.1)
    }
}

impl<'a, T: Copy> From<&'a Vector<T>> for Vector<T> {
    #[inline]
    fn from(v: &'a Vector<T>) -> Self {
        Vector::new(v.x, v.y)
    }
}

impl<T: Copy> From<[T; 2]> for Vector<T> {
    #[inline]
    fn from(coords: [T; 2]) -> Self {
        Vector::new(coords[0], coords[1])
    }
}

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

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

impl<T: Zero> Zero for Vector<T> {
    /// Get zero-vector.
    ///
    /// # Examples
    /// ```
    /// use iron_shapes::vector::{Vector, Zero};
    ///
    /// let a = Vector::zero();
    /// let b = Vector::new(0, 0);
    ///
    /// assert_eq!(a, b);
    /// ```
    #[inline]
    fn zero() -> Self {
        Vector {
            x: T::zero(),
            y: T::zero(),
        }
    }

    /// Check if this is the zero-vector.
    ///
    /// # Examples
    /// ```
    /// use iron_shapes::vector::{Vector, Zero};
    ///
    /// assert!(Vector::<usize>::zero().is_zero());
    /// ```
    #[inline]
    fn is_zero(&self) -> bool {
        self.x.is_zero() && self.y.is_zero()
    }
}

impl<T> Vector<T> {
    /// Create a new vector with `x` and `y` coordinates.
    /// # Examples
    /// ```
    /// use iron_shapes::vector::Vector;
    /// let a = Vector::new(2, 3);
    /// assert_eq!(a.x, 2);
    /// assert_eq!(a.y, 3);
    /// ```
    pub fn new(x: T, y: T) -> Self {
        Vector { x, y }
    }
}

impl<T: Copy + Zero + PartialOrd + Sub<Output = T>> Vector<T> {
    /// Get 1-norm of vector, i.e. the sum of the absolute values of its components.
    ///
    /// # Examples
    /// ```
    /// use iron_shapes::vector::Vector;
    /// let a = Vector::new(-2, 3);
    /// assert_eq!(a.norm1(), 5);
    /// ```
    #[inline]
    pub fn norm1(&self) -> T {
        let xabs = if self.x < T::zero() {
            T::zero() - self.x
        } else {
            self.x
        };
        let yabs = if self.y < T::zero() {
            T::zero() - self.y
        } else {
            self.y
        };
        xabs + yabs
    }
}

impl<T: Copy + Zero + PartialOrd + Mul<Output = T> + Sub<Output = T>> Vector<T> {
    /// Check if `other` is oriented clockwise or counter-clockwise respective to `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// use iron_shapes::vector::Vector;
    /// use iron_shapes::types::Orientation;
    ///
    /// let a = Vector::new(1, 0);
    /// let b = Vector::new(1, 1);
    /// let c = Vector::new(1, -1);
    /// let d = Vector::new(2, 0);
    ///
    /// assert_eq!(a.orientation_of(b), Orientation::CounterClockWise);
    /// assert_eq!(a.orientation_of(c), Orientation::ClockWise);
    /// assert_eq!(a.orientation_of(d), Orientation::Straight);
    /// ```
    pub fn orientation_of(&self, other: Self) -> Orientation {
        let p = self.cross_prod(other);

        if p < T::zero() {
            Orientation::ClockWise
        } else if p > T::zero() {
            Orientation::CounterClockWise
        } else {
            Orientation::Straight
        }
    }
}

impl<T: Copy + Mul<Output = T> + Add<Output = T>> Vector<T> {
    /// Get squared 2-norm of vector.
    ///
    /// # Examples
    /// ```
    /// use iron_shapes::vector::Vector;
    /// let a = Vector::new(2, 3);
    /// assert_eq!(a.norm2_squared(), 2*2+3*3);
    /// ```
    #[inline]
    pub fn norm2_squared(&self) -> T {
        self.x * self.x + self.y * self.y
    }

    /// Calculate scalar product.
    ///
    /// # Examples
    ///
    /// ```
    /// use iron_shapes::vector::Vector;
    ///
    /// let a = Vector::new(1, 2);
    /// let b = Vector::new(3, 4);
    ///
    /// assert_eq!(a.dot(b), 1*3 + 2*4);
    /// ```
    pub fn dot(&self, other: Self) -> T {
        self.x * other.x + self.y * other.y
    }
}

impl<T: Copy + Mul<Output = T> + Sub<Output = T>> Vector<T> {
    /// Calculate cross product.
    ///
    /// # Examples
    ///
    /// ```
    /// use iron_shapes::vector::Vector;
    ///
    /// let a = Vector::new(2, 0);
    /// let b = Vector::new(0, 2);
    ///
    /// assert_eq!(a.cross_prod(b), 4);
    /// assert_eq!(b.cross_prod(a), -4);
    /// ```
    #[inline]
    pub fn cross_prod(&self, other: Self) -> T {
        self.x * other.y - other.x * self.y
    }
}

impl<T> Vector<T>
where
    T: Copy + NumCast,
{
    /// Convert vector into a vector with floating point data type.
    #[inline]
    pub fn cast_to_float<F>(&self) -> Vector<F>
    where
        F: Float + NumCast,
    {
        // TODO: find conversion that does not panic for sure.
        Vector {
            x: F::from(self.x).unwrap(),
            y: F::from(self.y).unwrap(),
        }
    }
}

impl<T: Copy + NumCast, Dst: Copy + NumCast> TryCastCoord<T, Dst> for Vector<T> {
    type Output = Vector<Dst>;

    /// Try to cast to vector of target data type.
    ///
    /// Conversion from float to int can fail and will return `None`.
    /// Float values like infinity or non-a-number
    /// have no integer representation.
    ///
    /// # Examples
    ///
    /// ```
    /// use iron_shapes::vector::Vector;
    /// use iron_shapes::traits::TryCastCoord;
    ///
    /// let v_int = Vector::new(1,2);
    /// let maybe_v_float: Option<Vector<f64>> = v_int.try_cast();
    ///
    /// assert_eq!(maybe_v_float, Some(Vector::new(1.0, 2.0)));
    ///
    /// // Conversion from float to int can fail.
    ///
    /// let w_float = Vector::new(42.0, 0. / 0.);
    /// let maybe_w_int: Option<Vector<i32>> = w_float.try_cast();
    ///
    /// assert_eq!(maybe_w_int, None);
    /// ```
    #[inline]
    fn try_cast(&self) -> Option<Self::Output> {
        match (Dst::from(self.x), Dst::from(self.y)) {
            (Some(x), Some(y)) => Some(Vector::new(x, y)),
            _ => None,
        }
    }
}

impl<T> Vector<T>
where
    T: Copy + Float + Mul<Output = T> + Add<Output = T>,
{
    /// Get 2-norm of vector (length of vector).
    ///
    /// # Examples
    /// ```
    /// use iron_shapes::vector::Vector;
    /// let a = Vector::new(2.0, 3.0);
    /// let norm2 = a.norm2();
    /// let norm2_sq = norm2 * norm2;
    /// let expected = a.norm2_squared();
    /// assert!(norm2_sq < expected + 1e-12);
    /// assert!(norm2_sq > expected - 1e-12);
    /// ```
    #[inline]
    pub fn norm2(&self) -> T {
        (self.norm2_squared()).sqrt()
    }

    /// Return a vector with the same direction but length 1.
    ///
    /// # Panics
    /// Panics if the vector has length 0.
    #[inline]
    pub fn normalized(&self) -> Self {
        *self / self.norm2()
    }

    /// Return the normal vector onto this vector.
    /// The normal has length `1`.
    ///
    /// # Panics
    /// Panics if the vector has length 0.
    #[inline]
    pub fn normal(&self) -> Self {
        self.normalized().rotate_ortho(Angle::R90)
    }
}

impl<T: Copy + NumCast> Vector<T> {
    /// Calculate length of vector.
    ///
    /// Similar to `Vector::norm2` but does potentially return another data type for the length.
    ///
    /// # Examples
    /// ```
    /// use iron_shapes::vector::Vector;
    /// let a = Vector::new(2.0, 3.0);
    /// let length: f64 = a.length();
    /// let norm2_sq = length * length;
    /// let expected = a.norm2_squared();
    /// assert!(norm2_sq < expected + 1e-12);
    /// assert!(norm2_sq > expected - 1e-12);
    /// ```
    #[inline]
    pub fn length<F: Float>(&self) -> F {
        let x = F::from(self.x).unwrap();
        let y = F::from(self.y).unwrap();

        x.hypot(y)
    }
}

/// Vector addition.
impl<T> Add for Vector<T>
where
    T: Add<Output = T>,
{
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self {
        Vector {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

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

/// Vector subtraction.
impl<T> Sub for Vector<T>
where
    T: Sub<Output = T>,
{
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self {
        Vector {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

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

impl<T> Neg for Vector<T>
where
    T: Neg<Output = T>,
{
    type Output = Self;

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

/// Scalar multiplication.
impl<T, M> Mul<M> for Vector<T>
where
    T: Mul<M, Output = T>,
    M: Copy,
{
    type Output = Self;

    fn mul(self, rhs: M) -> Self {
        Vector {
            x: self.x * rhs,
            y: self.y * rhs,
        }
    }
}

/// In-place scalar multiplication.
impl<T, M> MulAssign<M> for Vector<T>
where
    T: Copy + MulAssign<M>,
    M: Copy,
{
    #[inline]
    fn mul_assign(&mut self, rhs: M) {
        self.x *= rhs;
        self.y *= rhs;
    }
}

/// Scalar division.
impl<T, D> Div<D> for Vector<T>
where
    T: Copy + Div<D, Output = T>,
    D: Copy,
{
    type Output = Self;

    #[inline]
    fn div(self, rhs: D) -> Self {
        Vector {
            x: self.x / rhs,
            y: self.y / rhs,
        }
    }
}

/// Assigning scalar division.
impl<T, D> DivAssign<D> for Vector<T>
where
    T: Copy + DivAssign<D>,
    D: Copy,
{
    #[inline]
    fn div_assign(&mut self, rhs: D) {
        self.x /= rhs;
        self.y /= rhs;
    }
}

impl<T> MapPointwise<T> for Vector<T>
where
    T: Copy,
{
    #[inline]
    fn transform<F>(&self, transformation: F) -> Self
    where
        F: Fn(Point<T>) -> Point<T>,
    {
        *transformation(Point::from(self))
    }
}

/// Compute the sum of all vectors in the iterator.
/// If the iterator is empty, (0, 0) is returned.
impl<T> std::iter::Sum for Vector<T>
where
    T: Zero + Add<Output = T>,
{
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::zero(), |acc, v| acc + v)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use num_rational::Rational64;
    use num_traits::Zero;

    #[test]
    fn test_rational_vector() {
        let _ = Vector::new(Rational64::zero(), Rational64::zero());
    }
}