Skip to content

Commit

Permalink
Add Line::reversed, Line::midpoint, rename Arc::flipped (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
waywardmonkeys authored Sep 4, 2024
1 parent f753199 commit 2cb3ec6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -227,9 +227,9 @@ impl Mul<Arc> 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);
Expand All @@ -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());
}
}
4 changes: 2 additions & 2 deletions src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point> {
let ab = self.p1 - self.p0;
Expand Down Expand Up @@ -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));
Expand All @@ -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 {
Expand Down

0 comments on commit 2cb3ec6

Please sign in to comment.