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 tangents_to_point for QuadBez #417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 22 additions & 1 deletion src/quadbez.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::ops::{Mul, Range};

use arrayvec::ArrayVec;

use crate::common::solve_cubic;
use crate::common::{solve_cubic, solve_quadratic};
use crate::MAX_EXTREMA;
use crate::{
Affine, CubicBez, Line, Nearest, ParamCurve, ParamCurveArclen, ParamCurveArea,
Expand Down Expand Up @@ -110,6 +110,27 @@ impl QuadBez {
pub fn is_nan(&self) -> bool {
self.p0.is_nan() || self.p1.is_nan() || self.p2.is_nan()
}

/// Find points on the curve where the tangent line passes through the
/// given point.
///
/// Result is array of t values such that the tangent line from the curve
/// evaluated at that point goes through the argument point.
pub fn tangents_to_point(&self, p: Point) -> ArrayVec<f64, 2> {
let a = self.p0.to_vec2() - 2.0 * self.p1.to_vec2() + self.p2.to_vec2();
let b = 2.0 * (self.p1.to_vec2() - self.p0.to_vec2());
let c = self.p0.to_vec2() - p.to_vec2();

// coefficients of x(t) \cross x'(t)
let c2 = a.cross(b);
let c1 = -2.0 * c.cross(a);
let c0 = b.cross(c);
Comment on lines +120 to +127
Copy link
Author

Choose a reason for hiding this comment

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

I'd like a double check on the math here, both in terms of its correctness (I'm not certain of it) and its formatting (is it more faithful to the canonical Bézier equations to reverse the orders of the terms, swap the order of the cross product and negate it, etc.?). This mostly arose from pattern recognition based fiddling and I wasn't able to quite grasp its relationship with the Bézier equations on Wikipedia. So please confirm this is right.

solve_quadratic(c0, c1, c2)
.iter()
.copied()
.filter(|t| *t >= 0.0 && *t <= 1.0)
.collect()
}
}

/// An iterator for quadratic beziers.
Expand Down
Loading