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
#[allow(unused_imports)]
use crate::f32ext::F32Ext;
use core::{
fmt::Debug,
ops::{Index, MulAssign},
};
use generic_array::{ArrayLength, GenericArray};
mod component;
mod iter;
mod xy;
mod xyz;
pub use self::{component::*, iter::*, xy::*, xyz::*};
pub trait Vector:
Copy + Debug + Default + Index<usize> + MulAssign<f32> + PartialEq + Sized + Send + Sync
{
type Component: Component;
type Axes: ArrayLength<Self::Component>;
const MIN: Self::Component;
const MAX: Self::Component;
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = Self::Component>;
fn from_slice(slice: &[Self::Component]) -> Self {
Self::from_iter(slice.iter().cloned())
}
fn get(self, index: usize) -> Option<Self::Component>;
fn iter(&self) -> Iter<Self> {
Iter::new(self)
}
fn distance(self, other: Self) -> f32 {
let differences = self
.iter()
.zip(other.iter())
.map(|(a, b)| a.into() - b.into());
differences.map(|n| n * n).sum::<f32>().sqrt()
}
fn magnitude(self) -> f32 {
self.iter()
.map(|n| {
let n = n.into();
n * n
})
.sum::<f32>()
.sqrt()
}
fn to_array(self) -> GenericArray<Self::Component, Self::Axes>;
}