epaint/shapes/
ellipse_shape.rs

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