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

Impl ParamCurve, ParamCurveArclen for Arc #378

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ You can find its changes [documented below](#0111-2024-09-12).

This release has an [MSRV][] of 1.65.

### Changes

- `Arc` now implements `ParamCurve` and `ParamCurveArclen`. ([#378] by [@waywardmonkeys])

## [0.11.1][] (2024-09-12)

This release has an [MSRV][] of 1.65.
Expand Down Expand Up @@ -75,6 +79,7 @@ Note: A changelog was not kept for or before this release
[#370]: https://github.com/linebender/kurbo/pull/370
[#375]: https://github.com/linebender/kurbo/pull/375
[#376]: https://github.com/linebender/kurbo/pull/376
[#378]: https://github.com/linebender/kurbo/pull/378

[Unreleased]: https://github.com/linebender/kurbo/compare/v0.11.1...HEAD
[0.11.0]: https://github.com/linebender/kurbo/releases/tag/v0.11.0
Expand Down
40 changes: 38 additions & 2 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

//! An ellipse arc.

use crate::{Affine, Ellipse, PathEl, Point, Rect, Shape, Vec2};
use crate::{Affine, Ellipse, ParamCurve, ParamCurveArclen, PathEl, Point, Rect, Shape, Vec2};
use core::{
f64::consts::{FRAC_PI_2, PI},
iter,
ops::Mul,
ops::{Mul, Range},
};

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -171,6 +171,42 @@ fn rotate_pt(pt: Vec2, angle: f64) -> Vec2 {
)
}

impl ParamCurve for Arc {
fn eval(&self, t: f64) -> Point {
let angle = self.start_angle + (self.sweep_angle * t);
sample_ellipse(self.radii, self.x_rotation, angle).to_point()
}

fn subsegment(&self, range: Range<f64>) -> Self {
Self {
center: self.center,
radii: self.radii,
start_angle: self.start_angle + (self.sweep_angle * range.start),
sweep_angle: self.sweep_angle - (self.sweep_angle * (1.0 - range.end)),
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels wrong, though I haven't done tests to validate it. My intuition says it should be self.sweep_angle * (range.end - range.start). In any case, the code that's there reduces algebraically to self.sweep_angle * range.end. It is of course possible I'm missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think what I have is wrong. I'll get back into this and see.

x_rotation: self.x_rotation,
}
}

fn start(&self) -> Point {
sample_ellipse(self.radii, self.x_rotation, self.start_angle).to_point()
}

fn end(&self) -> Point {
sample_ellipse(
self.radii,
self.x_rotation,
self.start_angle + self.sweep_angle,
)
.to_point()
}
}

impl ParamCurveArclen for Arc {
fn arclen(&self, accuracy: f64) -> f64 {
self.path_segments(0.1).perimeter(accuracy)
Copy link
Contributor

Choose a reason for hiding this comment

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

The 0.1 is arbitrary here - I think accuracy for both is reasonable, though of course the "right" way to do this is careful numeric analysis.

It is true that arc length of an ellipse is tricky, I believe it involves the incomplete elliptic integral of the second kind. It might also make sense to do Gauss-Legendre integration of the norm of first derivative, which is pretty simple and is likely more "bang for the buck" than going to Bézier.

I'm also wondering whether it might make sense to special case the circular case, as I think it's pretty common and also the math is much easier (especially for inverse arc length). But I'm not going to insist on that, as prefer prioritizing making the general case good rather than having a bunch of special cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was using the same math being used for the perimeter function ... I do think we should improve upon this and also either have this call the perimeter function or have that one call this one.

Copy link
Member

Choose a reason for hiding this comment

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

I'm proposing a numerical approximation in #381 (on top of @waywardmonkeys's PR). It needs some more work as the error bounds are not yet as I would've expected, but you can take a look already.

}
}

impl Shape for Arc {
type PathElementsIter<'iter> = iter::Chain<iter::Once<PathEl>, ArcAppendIter>;

Expand Down