%matplotlib inline
from matplotlib import pyplot as plt
class Bezier():
def __init__(self, vertices):
self.vertices = vertices
def interpolate(self, t):
new_vert = []
vertices = self.vertices
for axis in range(len(vertices[0])):
coord, alpha, beta, coeff = 0, len(vertices) - 1, 0, 1
for vertex in vertices:
if alpha < len(vertices) - 1:
coeff = (alpha + 1)*coeff/beta
coord += vertex[axis]*((1 - t)**alpha)*(t**beta)*coeff
alpha -= 1
beta += 1
new_vert.append(coord)
return new_vert
def draw(self, res):
output = []
for i in range(res):
t = i/(res - 1)
output.append(self.interpolate(t))
return output
b = Bezier([[0, 0], [2.0, 0.0], [-1.0, 3], [2, 2]])
plt.scatter([x for x,_ in b.draw(20)], [y for _,y in b.draw(20)])
b.interpolate(0.25)