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
use std::fmt::{Display, Formatter};
use std::ops::{Add, Div, Mul, Sub};

pub mod vector_2_to_decimal;
pub mod mul;

#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vector2<T> {
    x: T,
    y: T
}

impl<T> Vector2<T> {
    /// Make a new vector
    pub fn new(x: T, y: T) -> Vector2<T> {
        Self {
            x,
            y
        }
    }

    /// Cast a vector from type T to type R
    pub fn cast_to<R: From<T>>(self) -> Vector2<R> {
        Vector2::<R> {
            x: R::from(self.x),
            y: R::from(self.y),
        }
    }
}

impl<T: Add<Output=T>+Mul<Output=T>+Copy> Vector2<T> {

    /// Returns the value of (x * x) + (y * y). Integer types don't implement sqrt so we
    /// can't implement magnitude.
    pub fn square_magnitude(&self) -> T {
        (self.x * self.x) + (self.y * self.y)
    }
}

impl<T: Mul<Output=T>+Add<Output=T>+Sub<Output=T>+Copy> Vector2<T> {
    /// Returns the value of (x_2 - x_1)^2 + (y_2 - y_1)^2. Integer types don't implement
    /// sqrt so we can't implement distance.
    pub fn square_distance(&self, from: &Vector2<T>) -> T {
        let x_dist = from.x - self.x;
        let y_dist = from.y - self.y;

        return (x_dist * x_dist) + (y_dist * y_dist);
    }
}

impl Vector2<f32> {
    pub fn magnitude(&self) -> f32 {
        self.square_magnitude().sqrt()
    }
    pub fn distance(&self, other: &Vector2<f32>) -> f32 {
        self.square_distance(other).sqrt()
    }
}

impl Vector2<f64> {
    pub fn magnitude(&self) -> f64 {
        self.square_magnitude().sqrt()
    }
    pub fn distance(&self, other: &Vector2<f64>) -> f64 {
        self.square_distance(other).sqrt()
    }
}

impl<T> From<(T, T)> for Vector2<T> {
    fn from(value: (T, T)) -> Self {
        Vector2::new(value.0, value.1)
    }
}

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

impl<T:Default> Default for Vector2<T> {
    fn default() -> Self {
        Self {
            x: T::default(),
            y: T::default(),
        }
    }
}

impl<T> Add for Vector2<T> where T: Add<Output = T>{
    type Output = Vector2<T>;

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

impl<T> Sub for Vector2<T> where T: Sub<Output = T>{
    type Output = Vector2<T>;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

impl<T> Mul<T> for Vector2<T> where T: Mul<Output = T> + Copy {
    type Output = Vector2<T>;

    fn mul(self, scalar: T) -> Self::Output {
        Self {
            x: self.x * scalar.clone(),
            y: self.y * scalar,
        }
    }
}

impl<T> Div<T> for Vector2<T> where T: Div<Output = T> + Copy {
    type Output = Vector2<T>;

    fn div(self, scalar: T) -> Self::Output {
        Self {
            x: self.x / scalar.clone(),
            y: self.y / scalar,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::vector_2_to_decimal::{Vector2ToDouble, Vector2ToFloat};
    use super::*;

    #[test]
    fn convert_i32_f32() {
        let v1 = Vector2::new(3, 4);
        let v2 = v1.to_float();
        assert_eq!(v2, Vector2::new(3.0, 4.0))
    }

    #[test]
    fn convert_i32_f64() {
        let v1 = Vector2::new(3, 4);
        let v2 = v1.to_double();
        assert_eq!(v2, Vector2::new(3.0, 4.0))
    }

    #[test]
    fn cast_i32_i64() {
        let v1 = Vector2::new(3, 4);
        let v2: Vector2<i64> = v1.cast_to();
        assert_eq!(v2, Vector2::new(3, 4))
    }

    #[test]
    fn add() {
        let v1 = Vector2::new(1, 2);
        let v2 = Vector2::new(2, 2);
        assert_eq!(v1 + v2, Vector2::new(3, 4))
    }

    #[test]
    fn sub() {
        let v1 = Vector2::new(1, 2);
        let v2 = Vector2::new(2, 2);
        assert_eq!(v2 - v1, Vector2::new(1, 0))
    }

    #[test]
    fn mul() {
        let v1 = Vector2::new(1, 1);
        let v2 = 2 * v1;
        assert_eq!(v2, Vector2::new(2, 2))
    }

    #[test]
    fn div() {
        let v1 = Vector2::new(2, 2);
        let v2 = v1 / 2;

        assert_eq!(v2, Vector2::new(1, 1))
    }
}