# Bezier curve from python to julia

**URL:** <https://discourse.julialang.org/t/bezier-curve-from-python-to-julia/22630>\
**Category:** General Usage\
**Created:** [April 2, 2019, 8:37am UTC](https://discourse.julialang.org/t/bezier-curve-from-python-to-julia/22630 "2019-04-02T08:37:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)\
**Post date:** [April 2, 2019, 8:37am UTC](https://discourse.julialang.org/t/bezier-curve-from-python-to-julia/22630/1 "2019-04-02T08:37:35Z")

</div>

I have this bezier curve diagram I created in Python but i need to redo it in Julia. I looked up alot of articles but still couldn’t learn how to do it. Please help by commenting on how to do it. It can be a part of this code or all of it.I know once I see the syntax…it will be easy for me to do on my own. I have attached the image i got when i run this code in Python. it can be a little different in Julia, am still okay with it.

```julia
import numpy as np
import matplotlib.pyplot as plt
import scipy.special 
from scipy import comb
from scipy.stats import binom
from matplotlib.colors import hsv_to_rgb
import sys
import numpy as np

k = np.array([3, 4]);k
array([120., 210.])
comb(10, 3, exact=True)
comb(10, 3, exact=True, repetition=True)

# Save combinations into array
combs = []

for n in range(10):
    ar = []
    for k in range(n + 1):
        ar.append(comb(n, k))
    combs.append(ar)

def combinations(n, k):
    if n < k: return .0
    else:
        return combs[n][k]

# Done
def bernstein_matrix_2(t, w):
    return np.matmul(np.matmul([1, t **1, t** 2], [[1, 0, 0], [-2, 2, 0], [1, -2, 1]]), w)

def bernstein_matrix_3(t, w):
    return np.matmul(np.matmul([1, t, t **2, t** 3], [[1, 0, 0, 0], [-3, 3, 0, 0], [3, -6, 3, 0], [-1, 3, -3, 1]]), w)

# Done
def bernstein_2(t, w):
    return np.multiply((1-t)**2, w[0]) + np.multiply(2*t*(1-t), w[1]) + np.multiply(t**2 , w[2])

# Basis for Bernstein
def bbasis(t, n, i):
    return combinations(n, i) * (1 - t)**(n - i) * t**(i)

# Done
def bernstein(t, w):
    w = np.array(w).astype('float')
    ret = np.array([0,0])
    for i in range(len(w)):
        ret = ret + w[i] * bbasis(t, len(w)-1, i)
    return ret

# Derivaive for 2d vector
def deriv2(t, w):
    return (np.array(w[1]) - np.array(w[0])) * 2 * (1 - t) + (np.array(w[2] - np.array(w[1]))) * 2 * t

# Derivaive for Nd vector
def deriv(t, w):
    n = len(w) - 1
    ret = np.array([0, 0])
    
    for i in range(n):
        ret = ret + (np.array(w[i+1]) - np.array(w[i])) * bbasis(t, n-1, i) * (n)
    return ret

# De Casteljue
# rerurn all control points
def de_cast_all(t, w):
    ret = []
    n = len(w)
    for k in range(0, n):
        ar = []
        for i in range(n - k):
            ar.append(r(i, k, t, w))
        ret.append(ar)
    return ret
    
# De Casteljue
# rerurn point on Bezier curve
def de_cast(t, w):
    return r(0, len(w)-1, t, w)

# De Casteljue
# recursive stuff
def r(i, k, t, w):
    if k == 0: return np.array(w[i])
    
    return (1 - t) * r(i, k-1, t, w) + t * r(i+1, k-1, t, w)

w_points = [[0, 0], [2, 3], [3, 0]]

bezier_matrix_points = []
bezier_bernstein_points = []
bernstein_points = []

for t in np.linspace(0, 1, 100):
    bezier_matrix_points.append(bernstein_matrix_2(t, w_points))
    bezier_bernstein_points.append(bernstein_2(t, w_points))
    bernstein_points.append(bernstein(t, w_points))

fig = plt.figure(figsize=(18,18))
axs = [fig.add_subplot(1, 3, i) for i in range(1, 4)]

for ax in axs:
    ax.grid()
    ax.set_aspect('equal')
    
axs[0].set_title('Matrix 2')
axs[1].set_title('Bernstein 2')
axs[2].set_title('Bernstein N')

axs[0].plot([x[0] for x in bezier_matrix_points], [x[1] for x in bezier_matrix_points], 
            linewidth=4, color='r', label='Bezier curve')
axs[0].plot([x[0] for x in w_points], [x[1] for x in w_points], 
            marker='o', color='black', label='Control points')
axs[0].legend(fontsize=13, framealpha=1)

axs[1].plot([x[0] for x in bezier_bernstein_points], [x[1] for x in bezier_bernstein_points], 
            linewidth=4, color='g', label='Bezier curve')
axs[1].plot([x[0] for x in w_points], [x[1] for x in w_points], 
            marker='o', color='black', label='Control points')
axs[1].legend(fontsize=13, framealpha=1)

axs[2].plot([x[0] for x in bernstein_points], [x[1] for x in bernstein_points], 
            linewidth=4, color='b', label='Bezier curve')
axs[2].plot([x[0] for x in w_points], [x[1] for x in w_points], 
            marker='o', color='black', label='Control points')
axs[2].legend(fontsize=13, framealpha=1)

```

 ![1](https://global.discourse-cdn.com/julialang/original/3X/c/5/c5ed4a36de68a9bd31c57c81052778ebf69ca8f9.png) ![2](https://global.discourse-cdn.com/julialang/original/3X/9/f/9fa46dc15946f9baa0cc2a3de2e52296f624ebd5.png)

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 2, 2019, 8:40am UTC](https://discourse.julialang.org/t/bezier-curve-from-python-to-julia/22630/2 "2019-04-02T08:40:34Z")

</div>

Have you seen [https://cormullion.github.io/blog/2018/06/21/bezier.html](https://cormullion.github.io/blog/2018/06/21/bezier.html) and its code repo [GitHub - cormullion/cormullion.github.io: a JuliaLang blog made with Franklin.jl](https://github.com/cormullion/cormullion.github.io)?

---

<div class="post-metadata">

**Author:** ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)\
**Post date:** [April 2, 2019, 1:01pm UTC](https://discourse.julialang.org/t/bezier-curve-from-python-to-julia/22630/3 "2019-04-02T13:01:42Z")

</div>

They are both so easy and I dont even need to define my own fuctions. Thankyou so much.
