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
use std::ops::Neg;

use auto_ops::*;

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

impl Default for Vec2 {
    /// Creates a zero vector
    fn default() -> Self {
        Self::zero()
    }
}

impl Vec2 {
    pub const fn new(x: f32, y: f32) -> Self {
        Self{x, y}
    }

    /// Creates (0, 0)
    pub const fn zero() -> Self {
        Self{x: 0.0, y: 0.0}
    }

    /// Creates (1, 1)
    pub const fn one() -> Self {
        Self{x: 1.0, y: 1.0}
    }

    /// Returns the square of the vector's length.
    /// 
    /// Faster to compute than [`magnitude()`](Self::magnitude())
    pub fn sqr_magnitude(&self) -> f32 {
        self.x * self.x + self.y * self.y
    }

    /// Returns the vector's length
    pub fn magnitude(&self) -> f32 {
        self.sqr_magnitude().sqrt()
    }

    /// Normalizes `self` in place
    pub fn normalize(&mut self) -> &mut Self {
        let m = self.magnitude();
        self.x /= m;
        self.y /= m;
        self
    }
    /// Returns a normalized copy of `self`
    pub fn normalized(&self) -> Self {
        *self.clone().normalize()
    }

    /// Returns the dot product of `self` and `b`
    pub fn dot(&self, b: Vec2) -> f32 {
        self.x * b.x + self.y * b.y
    }
}

impl_op_ex!(+= |a: &mut Vec2, b: &Vec2| { a.x += b.x; a.y += b.y; });
impl_op_ex!(-= |a: &mut Vec2, b: &Vec2| { a.x -= b.x; a.y -= b.y; });
impl_op_ex!(*= |a: &mut Vec2, b: &Vec2| { a.x *= b.x; a.y *= b.y; });
impl_op_ex!(/= |a: &mut Vec2, b: &Vec2| { a.x /= b.x; a.y /= b.y; });

impl_op_ex!(*= |a: &mut Vec2, b: &f32| { a.x *= b; a.y *= b });
impl_op_ex!(/= |a: &mut Vec2, b: &f32| { a.x /= b; a.y /= b });

impl_op_ex!(+ |a: &Vec2, b: &Vec2| -> Vec2 { Vec2{x: a.x + b.x, y: a.y + b.y} });
impl_op_ex!(- |a: &Vec2, b: &Vec2| -> Vec2 { Vec2{x: a.x - b.x, y: a.y - b.y} });
impl_op_ex!(* |a: &Vec2, b: &Vec2| -> Vec2 { Vec2{x: a.x * b.x, y: a.y * b.y} });
impl_op_ex!(/ |a: &Vec2, b: &Vec2| -> Vec2 { Vec2{x: a.x / b.x, y: a.y / b.y} });

impl_op_ex_commutative!(* |a: &Vec2, b: &f32| -> Vec2 { Vec2{x: a.x * b, y: a.y * b} });
impl_op_ex!(/ |a: &Vec2, b: &f32| -> Vec2 { Vec2{x: a.x / b, y: a.y / b} });
impl_op_ex!(/ |a: &f32, b: &Vec2| -> Vec2 { Vec2{x: a / b.x, y: a / b.y} });

impl Neg for Vec2 {
    type Output = Vec2;
    fn neg(self) -> Self::Output {
        Vec2{x: -self.x, y: -self.y}
    }
}

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

    #[test]
    fn operators() {
        let a = Vec2::new(1.0, 2.0);
        let b = Vec2::new(3.0, 4.0);
        
        assert_eq!(-a, Vec2{x: -1.0, y: -2.0});

        assert_eq!(a.sqr_magnitude(), 5.0);
        assert_eq!(a.magnitude(), 5.0f32.sqrt());

        assert_eq!(a.dot(b), 11.0);

        assert_eq!(a + b, Vec2{x: 4.0, y: 6.0});
        assert_eq!(a - b, Vec2{x: -2.0, y: -2.0});
        assert_eq!(a * b, Vec2{x: 3.0, y: 8.0});
        assert_eq!(a / b, Vec2{x: 1.0 / 3.0, y: 0.5});

        assert_eq!(a * 2.0, Vec2{x: 2.0, y: 4.0});
        assert_eq!(2.0 * a, Vec2{x: 2.0, y: 4.0});

        assert_eq!(a / 2.0, Vec2{x: 0.5, y: 1.0});
        assert_eq!(2.0 / a, Vec2{x: 2.0, y: 1.0});

        let mut c = a;

        assert_eq!(c.normalized(), a / a.magnitude());

        c.normalize();
        assert_eq!(c, a / a.magnitude());

        c = a;
        c += b;
        assert_eq!(c, a + b);

        c = a;
        c -= b;
        assert_eq!(c, a - b);

        c = a;
        c *= b;
        assert_eq!(c, a * b);

        c = a;
        c /= b;
        assert_eq!(c, a / b);

        c = a;
        c *= 2.0;
        assert_eq!(c, a * 2.0);

        c = a;
        c /= 2.0;
        assert_eq!(c, a / 2.0);
    }

}