microcad_core/geo2d/
circle.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! 2D Geometry
5
6use crate::*;
7
8/// Circle with offset.
9#[derive(Debug, Clone)]
10pub struct Circle {
11    /// Radius of the circle.
12    pub radius: Scalar,
13
14    /// Offset.
15    pub offset: Vec2,
16}
17
18impl CalcBounds2D for Circle {
19    fn calc_bounds_2d(&self) -> Bounds2D {
20        use geo::Coord;
21
22        if self.radius > 0.0 {
23            let r = Vec2::new(self.radius, self.radius);
24            let min: (Scalar, Scalar) = (self.offset - r).into();
25            let max: (Scalar, Scalar) = (self.offset + r).into();
26
27            Some(Rect::new(Coord::from(min), Coord::from(max)))
28        } else {
29            None
30        }
31        .into()
32    }
33}
34
35impl FetchPoints2D for Circle {
36    fn fetch_points_2d(&self) -> Vec<Vec2> {
37        vec![self.offset]
38    }
39}
40
41impl Circle {
42    /// Render a circle with a radius into a polygon.
43    pub fn circle_polygon(radius: Scalar, resolution: &RenderResolution) -> Polygon {
44        let n = resolution.circular_segments(radius);
45        let points = NgonIterator::new(n).map(|p| p * radius).collect();
46        Polygon::new(LineString::new(points), vec![])
47    }
48}