Skip to content

Commit

Permalink
Add Line::flipped, Line::midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
waywardmonkeys committed Sep 1, 2024
1 parent 87290c7 commit 0c30ddf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This release has an [MSRV][] of 1.65.
- Add `Arc::flipped`. ([#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::flipped` and `Line::midpoint`. ([#375] by [@waywardmonkeys])

### Changed

Expand Down Expand Up @@ -61,6 +62,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
38 changes: 38 additions & 0 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,32 @@ impl Line {
}
}

/// Return a copy of this `Line` in the opposite direction.
#[must_use]
#[inline]
pub fn flipped(&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 +322,18 @@ impl Iterator for LinePathIter {
mod tests {
use crate::{Line, ParamCurveArclen, Point};

#[test]
fn line_flipped() {
let l = Line::new((0.0, 0.0), (1.0, 1.0));
let f = l.flipped();

assert_eq!(l.p0, f.p1);
assert_eq!(l.p1, f.p0);

// Flipping it again should result in the original line
assert_eq!(l, f.flipped());
}

#[test]
fn line_arclen() {
let l = Line::new((0.0, 0.0), (1.0, 1.0));
Expand All @@ -313,6 +345,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 0c30ddf

Please sign in to comment.