Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Arc::flipped #367

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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])

### Changed

Expand Down Expand Up @@ -55,6 +56,7 @@ Note: A changelog was not kept for or before this release
[#354]: https://github.com/linebender/kurbo/pull/354
[#356]: https://github.com/linebender/kurbo/pull/356
[#361]: https://github.com/linebender/kurbo/pull/361
[#367]: https://github.com/linebender/kurbo/pull/367

[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
37 changes: 37 additions & 0 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ impl Arc {
}
}

/// Return 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 {
Self {
center: self.center,
radii: self.radii,
start_angle: self.start_angle + self.sweep_angle,
sweep_angle: -self.sweep_angle,
x_rotation: self.x_rotation,
}
}

/// Create an iterator generating Bezier path elements.
///
/// The generated elements can be appended to an existing bezier path.
Expand Down Expand Up @@ -206,3 +222,24 @@ impl Mul<Arc> for Affine {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flipped_arc() {
let a = Arc::new((0., 0.), (1., 0.), 0., PI, 0.);
let f = a.flipped();

// Most fields should be unchanged:
assert_eq!(a.center, f.center);
assert_eq!(a.radii, f.radii);
assert_eq!(a.x_rotation, f.x_rotation);

// 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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No fuzzy testing needed, since there is only addition/subtraction meaning perfect accuracy, at least for normal input.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are confirming what I have here is fine, not suggesting a change, right?

}
}