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
use nalgebra::{base::*, Matrix};
pub type V2 = Matrix<f32, U2, U1, ArrayStorage<f32, U2, U1>>;
pub type V4 = Matrix<f32, U4, U1, ArrayStorage<f32, U4, U1>>;
#[derive(Debug, PartialEq, Clone)]
pub struct Path {
vertices: Vec<V2>,
}
impl Path {
pub fn edges<'a>(&'a self) -> impl Iterator<Item = (&'a V2, &'a V2)> + 'a {
self.vertices().iter().zip(self.vertices().iter().skip(1))
}
pub fn edges_wrapped<'a>(&'a self) -> impl Iterator<Item = (&'a V2, &'a V2)> + 'a {
let wrap_around = self
.vertices
.iter()
.skip(self.vertices.len() - 1)
.zip(self.vertices.iter().take(1));
self.edges().chain(wrap_around)
}
pub fn vertices(&self) -> &[V2] { &self.vertices }
}
impl From<Vec<V2>> for Path {
fn from(vertices: Vec<V2>) -> Self { Path { vertices } }
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn polygon_edges() {
let polygon = Path {
vertices: vec![V2::new(0.0, 0.0), V2::new(0.0, 1.0), V2::new(1.0, 0.0)],
};
assert_eq!(
polygon.edges().collect::<Vec<(&V2, &V2)>>(),
vec![
(&V2::new(0.0, 0.0), &V2::new(0.0, 1.0)),
(&V2::new(0.0, 1.0), &V2::new(1.0, 0.0)),
(&V2::new(1.0, 0.0), &V2::new(0.0, 0.0))
]
);
}
}