-
Notifications
You must be signed in to change notification settings - Fork 0
/
3d_vector_calc.rs
60 lines (59 loc) · 1.82 KB
/
3d_vector_calc.rs
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
#[derive(Copy, Clone)]
struct Vector {
i: f64,
j: f64,
k: f64,
}
impl Vector {
fn new(i: f64, j: f64, k: f64) -> Self {
Vector { i: i, j: j, k: k }
}
fn get_magnitude(&self) -> f64 {
(self.i * self.i + self.j * self.j + self.k * self.k).sqrt()
}
fn get_i() -> Vector {
Vector::new(1.0, 0.0, 0.0)
}
fn get_j() -> Vector {
Vector::new(0.0, 1.0, 0.0)
}
fn get_k() -> Vector {
Vector::new(0.0, 0.0, 1.0)
}
fn add(&self, other: Vector) -> Vector {
Vector::new(self.i + other.i, self.j + other.j, self.k + other.k)
}
fn multiply_by_scalar(&self, k: f64) -> Vector {
Vector::new(self.i * k, self.j * k, self.k * k)
}
fn dot(&self, other: Vector) -> f64 {
self.i * other.i + self.j * other.j + self.k * other.k
}
fn cross(&self, other: Vector) -> Vector {
Vector::new(
self.j * other.k - self.k * other.j,
self.k * other.i - self.i * other.k,
self.i * other.j - self.j * other.i,
)
}
fn is_parallel_to(&self, other: Vector) -> bool {
let u = self.cross(other);
!(self.i == 0.0 && self.j == 0.0 && self.k == 0.0)
&& !(other.i == 0.0 && other.j == 0.0 && other.k == 0.0)
&& u.i.abs() < 1e-6
&& u.j.abs() < 1e-6
&& u.k.abs() < 1e-6
}
fn is_perpendicular_to(&self, other: Vector) -> bool {
!(self.i == 0.0 && self.j == 0.0 && self.k == 0.0)
&& !(other.i == 0.0 && other.j == 0.0 && other.k == 0.0)
&& self.dot(other).abs() < 1e-6
}
fn normalize(&self) -> Vector {
let m = self.get_magnitude();
Vector::new(self.i / m, self.j / m, self.k / m)
}
fn is_normalized(&self) -> bool {
(1.0 - self.get_magnitude()).abs() < 1e-6
}
}