Skip to content

Instantly share code, notes, and snippets.

@gferreira
Last active April 18, 2019 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gferreira/9618824 to your computer and use it in GitHub Desktop.
Save gferreira/9618824 to your computer and use it in GitHub Desktop.
find a point in a Bezier segment
'''find a point in a Bezier segment'''
# formulas converted from:
# http://13thparallel.com/archive/bezier-curves/
# run this script in DrawBot: http://drawbot.com/
def B1(t): return t*t*t
def B2(t): return 3*t*t*(1-t)
def B3(t): return 3*t*(1-t)*(1-t)
def B4(t): return (1-t)*(1-t)*(1-t)
def getPoint(t, p1, p2, p3, p4):
x1, y1 = p1
x2, y2 = p2
x3, y3 = p3
x4, y4 = p4
x = x1*B1(t) + x2*B2(t) + x3*B3(t) + x4*B4(t)
y = y1*B1(t) + y2*B2(t) + y3*B3(t) + y4*B4(t)
return x, y
size(200, 200)
fill(None)
stroke(1, 0, 0)
strokeWidth(3)
B = BezierPath()
B.moveTo((38, 36))
B.curveTo((-18, 160), (168, 190), (150, 56))
drawPath(B)
p1, p4 = B.onCurvePoints
p2, p3 = B.offCurvePoints
Variable([dict(name='t', ui='Slider',
args=dict(value=0.5, minValue=0.0, maxValue=1.0))],
globals())
x, y = getPoint(t, p1, p2, p3, p4)
r = 10
oval(x-r, y-r, r*2, r*2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment