graphics/
circle_arc.rs

1//! Draw an arc
2
3use crate::{
4    math::Matrix2d,
5    triangulation,
6    types::{Color, Radius, Rectangle, Resolution, Scalar},
7    DrawState, Graphics,
8};
9
10/// A curved line
11#[derive(Copy, Clone)]
12pub struct CircleArc {
13    /// The arcs color
14    pub color: Color,
15
16    /// The radius of the arc (Thickness of the drawing, not the radius of the circle)
17    pub radius: Radius,
18
19    /// The start of the arc in radians
20    pub start: Scalar,
21
22    /// The end of the arc in radians
23    pub end: Scalar,
24
25    /// The resolution for the arc.
26    pub resolution: Resolution,
27}
28
29impl CircleArc {
30    /// Creates a new arc
31    pub fn new(color: Color, radius: Radius, start: Scalar, end: Scalar) -> CircleArc {
32        CircleArc {
33            color,
34            radius,
35            start,
36            end,
37            resolution: 128,
38        }
39    }
40
41    /// Sets the arcs color.
42    pub fn color(mut self, value: Color) -> Self {
43        self.color = value;
44        self
45    }
46
47    /// Sets the radius of the arc (Thickness of the arc, not the radius of the circle it wraps)
48    pub fn radius(mut self, value: Radius) -> Self {
49        self.radius = value;
50        self
51    }
52
53    /// Sets the start of the arc (in radians).
54    pub fn start(mut self, value: Scalar) -> Self {
55        self.start = value;
56        self
57    }
58
59    /// Sets the end of the arc (in radians).
60    pub fn end(mut self, value: Scalar) -> Self {
61        self.end = value;
62        self
63    }
64
65    /// Sets the resolution of the arcs smoothness.
66    pub fn resolution(mut self, value: Resolution) -> Self {
67        self.resolution = value;
68        self
69    }
70
71    /// Draws circle arc using default method.
72    #[inline(always)]
73    pub fn draw<R: Into<Rectangle>, G>(
74        &self,
75        rectangle: R,
76        draw_state: &DrawState,
77        transform: Matrix2d,
78        g: &mut G,
79    ) where
80        G: Graphics,
81    {
82        g.circle_arc(self, rectangle, draw_state, transform);
83    }
84
85    /// Draws circle arc using triangulation.
86    pub fn draw_tri<R: Into<Rectangle>, G>(
87        &self,
88        rectangle: R,
89        draw_state: &DrawState,
90        transform: Matrix2d,
91        g: &mut G,
92    ) where
93        G: Graphics,
94    {
95        let rectangle = rectangle.into();
96        g.tri_list(draw_state, &self.color, |f| {
97            triangulation::with_arc_tri_list(
98                self.start,
99                self.end,
100                self.resolution,
101                transform,
102                rectangle,
103                self.radius,
104                |vertices| f(vertices),
105            )
106        });
107    }
108}
109
110#[cfg(test)]
111mod test {
112    use super::*;
113    use crate::{radians::Radians, types::Scalar};
114
115    #[test]
116    fn test_circle_arc() {
117        let _arc = CircleArc::new([1.0; 4], 4.0, 0.0, Radians::_180())
118            .color([0.0; 4])
119            .radius(4.0)
120            .start(<Scalar as Radians>::_180() * 0.25)
121            .end(<Scalar as Radians>::_180() * 1.25);
122    }
123}