graphics/
polygon.rs

1//! Draw polygon
2
3use crate::{
4    math::{Matrix2d, Scalar},
5    triangulation, types,
6    types::Color,
7    DrawState, Graphics,
8};
9
10/// A polygon
11#[derive(Copy, Clone)]
12pub struct Polygon {
13    /// The color of the polygon
14    pub color: Color,
15}
16
17impl Polygon {
18    /// Creates new polygon
19    pub fn new(color: Color) -> Polygon {
20        Polygon { color }
21    }
22
23    /// Sets color.
24    pub fn color(mut self, color: Color) -> Self {
25        self.color = color;
26        self
27    }
28
29    /// Draws polygon using the default method.
30    #[inline(always)]
31    pub fn draw<G>(
32        &self,
33        polygon: types::Polygon<'_>,
34        draw_state: &DrawState,
35        transform: Matrix2d,
36        g: &mut G,
37    ) where
38        G: Graphics,
39    {
40        g.polygon(self, polygon, draw_state, transform);
41    }
42
43    /// Draws polygon using triangulation.
44    pub fn draw_tri<G>(
45        &self,
46        polygon: types::Polygon<'_>,
47        draw_state: &DrawState,
48        transform: Matrix2d,
49        g: &mut G,
50    ) where
51        G: Graphics,
52    {
53        g.tri_list(draw_state, &self.color, |f| {
54            triangulation::with_polygon_tri_list(transform, polygon, |vertices| f(vertices))
55        });
56    }
57
58    /// Draws tweened polygon with linear interpolation, using default method.
59    #[inline(always)]
60    pub fn draw_tween_lerp<G>(
61        &self,
62        polygons: types::Polygons<'_>,
63        tween_factor: Scalar,
64        draw_state: &DrawState,
65        transform: Matrix2d,
66        g: &mut G,
67    ) where
68        G: Graphics,
69    {
70        g.polygon_tween_lerp(self, polygons, tween_factor, draw_state, transform);
71    }
72
73    /// Draws tweened polygon with linear interpolation, using triangulation.
74    pub fn draw_tween_lerp_tri<G>(
75        &self,
76        polygons: types::Polygons<'_>,
77        tween_factor: Scalar,
78        draw_state: &DrawState,
79        transform: Matrix2d,
80        g: &mut G,
81    ) where
82        G: Graphics,
83    {
84        if self.color[3] == 0.0 {
85            return;
86        }
87        g.tri_list(draw_state, &self.color, |f| {
88            triangulation::with_lerp_polygons_tri_list(
89                transform,
90                polygons,
91                tween_factor,
92                |vertices| f(vertices),
93            )
94        });
95    }
96}
97
98#[cfg(test)]
99mod test {
100    use super::*;
101
102    #[test]
103    fn test_polygon() {
104        let _polygon = Polygon::new([1.0; 4]).color([0.0; 4]);
105    }
106}