gemini_engine/primitives/
polygon.rs

1use super::Triangle;
2use crate::core::{CanDraw, ColChar, Vec2D};
3
4/// A polygon primitive which implements [`CanDraw`], and so can be drawn to [Canvas](crate::core::Canvas)es
5///
6/// It uses triangulation to draw the polygon
7pub struct Polygon {
8    /// The vertices that make up the `Polygon`
9    pub vertices: Vec<Vec2D>,
10    /// The [`ColChar`] used to fill the `Polygon`
11    pub fill_char: ColChar,
12}
13
14impl Polygon {
15    /// Create a new `Polygon`
16    #[must_use]
17    pub fn new(vertices: &[Vec2D], fill_char: ColChar) -> Self {
18        Self {
19            vertices: vertices.to_vec(),
20            fill_char,
21        }
22    }
23}
24
25impl CanDraw for Polygon {
26    fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
27        super::triangulate(&self.vertices)
28            .into_iter()
29            .map(|corners| Triangle::with_array(corners, self.fill_char))
30            .for_each(|t| t.draw_to(canvas));
31    }
32}