1
2pub trait Vec {
3 fn get_component_count(&self) -> usize;
4 fn get_component(&self, index: usize) -> f32;
5 fn set_component(&mut self, index: usize, value: f32);
6
7 fn abs(&mut self) -> &mut Self {
8 for i in 0..self.get_component_count() { self.set_component(i,
9 self.get_component(i).abs()
10 ); }
11 self
12 }
13 fn add(&mut self, other: &impl Vec) -> &mut Self {
14 for i in 0..self.get_component_count() { self.set_component(i,
15 self.get_component(i) + other.get_component(i)
16 ); }
17 self
18 }
19 fn sub(&mut self, other: &impl Vec) -> &mut Self {
20 for i in 0..self.get_component_count() { self.set_component(i,
21 self.get_component(i) - other.get_component(i)
22 ); }
23 self
24 }
25 fn mul(&mut self, other: &impl Vec) -> &mut Self {
26 for i in 0..self.get_component_count() { self.set_component(i,
27 self.get_component(i) * other.get_component(i)
28 ); }
29 self
30 }
31 fn div(&mut self, other: &impl Vec) -> &mut Self {
32 for i in 0..self.get_component_count() { self.set_component(i,
33 self.get_component(i) / other.get_component(i)
34 ); }
35 self
36 }
37 fn modulo(&mut self, other: &impl Vec) -> &mut Self {
38 for i in 0..self.get_component_count() { self.set_component(i,
39 self.get_component(i) % other.get_component(i)
40 ); }
41 self
42 }
43 fn min(&mut self, other: &impl Vec) -> &mut Self {
44 for i in 0..self.get_component_count() { self.set_component(i,
45 self.get_component(i).min(other.get_component(i))
46 ); }
47 self
48 }
49 fn max(&mut self, other: &impl Vec) -> &mut Self {
50 for i in 0..self.get_component_count() { self.set_component(i,
51 self.get_component(i).max(other.get_component(i))
52 ); }
53 self
54 }
55
56 fn len(&self) -> f32 {
57 let mut length_squared: f32 = 0.0;
58 for i in 0..self.get_component_count() {
59 length_squared += self.get_component(i).powi(2);
60 }
61 length_squared.sqrt()
62 }
63 fn neg(&mut self) -> &mut Self {
64 for i in 0..self.get_component_count() { self.set_component(i,
65 -self.get_component(i)
66 ); }
67 self
68 }
69 fn scale(&mut self, f: f32) -> &mut Self {
70 for i in 0..self.get_component_count() { self.set_component(i,
71 self.get_component(i) * f
72 ); }
73 self
74 }
75 fn normal(&mut self) -> &mut Self {
76 let length: f32 = self.len();
77 if length == 0.0 { return self; }
78 self.scale(1.0 / length);
79 self
80 }
81
82 fn dist(&self, other: &impl Vec) -> f32 {
83 let mut dist_squared: f32 = 0.0;
84 for i in 0..self.get_component_count() {
85 dist_squared += (self.get_component(i) - other.get_component(i)).powi(2);
86 }
87 dist_squared.sqrt()
88 }
89 fn dot(&self, other: &impl Vec) -> f32 {
90 let mut dot_product: f32 = 0.0;
91 for i in 0..self.get_component_count() {
92 dot_product += self.get_component(i) * other.get_component(i);
93 }
94 dot_product
95 }
96 fn angle(&self, other: &impl Vec) -> f32 {
97 (self.dot(other) / (self.len() * other.len())).acos()
98 }
99}
100
101
102#[derive(Clone, Copy)]
103pub struct Vec2 {
104 pub x: f32,
105 pub y: f32
106}
107impl Vec2 {
108 pub fn new(x: f32, y: f32) -> Vec2 { Vec2 { x, y } }
109}
110impl Vec for Vec2 {
111 fn get_component_count(&self) -> usize { 2 }
112 fn get_component(&self, index: usize) -> f32 {
113 match index {
114 0 => self.x,
115 1 => self.y,
116 _ => 0.0
117 }
118 }
119 fn set_component(&mut self, index: usize, value: f32) {
120 match index {
121 0 => self.x = value,
122 1 => self.y = value,
123 _ => {}
124 }
125 }
126}
127impl ToString for Vec2 {
128 fn to_string(&self) -> String {
129 format!("[{}, {}]", self.x, self.y)
130 }
131}
132
133
134#[derive(Clone, Copy)]
135pub struct Vec3 {
136 pub x: f32,
137 pub y: f32,
138 pub z: f32
139}
140impl Vec3 {
141 pub fn new(x: f32, y: f32, z: f32) -> Vec3 { Vec3 { x, y, z } }
142}
143impl Vec for Vec3 {
144 fn get_component_count(&self) -> usize { 3 }
145 fn get_component(&self, index: usize) -> f32 {
146 match index {
147 0 => self.x,
148 1 => self.y,
149 2 => self.z,
150 _ => 0.0
151 }
152 }
153 fn set_component(&mut self, index: usize, value: f32) {
154 match index {
155 0 => self.x = value,
156 1 => self.y = value,
157 2 => self.z = value,
158 _ => {}
159 }
160 }
161}
162impl ToString for Vec3 {
163 fn to_string(&self) -> String {
164 format!("[{}, {}, {}]", self.x, self.y, self.z)
165 }
166}
167
168
169#[derive(Clone, Copy)]
170pub struct Vec4 {
171 pub x: f32,
172 pub y: f32,
173 pub z: f32,
174 pub w: f32
175}
176impl Vec4 {
177 pub fn new(x: f32, y: f32, z: f32, w: f32) -> Vec4 { Vec4 { x, y, z, w } }
178}
179impl Vec for Vec4 {
180 fn get_component_count(&self) -> usize { 4 }
181 fn get_component(&self, index: usize) -> f32 {
182 match index {
183 0 => self.x,
184 1 => self.y,
185 2 => self.z,
186 3 => self.w,
187 _ => 0.0
188 }
189 }
190 fn set_component(&mut self, index: usize, value: f32) {
191 match index {
192 0 => self.x = value,
193 1 => self.y = value,
194 2 => self.z = value,
195 3 => self.w = value,
196 _ => {}
197 }
198 }
199}
200impl ToString for Vec4 {
201 fn to_string(&self) -> String {
202 format!("[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
203 }
204}