Skip to content

Instantly share code, notes, and snippets.

@simoncozens
Last active October 23, 2023 06:42
Show Gist options
  • Save simoncozens/3c5d304ae2c14894393c6284df91be5b to your computer and use it in GitHub Desktop.
Save simoncozens/3c5d304ae2c14894393c6284df91be5b to your computer and use it in GitHub Desktop.
Harmonizing two Bezier curves

To harmonize (with G2 curvature) two cubic Bézier curves a0,a1,a2,a3 and b0,b1,b2,b3 where a2, a3 = b0, and b1 are colinear:

  • First find d = intersection point of line a1--a2 and line b1--b2.
  • Now find ratios p0 = |a1, a2| / |a2, d| and p1 = |d1, b1| / |b1, b2|.
  • Determine ratio p = sqrt(p0 * p1)
  • Now set position of a3 = b0 such that |a2, a3| / |a3, b1| == p.
  • To do this, set t = p / (p+1).
  • Adjust the position of a3=b0 so that it sits t of the way between a2 and b1.

Of course, you may prefer to keep the position of a3 because it's the on-curve point. Fine. Instead, compute where a3 should go according to this algorithm, work out the delta between the new position and the current position, and apply that delta to the handles a2 and b1 instead.

@mekkablue
Copy link

Note: used in Glyphs.app plug-in Green Harmony and in Alex Slobzheninov’s script G2 Harmonize (install either via Window > Plugin Manager).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment