visim/
vector.rs

1
2#[derive(Clone, Copy, Debug)]
3#[repr(C, packed)]
4pub struct Vector2 {
5    pub x: f32,
6    pub y: f32
7}
8
9impl Vector2 {
10    pub fn new(x: f32, y: f32) -> Self {
11        Self { x, y }
12    }
13
14    pub fn mag(&self) -> f32 {
15        let scalar = self.x * self.x + self.y * self.y;
16        scalar.sqrt()
17    }
18
19    pub fn norm(&mut self) {
20        let mag = self.mag();
21        if mag != 0.0 {
22            // self /= mag;
23        }
24    }
25}
26
27impl From<(f32, f32)> for Vector2 {
28    fn from(value: (f32, f32)) -> Self {
29        Self { x: value.0, y: value.1 }
30    }
31}
32
33impl std::ops::Add<Self> for Vector2 {
34    type Output = Self;
35
36    fn add(self, rhs: Self) -> Self::Output {
37        Self { x: self.x + rhs.x, y: self.y + rhs.y }
38    }
39}
40
41impl std::ops::Sub<Self> for Vector2 {
42    type Output = Self;
43
44    fn sub(self, rhs: Self) -> Self::Output {
45        Self { x: self.x - rhs.x, y: self.y - rhs.y }
46    }
47}
48
49impl std::ops::Mul<Self> for Vector2 {
50    type Output = Self;
51
52    fn mul(self, rhs: Self) -> Self::Output {
53        Self { x: self.x * rhs.x, y: self.y * rhs.y }
54    }
55}
56
57impl std::ops::Div<Self> for Vector2 {
58    type Output = Self;
59
60    fn div(self, rhs: Self) -> Self::Output {
61        Self { x: self.x / rhs.x, y: self.y / rhs.y }
62    }
63}
64
65impl std::ops::AddAssign<Self> for Vector2 {
66    fn add_assign(&mut self, rhs: Self) {
67        self.x += rhs.x;
68        self.y += rhs.y;
69    }
70}
71
72impl std::ops::SubAssign<Self> for Vector2 {
73    fn sub_assign(&mut self, rhs: Self) {
74        self.x -= rhs.x;
75        self.y -= rhs.y;
76    }
77}
78
79impl std::ops::MulAssign<Self> for Vector2 {
80    fn mul_assign(&mut self, rhs: Self) {
81        self.x *= rhs.x;
82        self.y *= rhs.y;
83    }
84}
85
86impl std::ops::DivAssign<Self> for Vector2 {
87    fn div_assign(&mut self, rhs: Self) {
88        self.x /= rhs.x;
89        self.y /= rhs.y;
90    }
91}
92
93impl std::ops::Add<f32> for Vector2 {
94    type Output = Self;
95
96    fn add(self, rhs: f32) -> Self::Output {
97        Self { x: self.x + rhs, y: self.y + rhs }
98    }
99}
100
101impl std::ops::Sub<f32> for Vector2 {
102    type Output = Self;
103
104    fn sub(self, rhs: f32) -> Self::Output {
105        Self { x: self.x - rhs, y: self.y - rhs }
106    }
107}
108
109impl std::ops::Mul<f32> for Vector2 {
110    type Output = Self;
111
112    fn mul(self, rhs: f32) -> Self::Output {
113        Self { x: self.x * rhs, y: self.y * rhs }
114    }
115}
116
117impl std::ops::Div<f32> for Vector2 {
118    type Output = Self;
119
120    fn div(self, rhs: f32) -> Self::Output {
121        Self { x: self.x / rhs, y: self.y / rhs }
122    }
123}
124
125impl std::ops::AddAssign<f32> for Vector2 {
126    fn add_assign(&mut self, rhs: f32) {
127        self.x += rhs;
128        self.y += rhs;
129    }
130}
131
132impl std::ops::SubAssign<f32> for Vector2 {
133    fn sub_assign(&mut self, rhs: f32) {
134        self.x -= rhs;
135        self.y -= rhs;
136    }
137}
138
139impl std::ops::MulAssign<f32> for Vector2 {
140    fn mul_assign(&mut self, rhs: f32) {
141        self.x *= rhs;
142        self.y *= rhs;
143    }
144}
145
146impl std::ops::DivAssign<f32> for Vector2 {
147    fn div_assign(&mut self, rhs: f32) {
148        self.x /= rhs;
149        self.y /= rhs;
150    }
151}
152
153impl std::ops::Neg for Vector2 {
154    type Output = Self;
155
156    fn neg(self) -> Self::Output {
157        Self { x: -self.x, y: -self.y }
158    }
159}