epaint/shapes/
circle_shape.rs

1use crate::{Color32, Pos2, Rect, Shape, Stroke, Vec2};
2
3/// How to paint a circle.
4#[derive(Copy, Clone, Debug, PartialEq)]
5#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
6pub struct CircleShape {
7    pub center: Pos2,
8    pub radius: f32,
9    pub fill: Color32,
10    pub stroke: Stroke,
11}
12
13impl CircleShape {
14    #[inline]
15    pub fn filled(center: Pos2, radius: f32, fill_color: impl Into<Color32>) -> Self {
16        Self {
17            center,
18            radius,
19            fill: fill_color.into(),
20            stroke: Default::default(),
21        }
22    }
23
24    #[inline]
25    pub fn stroke(center: Pos2, radius: f32, stroke: impl Into<Stroke>) -> Self {
26        Self {
27            center,
28            radius,
29            fill: Default::default(),
30            stroke: stroke.into(),
31        }
32    }
33
34    /// The visual bounding rectangle (includes stroke width)
35    pub fn visual_bounding_rect(&self) -> Rect {
36        if self.fill == Color32::TRANSPARENT && self.stroke.is_empty() {
37            Rect::NOTHING
38        } else {
39            Rect::from_center_size(
40                self.center,
41                Vec2::splat(self.radius * 2.0 + self.stroke.width),
42            )
43        }
44    }
45}
46
47impl From<CircleShape> for Shape {
48    #[inline(always)]
49    fn from(shape: CircleShape) -> Self {
50        Self::Circle(shape)
51    }
52}