Skip to content

Instantly share code, notes, and snippets.

View mekkablue's full-sized avatar

Rainer Erich Scheichelbauer mekkablue

View GitHub Profile
@simoncozens
simoncozens / harmonization.md
Last active October 23, 2023 06:42
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
mekkablue / Add Hints.py
Created May 2, 2014 07:06
Adds hints for the current node selection in Glyphs.app
#MenuTitle: Add Hints
# -*- coding: utf-8 -*-
"""Add Hints for the Selected Nodes. Tries to guess whether it should be H or V. If exactly one node inside a zone is selected, it will add a Ghost Hint."""
import GlyphsApp
Font = Glyphs.font
FontMaster = Font.selectedFontMaster
thisLayer = Font.selectedLayers[0]
thisSelection = [ n for n in thisLayer.selection() if n.className() == "GSNode" ]
def intersect( x1, y1, x2, y2, x3, y3, x4, y4 ):
"""Calculates the intersection of line P1-P2 with P3-P4."""
slope12 = ( float(y2) - float(y1) ) / ( float(x2) - float(x1) )
slope34 = ( float(y4) - float(y3) ) / ( float(x4) - float(x3) )
x = ( slope12 * x1 - y1 - slope34 * x3 + y3 ) / ( slope12 - slope34 )
y = slope12 * ( x - x1 ) + y1
return x, y