lgeo/shapes/
polygon.rs

1use std::fmt::{Display, Formatter};
2use lmaths::*;
3use crate::Shape;
4use crate::util::*;
5
6#[allow(dead_code)]
7#[derive(Clone, Default, PartialEq, Debug)]
8/// A Convex polygon, made by at least three vertices.
9pub struct Polygon {
10    pub position: Vector2,
11    pub vertices: Vec<Vector2>
12}
13
14#[allow(dead_code)]
15impl Polygon {
16    /// Create a new polygon with the provided set of vertices
17    pub fn new(position:Vector2, vertices: Vec<Vector2>) -> Self {
18        Self { position, vertices }
19    }
20}
21
22impl Shape for Polygon {
23    fn position(&self) -> Vector2 {
24        self.position
25    }
26
27    fn support_point(&self, direction: Vector2) -> Vector2 {
28        get_max_point(self.vertices.iter(), direction, self.position())
29    }
30}
31
32impl Display for Polygon {
33    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34        write!(f, "Polygon pos({0}), vertices_count: {1}", self.position, self.vertices.len())
35    }
36}