from numpy import linspace as lin  # linespace
from scipy.special import comb  # binomial coefficient
import matplotlib.pyplot as plt  # plotting

cpx = [0.0, 0.5, 2.0, 1.5] # control point x coordinates
cpy = [0.0, 3.0, 0.0, 3.0] # control point y coordinates

t = lin(0, 1, 100)  # 100 sample inputs between 0 and 1
num = 3  # degree 0 indexed

# returns the value from the Bernstein basis function at the point x
def bern(i, n, t):
    return comb(n, i) * t ** i * (1 - t) ** (n-i)

# Generalized De Casteljau's Explicit formula 
def f(a, t):
    ret = 0
    for i in range(num + 1):
        ret += a[i] * bern(i, num, t)
    return ret

def x(t): return f(cpx, t) # implemented on X's control points
def y(t): return f(cpy, t) # implemented on Y's control points

plt.plot([x(i) for i in t], [y(i) for i in t]) # plot bezier curve
plt.plot(cpx, cpy, 'ro') # plot control points
plt.plot(cpx, cpy, 'r:') # graph lines between control points
for i in range(num+1):
    # label control points
    plt.text(cpx[i]+0.05, cpy[i]+0.05, 'P{}'.format(i))
plt.title('Bezier') # title
plt.show() # display graphs
8e08c57c4b7f138bc3f558a11d8b8a6c277f264c
<mask token>


def bern(i, n, t):
    return comb(n, i) * t ** i * (1 - t) ** (n - i)


def f(a, t):
    ret = 0
    for i in range(num + 1):
        ret += a[i] * bern(i, num, t)
    return ret


<mask token>


def y(t):
    return f(cpy, t)


<mask token>

<mask token>


def bern(i, n, t):
    return comb(n, i) * t ** i * (1 - t) ** (n - i)


def f(a, t):
    ret = 0
    for i in range(num + 1):
        ret += a[i] * bern(i, num, t)
    return ret


def x(t):
    return f(cpx, t)


def y(t):
    return f(cpy, t)


plt.plot([x(i) for i in t], [y(i) for i in t])
plt.plot(cpx, cpy, 'ro')
plt.plot(cpx, cpy, 'r:')
for i in range(num + 1):
    plt.text(cpx[i] + 0.05, cpy[i] + 0.05, 'P{}'.format(i))
plt.title('Bezier')
plt.show()

<mask token>
cpx = [0.0, 0.5, 2.0, 1.5]
cpy = [0.0, 3.0, 0.0, 3.0]
t = lin(0, 1, 100)
num = 3


def bern(i, n, t):
    return comb(n, i) * t ** i * (1 - t) ** (n - i)


def f(a, t):
    ret = 0
    for i in range(num + 1):
        ret += a[i] * bern(i, num, t)
    return ret


def x(t):
    return f(cpx, t)


def y(t):
    return f(cpy, t)


plt.plot([x(i) for i in t], [y(i) for i in t])
plt.plot(cpx, cpy, 'ro')
plt.plot(cpx, cpy, 'r:')
for i in range(num + 1):
    plt.text(cpx[i] + 0.05, cpy[i] + 0.05, 'P{}'.format(i))
plt.title('Bezier')
plt.show()

from numpy import linspace as lin
from scipy.special import comb
import matplotlib.pyplot as plt
cpx = [0.0, 0.5, 2.0, 1.5]
cpy = [0.0, 3.0, 0.0, 3.0]
t = lin(0, 1, 100)
num = 3


def bern(i, n, t):
    return comb(n, i) * t ** i * (1 - t) ** (n - i)


def f(a, t):
    ret = 0
    for i in range(num + 1):
        ret += a[i] * bern(i, num, t)
    return ret


def x(t):
    return f(cpx, t)


def y(t):
    return f(cpy, t)


plt.plot([x(i) for i in t], [y(i) for i in t])
plt.plot(cpx, cpy, 'ro')
plt.plot(cpx, cpy, 'r:')
for i in range(num + 1):
    plt.text(cpx[i] + 0.05, cpy[i] + 0.05, 'P{}'.format(i))
plt.title('Bezier')
plt.show()

from numpy import linspace as lin  # linespace
from scipy.special import comb  # binomial coefficient
import matplotlib.pyplot as plt  # plotting

cpx = [0.0, 0.5, 2.0, 1.5] # control point x coordinates
cpy = [0.0, 3.0, 0.0, 3.0] # control point y coordinates

t = lin(0, 1, 100)  # 100 sample inputs between 0 and 1
num = 3  # degree 0 indexed

# returns the value from the Bernstein basis function at the point x
def bern(i, n, t):
    return comb(n, i) * t ** i * (1 - t) ** (n-i)

# Generalized De Casteljau's Explicit formula 
def f(a, t):
    ret = 0
    for i in range(num + 1):
        ret += a[i] * bern(i, num, t)
    return ret

def x(t): return f(cpx, t) # implemented on X's control points
def y(t): return f(cpy, t) # implemented on Y's control points

plt.plot([x(i) for i in t], [y(i) for i in t]) # plot bezier curve
plt.plot(cpx, cpy, 'ro') # plot control points
plt.plot(cpx, cpy, 'r:') # graph lines between control points
for i in range(num+1):
    # label control points
    plt.text(cpx[i]+0.05, cpy[i]+0.05, 'P{}'.format(i))
plt.title('Bezier') # title
plt.show() # display graphs