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

pub fn distance(a: f32, b: f32) -> f32{
    return f32::sqrt(a*a + b*b);
}

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

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Vector3D {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Vector4D {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}

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

    // returns the dot product of two given vectors
    pub fn dot(vec1: Vector2D, vec2: Vector2D) -> f32 {
        return vec1.x*vec2.x + vec1.y*vec2.y;
    }

    pub fn magnitude(self) -> f32 {
        return f32::sqrt(self.x*self.x + self.y*self.y)
    }

    pub fn normalize(self) -> Vector2D{
        return Vector2D::new(self.x, self.y)/self.magnitude();
    }
}

impl Vector3D {
    pub const ZERO: Vector3D = Vector3D { x: 0.0, y: 0.0, z: 0.0};

    pub fn new(x: f32, y: f32, z: f32) -> Self {
        Self{
            x, y, z
        }
    }

    // AKA: distance, length, module, .....
    pub fn magnitude(self) -> f32 {
        return f32::sqrt(self.x*self.x + self.y*self.y + self.z*self.z);
    }

    // retunrs the dot product between two 3d vectors
    pub fn dot(vec1: Vector3D, vec2: Vector3D) -> f32 {
        return vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z;
    }
    
    // returns the cross product of two given vectors
    pub fn cross(vec1: Vector3D, vec2: Vector3D) -> Vector3D {
        return Vector3D::new(vec1.y * vec2.z - vec1.z * vec1.y, vec1.z * vec1.x - vec1.x * vec2.z, vec1.x * vec2.y - vec1.y * vec2.x);
    }

    pub fn normalize(self) -> Vector3D{
        return Vector3D::new(self.x, self.y, self.z)/self.magnitude();
    }
}

impl Vector4D {
    pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
        Vector4D { x, y, z, w }
    }

    pub fn dot(self, other: Vector4D) -> f32 {
        self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
    }

    pub fn magnitude(self) -> f32 {
        (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
    }

    pub fn normalize(self) -> Self {
        let mag = self.magnitude();
        if mag > 0.0 {
            Self {
                x: self.x / mag,
                y: self.y / mag,
                z: self.z / mag,
                w: self.w / mag,
            }
        } else {
            self
        }
    }
}


// some trait implementations for vector operations 

impl Add for Vector2D {
    type Output = Vector2D;

    fn add(self, rhs: Self) -> Self::Output {
        let x = self.x + rhs.x;
        let y = self.y + rhs.y;
        return Vector2D::new(x, y);
    }
}

impl Sub for Vector2D {
    type Output = Vector2D;

    fn sub(self, rhs: Self) -> Self::Output {
        let x = self.x - rhs.x;
        let y = self.y - rhs.y;
        return Vector2D::new(x, y);
    }
}

impl Mul<f32> for Vector2D {
    type Output = Vector2D;

    fn mul(self, rhs: f32) -> Self::Output {
        return Vector2D::new(self.x*rhs, self.y*rhs);
    }
}

impl Mul for Vector2D {
    type Output = Vector2D;

    fn mul(self, rhs: Self) -> Self::Output {
        return Vector2D::new(self.x*rhs.x, self.y*rhs.y);
    }
}

impl Div<f32> for Vector2D {
    type Output = Vector2D;

    fn div(self, rhs: f32) -> Self::Output {
        return Vector2D::new(self.x/rhs, self.y/rhs);
    }
}


// trait implementations for Vector3D
impl Add for Vector3D {
    type Output = Vector3D;

    fn add(self, rhs: Self) -> Self::Output {
        let x = self.x + rhs.x;
        let y = self.y + rhs.y;
        let z = self.z + rhs.z;
        return Vector3D::new(x, y, z);
    }
}

impl Sub for Vector3D {
    type Output = Vector3D;

    fn sub(self, rhs: Self) -> Self::Output {
        let x = self.x - rhs.x;
        let y = self.y - rhs.y;
        let z = self.z - rhs.z;
        return Vector3D::new(x, y, z);
    }
}

impl Mul<f32> for Vector3D {
    type Output = Vector3D;

    fn mul(self, rhs: f32) -> Self::Output {
        return Vector3D::new(self.x*rhs, self.y*rhs, self.z*rhs);
    }
}

impl Mul for Vector3D {
    type Output = Vector3D;

    fn mul(self, rhs: Self) -> Self::Output {
        return Vector3D::new(self.x*rhs.x, self.y*rhs.y, self.z*rhs.z);
    }
}

impl Div<f32> for Vector3D {
    type Output = Vector3D;

    fn div(self, rhs: f32) -> Self::Output {
        return Vector3D::new(self.x/rhs, self.y/rhs, self.z/rhs);
    }
}

// Trait implementations for vector4d

impl Add for Vector4D {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
            w: self.w + other.w,
        }
    }
}

impl Sub for Vector4D {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
            w: self.w - other.w,
        }
    }
}

impl Mul<f32> for Vector4D {
    type Output = Self;

    fn mul(self, scalar: f32) -> Self {
        Self {
            x: self.x * scalar,
            y: self.y * scalar,
            z: self.z * scalar,
            w: self.w * scalar,
        }
    }
}

impl Div<f32> for Vector4D {
    type Output = Self;

    fn div(self, scalar: f32) -> Self {
        Self {
            x: self.x / scalar,
            y: self.y / scalar,
            z: self.z / scalar,
            w: self.w / scalar,
        }
    }
}