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
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,
}

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 fn new(x: f32, y: f32, z: f32) -> Self {
        Self{
            x, y, z
        }
    }

    // AKA: magnitude, 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();
    }
}

// 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);
    }
}