Skip to content

Instantly share code, notes, and snippets.

View verbosus's full-sized avatar

Antonio Cavedoni verbosus

View GitHub Profile
@typoman
typoman / bezier-path-find-closest-point.py
Last active October 28, 2024 19:11
Finds closest point on a bezier path using Newton–Raphson method and visualizes it in drawbot app
import math
def bezier_curve(p0, p1, p2, p3, t):
x = (1-t)**3 * p0[0] + 3*(1-t)**2 * t * p1[0] + 3*(1-t) * t**2 * p2[0] + t**3 * p3[0]
y = (1-t)**3 * p0[1] + 3*(1-t)**2 * t * p1[1] + 3*(1-t) * t**2 * p2[1] + t**3 * p3[1]
return (x, y)
def bezier_curve_derivative(p0, p1, p2, p3, t):
x = 3*(1-t)**2 * (p1[0] - p0[0]) + 6*(1-t) * t * (p2[0] - p1[0]) + 3*t**2 * (p3[0] - p2[0])
y = 3*(1-t)**2 * (p1[1] - p0[1]) + 6*(1-t) * t * (p2[1] - p1[1]) + 3*t**2 * (p3[1] - p2[1])
@jmsole
jmsole / typeproof.py
Last active March 30, 2023 22:27
Type proof generator script for DrawBot. It uses the `wordsiv` library to generate words when the character is not complete. The over script is based on the `simpleproof` script by DJR.
import unicodedata
import datetime
import os
import string
from wordsiv import WordSiv
from itertools import product
import en_wordcount_web
from fontTools.ttLib import TTFont
from fontTools.agl import toUnicode
from fontTools.pens.boundsPen import BoundsPen
@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.