maker_panel/features/
circle.rs

1use geo::{Coordinate, MultiPolygon, Point, Polygon};
2use std::fmt;
3
4/// A circular region.
5#[derive(Debug, Clone)]
6pub struct Circle<U = super::Unit> {
7    center: Coordinate<f64>,
8    radius: f64,
9    inner: U,
10}
11
12impl Circle {
13    /// Constructs a new circle using the provided center and radius.
14    pub fn new(center: Coordinate<f64>, radius: f64) -> Self {
15        Self {
16            center,
17            radius,
18            inner: super::Unit,
19        }
20    }
21
22    /// Constructs a new circle with the provided radius, centered on
23    /// the origin.
24    pub fn with_radius(radius: f64) -> Self {
25        Self::new([0.0, 0.0].into(), radius)
26    }
27}
28
29impl<U: super::InnerFeature + Clone> Circle<U> {
30    /// Constructs a circle surrounding the inner feature. The
31    /// origin of the inner feature will be positioned at the
32    /// center of the circle.
33    pub fn with_inner(mut inner: U, center: Coordinate<f64>, radius: f64) -> Self {
34        inner.translate(center);
35
36        Self {
37            center,
38            radius,
39            inner,
40        }
41    }
42
43    /// Constructs a circle with a given radius surrounding the inner feature.
44    pub fn wrap_with_radius(inner: U, radius: f64) -> Self {
45        Self {
46            radius,
47            inner,
48            center: [0.0, 0.0].into(),
49        }
50    }
51}
52
53impl<U: super::InnerFeature> fmt::Display for Circle<U> {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        write!(
56            f,
57            "circle({:?}, r = {:?}, U = {})",
58            self.center, self.radius, self.inner
59        )
60    }
61}
62
63impl<U: super::InnerFeature + Clone + std::fmt::Debug> super::Feature for Circle<U> {
64    fn name(&self) -> &'static str {
65        "circle"
66    }
67
68    fn edge_union(&self) -> Option<MultiPolygon<f64>> {
69        use geo::algorithm::rotate::RotatePoint;
70        let right_edge: Point<_> = (self.center.x + self.radius, self.center.y).into();
71        let mut out = Vec::with_capacity(361);
72
73        let num_points = (self.radius * 20.0).ceil() as usize;
74        let step = 360.0 / num_points as f64;
75
76        for i in 0..=num_points {
77            out.push(right_edge.rotate_around_point(i as f64 * step, self.center.into()));
78        }
79
80        Some(MultiPolygon(vec![Polygon::new(
81            geo::LineString(out.into_iter().map(|p| p.into()).collect()),
82            vec![],
83        )]))
84    }
85
86    fn translate(&mut self, v: Coordinate<f64>) {
87        self.center = self.center + v;
88        self.inner.translate(v);
89    }
90
91    fn interior(&self) -> Vec<super::InnerAtom> {
92        self.inner.atoms()
93    }
94}