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
pub mod cartesian1;
pub mod cartesian2;
pub mod cartesian3;
pub mod space;
pub use self::cartesian1::*;
pub use self::cartesian2::*;
pub use self::cartesian3::*;
pub use self::space::*;
extern crate num;
use self::num::{Float, Zero};
use std::ops::{Add, Sub, Neg, Mul, Div};
pub trait Vector<D>: Sized + Clone + Copy + Zero + Add<Self, Output=Self> + Sub<Self, Output=Self> + Neg<Output=Self> +
Mul<D, Output=Self> + Div<D, Output=Self>
where D: Float
{
fn space_ball(d: D) -> D;
fn dot(&lhs: &Self, rhs: &Self) -> D;
fn space_box(&self) -> D;
fn displacement(&self) -> D;
fn displacement_squared(&self) -> D {
self.displacement().powi(2)
}
fn normalized(&self) -> Self {
*self / self.displacement()
}
fn normalize(&mut self) {
*self = self.normalized();
}
}
#[test]
fn dot_vector() {
let a = Cartesian2::new(0.3, 0.5);
let _b = Cartesian2::dot(&a, &Cartesian2::new(1.0, 0.5));
}
pub trait CrossVector {
fn cross(lhs: &Self, rhs: &Self) -> Self;
}
#[test]
fn cross_vector() {
let a = Cartesian3::new(0.3, 0.5, 1.0);
let _b = Cartesian3::cross(&a, &Cartesian3::new(1.0, 0.5, -2.0));
}