From 2cb3ec6b36427037cd5ada74d34bb56b20eede10 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Wed, 4 Sep 2024 22:00:47 +0700 Subject: [PATCH] Add `Line::reversed`, `Line::midpoint`, rename `Arc::flipped` (#375) --- CHANGELOG.md | 4 +++- src/arc.rs | 12 ++++++------ src/circle.rs | 4 ++-- src/line.rs | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1e60845..900a16d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,9 +20,10 @@ This release has an [MSRV][] of 1.65. - Add `From (f32, f32)` for `Point`. ([#339] by [@rsheeter]) - Add `Rect::overlaps` and `Rect::contains_rect`. ([#347] by [@nils-mathieu]) - Add `CubicBez::tangents` ([#288] by [@raphlinus]) -- Add `Arc::flipped`. ([#367] by [@waywardmonkeys]) +- Add `Arc::reversed`. ([#367] by [@waywardmonkeys]) - Add `CircleSegment::inner_arc` and `CircleSegment::outer_arc` ([#368] by [@waywardmonkeys]) - Add `Rect::is_zero_area` and `Size::is_zero_area` and deprecate their `is_empty` methods. ([#370] by [@waywardmonkeys]) +- Add `Line::reversed` and `Line::midpoint`. ([#375] by [@waywardmonkeys]) ### Changed @@ -67,6 +68,7 @@ Note: A changelog was not kept for or before this release [#367]: https://github.com/linebender/kurbo/pull/367 [#368]: https://github.com/linebender/kurbo/pull/368 [#370]: https://github.com/linebender/kurbo/pull/370 +[#375]: https://github.com/linebender/kurbo/pull/375 [Unreleased]: https://github.com/linebender/kurbo/compare/v0.11.0...HEAD [0.11.0]: https://github.com/linebender/kurbo/releases/tag/v0.11.0 diff --git a/src/arc.rs b/src/arc.rs index fd903c32..9ac642be 100644 --- a/src/arc.rs +++ b/src/arc.rs @@ -49,13 +49,13 @@ impl Arc { } } - /// Return a copy of this `Arc` in the opposite direction. + /// Returns a copy of this `Arc` in the opposite direction. /// /// The new `Arc` will sweep towards the original `Arc`s /// start angle. #[must_use] #[inline] - pub fn flipped(&self) -> Arc { + pub fn reversed(&self) -> Arc { Self { center: self.center, radii: self.radii, @@ -227,9 +227,9 @@ impl Mul for Affine { mod tests { use super::*; #[test] - fn flipped_arc() { + fn reversed_arc() { let a = Arc::new((0., 0.), (1., 0.), 0., PI, 0.); - let f = a.flipped(); + let f = a.reversed(); // Most fields should be unchanged: assert_eq!(a.center, f.center); @@ -239,7 +239,7 @@ mod tests { // Sweep angle should be in reverse assert_eq!(a.sweep_angle, -f.sweep_angle); - // Flipping it again should result in the original arc - assert_eq!(a, f.flipped()); + // Reversing it again should result in the original arc + assert_eq!(a, f.reversed()); } } diff --git a/src/circle.rs b/src/circle.rs index c340a42b..969bd619 100644 --- a/src/circle.rs +++ b/src/circle.rs @@ -243,11 +243,11 @@ impl CircleSegment { /// Return an arc representing the inner radius. /// - /// This is [flipped] from the outer arc, so that it is in the + /// This is [reversed] from the outer arc, so that it is in the /// same direction as the arc that would be drawn (as the path /// elements for this circle segment produce a closed path). /// - /// [flipped]: Arc::flipped + /// [reversed]: Arc::reversed #[must_use] #[inline] pub fn inner_arc(&self) -> Arc { diff --git a/src/line.rs b/src/line.rs index edd4aeac..b7a8c586 100644 --- a/src/line.rs +++ b/src/line.rs @@ -34,12 +34,33 @@ impl Line { } } + /// Returns a copy of this `Line` with the end points swapped so that it + /// points in the opposite direction. + #[must_use] + #[inline] + pub fn reversed(&self) -> Line { + Self { + p0: self.p1, + p1: self.p0, + } + } + /// The length of the line. #[inline] pub fn length(self) -> f64 { self.arclen(DEFAULT_ACCURACY) } + /// The midpoint of the line. + /// + /// This is the same as calling [`Point::midpoint`] with + /// the endpoints of this line. + #[must_use] + #[inline] + pub fn midpoint(&self) -> Point { + self.p0.midpoint(self.p1) + } + /// Computes the point where two lines, if extended to infinity, would cross. pub fn crossing_point(self, other: Line) -> Option { let ab = self.p1 - self.p0; @@ -302,6 +323,18 @@ impl Iterator for LinePathIter { mod tests { use crate::{Line, ParamCurveArclen, Point}; + #[test] + fn line_reversed() { + let l = Line::new((0.0, 0.0), (1.0, 1.0)); + let f = l.reversed(); + + assert_eq!(l.p0, f.p1); + assert_eq!(l.p1, f.p0); + + // Reversing it again should result in the original line + assert_eq!(l, f.reversed()); + } + #[test] fn line_arclen() { let l = Line::new((0.0, 0.0), (1.0, 1.0)); @@ -313,6 +346,12 @@ mod tests { assert!((t - 1.0 / 3.0).abs() < epsilon); } + #[test] + fn line_midpoint() { + let l = Line::new((0.0, 0.0), (2.0, 4.0)); + assert_eq!(l.midpoint(), Point::new(1.0, 2.0)); + } + #[test] fn line_is_finite() { assert!((Line {