mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Paths: bend a straight segment from its thirds (#9225)
A straight segment's handles sit on its anchors, so bending it moved them straight out of the anchors and curled its ends. bend_segment now gives it its handles at its thirds first (the same line) and maps the grab position onto that form, so it bows evenly and the grabbed point still follows the pointer.
This commit is contained in:
1 parent
5b958a3d4f
commit
2047c048b5
1 file changed
+33
@@ -403,11 +403,29 @@ impl SubPath {
|
||||
/// Bend segment `seg` so its point at `t` moves by `dv`: both inner control points move by
|
||||
/// dv / 3t(1-t), since B(t) moves by 3t(1-t) times that. A smooth anchor stays smooth: its other
|
||||
/// handle turns with the moved one. False when there is no such segment.
|
||||
///
|
||||
/// A straight segment's handles lie on its anchors, so moving them as they are curls its ends
|
||||
/// round (#9225). It is bent as the same line with its handles at its thirds instead, so it
|
||||
/// bows evenly, the point at `t` still landing `dv` away.
|
||||
pub fn bend_segment(&mut self, seg: usize, t: f64, dv: kurbo::Vec2) -> bool {
|
||||
let n = self.anchors.len();
|
||||
if seg >= self.segment_count() || n == 0 {
|
||||
return false;
|
||||
}
|
||||
let mut t = t;
|
||||
if self.segment_is_line(seg) {
|
||||
let c = self.segment(seg);
|
||||
let third = (c.p3 - c.p0) / 3.0;
|
||||
if let Some(a) = self.anchors.get_mut(seg % n) {
|
||||
a.set_handle(true, c.p0 + third, false);
|
||||
}
|
||||
if let Some(b) = self.anchors.get_mut((seg + 1) % n) {
|
||||
b.set_handle(false, c.p3 - third, false);
|
||||
}
|
||||
// With its handles on its anchors, the line is at t²(3 - 2t) of the way along at t; with
|
||||
// them at its thirds, that far along is where it is at that same parameter.
|
||||
t = (t * t * (3.0 - 2.0 * t)).clamp(0.05, 0.95);
|
||||
}
|
||||
let v = dv / (3.0 * t * (1.0 - t));
|
||||
if let Some(a) = self.anchors.get_mut(seg % n) {
|
||||
a.set_handle(true, a.h_out + v, false);
|
||||
@@ -910,6 +928,21 @@ fn push_curve(cur: &mut Option<SubPath>, last: Point, c1: Point, c2: Point, p: P
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// #9225: a straight segment bent from its middle bows evenly: its handles leave from along the
|
||||
/// line, toward the middle, not straight out of its anchors, and the grabbed point follows.
|
||||
#[test]
|
||||
fn bending_a_straight_segment_pulls_its_handles_from_along_the_line() {
|
||||
for t in [0.5, 0.3] {
|
||||
let mut sp = SubPath::polyline(&[Point::new(0.0, 0.0), Point::new(300.0, 0.0)], false);
|
||||
let grabbed = sp.segment(0).eval(t);
|
||||
let dv = kurbo::Vec2::new(0.0, 60.0);
|
||||
assert!(sp.bend_segment(0, t, dv));
|
||||
let (a, b) = (sp.anchors[0], sp.anchors[1]);
|
||||
assert!(a.h_out.x > a.p.x + 1.0 && b.h_in.x < b.p.x - 1.0, "t {t}: {a:?} {b:?}");
|
||||
let landed = sp.segment(0).nearest(grabbed + dv, 1e-9).distance_sq.sqrt();
|
||||
assert!(landed < 1e-6, "t {t}: the grabbed point is {landed} off the pointer");
|
||||
}
|
||||
}
|
||||
use super::*;
|
||||
|
||||
/// `bounds` walks the anchors in place: bit for bit what the kurbo path it stands for gives,
|
||||
|
||||
Reference in new issue
Block a user