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
use std::{
    cmp::PartialEq,
    fmt::{Display, Result},
};

/// A pair of `isize` used for coordinates, size or direction on a 2D plane
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
pub struct Vec2D {
    /// X-coordinate
    pub x: isize,
    /// Y-coordinate
    pub y: isize,
}

impl Vec2D {
    impl_vec_single_value_const!(Vec2D, ZERO, 0, (x, y));

    impl_vec_core!(Vec2D, isize, (x, y));

    /// The length/magnitude of the `Vec2D`
    #[must_use]
    pub fn magnitude(&self) -> f64 {
        ((self.x.pow(2) + self.y.pow(2)) as f64).sqrt()
    }

    /// Returns the cross product between two vectors
    #[must_use]
    pub const fn cross(&self, other: Self) -> isize {
        self.x * other.y - self.y * other.x
    }
}

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

impl<T: Into<isize>> From<(T, T)> for Vec2D {
    fn from(value: (T, T)) -> Self {
        Self {
            x: value.0.into(),
            y: value.1.into(),
        }
    }
}

impl_vec_add!(Vec2D, (x, y));
impl_vec_sub!(Vec2D, (x, y));
impl_vec_neg!(Vec2D, 0, (x, y));
impl_vec_mul!(Vec2D, (x, y));
impl_vec_mul_single!(Vec2D, isize, (x, y));
impl_vec_div!(Vec2D, (x, y));
impl_vec_div_single!(Vec2D, isize, (x, y));
impl_vec_rem!(Vec2D, (x, y));

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

    #[test]
    fn eq_vec2_both() {
        assert_eq!(Vec2D::new(5, 4), Vec2D::new(5, 4));
    }

    #[test]
    fn eq_vec2_only_one() {
        assert_ne!(Vec2D::new(5, 2), Vec2D::new(5, 4));
    }

    #[test]
    fn eq_vec2_neither() {
        assert_ne!(Vec2D::new(17, 2), Vec2D::new(5, 4));
    }

    #[test]
    fn negative_vec2() {
        assert_eq!(-Vec2D::new(4, -5), Vec2D::new(-4, 5));
    }
}