Last active
April 10, 2023 11:09
-
-
Save justvanrossum/6fc9172a15a82de8fd40c556f02f20c4 to your computer and use it in GitHub Desktop.
Bezier demo for DrawBot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See this Twitter thread: | |
# https://twitter.com/MauriceMeilleur/status/1488347208709718021 | |
def lerp(v1, v2, t): | |
return v1 + t * (v2 - v1) | |
def lerpPoint(p1, p2, t): | |
return lerp(p1[0], p2[0], t), lerp(p1[1], p2[1], t) | |
def circle(p, r): | |
x, y = p | |
d = r * 2 | |
oval(x - r, y - r, d, d) | |
Variable([dict(name="t", ui="Slider", args=dict(minValue=0, value=0, maxValue=1))], globals()) | |
p1 = (100, 100) | |
p2 = (256, 662) | |
p3 = (686, 620) | |
p4 = (880, 184) | |
m1 = lerpPoint(p1, p2, t) | |
m2 = lerpPoint(p2, p3, t) | |
m3 = lerpPoint(p3, p4, t) | |
mm1 = lerpPoint(m1, m2, t) | |
mm2 = lerpPoint(m2, m3, t) | |
bp = lerpPoint(mm1, mm2, t) | |
fill(None) | |
stroke(0.5) | |
line(p1, p2) | |
line(p2, p3) | |
line(p3, p4) | |
line(m1, m2) | |
line(m2, m3) | |
line(mm1, mm2) | |
stroke(1, 0, 0) | |
newPath() | |
moveTo(p1) | |
curveTo(p2, p3, p4) | |
drawPath() | |
fill(0) | |
stroke(None) | |
for p in [p1, p2, p3, p4]: | |
circle(p, 4) | |
circle(bp, 6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment