shape_core/elements/polygons/
dim3.rs1use super::*;
2
3impl<T> Polygon3D<T> {
4 pub fn new(points: &[Point3D<T>]) -> Self
5 where
6 T: Clone,
7 {
8 Self { vertex: points.to_vec() }
9 }
10 pub fn edges(&self) -> impl Iterator<Item = Line3D<&T>> {
11 debug_assert!(self.vertex.len() >= 3, "Polygon must have at least three points");
12 self.vertex
13 .iter()
14 .cycle()
15 .take(self.vertex.len() + 1)
16 .tuple_windows()
17 .map(|(a, b)| Line3D { s: a.ref_inner(), e: b.ref_inner() })
18 }
19 pub fn center(&self) -> Point3D<T>
21 where
22 T: Zero + One + Clone + AddAssign + Div<Output = T>,
23 {
24 let mut n = T::zero();
25 let mut x = T::zero();
26 let mut y = T::zero();
27 let mut z = T::zero();
28 for p in self.vertex.iter() {
29 x.add_assign(p.x.clone());
30 y.add_assign(p.y.clone());
31 z.add_assign(p.z.clone());
32 n.add_assign(T::one());
33 }
34 Point3D { x: x / n.clone(), y: y / n.clone(), z: z / n.clone() }
35 }
36}