diff options
Diffstat (limited to 'src/geom/path.rs')
| -rw-r--r-- | src/geom/path.rs | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/geom/path.rs b/src/geom/path.rs index dcabb9cf..8878b6f1 100644 --- a/src/geom/path.rs +++ b/src/geom/path.rs @@ -22,6 +22,25 @@ impl Path { Self(vec![]) } + /// Create a path that approximates an axis-aligned ellipse. + pub fn ellipse(size: Size) -> Self { + // https://stackoverflow.com/a/2007782 + let rx = size.width / 2.0; + let ry = size.height / 2.0; + let m = 0.551784; + let mx = m * rx; + let my = m * ry; + let z = Length::ZERO; + let point = Point::new; + let mut path = Self::new(); + path.move_to(point(-rx, z)); + path.cubic_to(point(-rx, my), point(-mx, ry), point(z, ry)); + path.cubic_to(point(mx, ry), point(rx, my), point(rx, z)); + path.cubic_to(point(rx, -my), point(mx, -ry), point(z, -ry)); + path.cubic_to(point(-mx, -ry), point(-rx, -my), point(z - rx, z)); + path + } + /// Push a [`MoveTo`](PathElement::MoveTo) element. pub fn move_to(&mut self, p: Point) { self.0.push(PathElement::MoveTo(p)); @@ -42,22 +61,3 @@ impl Path { self.0.push(PathElement::ClosePath); } } - -/// Create a path that approximates an axis-aligned ellipse. -pub fn ellipse_path(size: Size) -> Path { - // https://stackoverflow.com/a/2007782 - let rx = size.width / 2.0; - let ry = size.height / 2.0; - let m = 0.551784; - let mx = m * rx; - let my = m * ry; - let z = Length::ZERO; - let point = Point::new; - let mut path = Path::new(); - path.move_to(point(-rx, z)); - path.cubic_to(point(-rx, my), point(-mx, ry), point(z, ry)); - path.cubic_to(point(mx, ry), point(rx, my), point(rx, z)); - path.cubic_to(point(rx, -my), point(mx, -ry), point(z, -ry)); - path.cubic_to(point(-mx, -ry), point(-rx, -my), point(z - rx, z)); - path -} |
