import numpy as np
from scipy.interpolate import splev


def bspline(cv, degree=3, periodic=False):
    """ return a function that defines a bezier spline
        cv :      an array of control points
        degree:   degree of the polynominal curve
        periodic: True - closed curve
                  False - open curve
        adopted from http://stackoverflow.com/a/35007804
    """
    # If periodic, extend the point array by count+degree+1
    cv = np.array(cv)
    count = cv.shape[0]
    if periodic:
        factor, fraction = divmod(count+degree+1, count)
        cv = np.concatenate((cv,) * factor + (cv[:fraction],))
        count = len(cv)
        degree = np.clip(degree,1,degree)
    # If opened, prevent degree from exceeding count-1
    else:
        degree = np.clip(degree,1,count-1)
    # Calculate knot vector
    if periodic:
        kv = np.arange(0-degree,count+degree+degree-1,dtype='int')
    else:
        kv = np.array([0]*degree + list(range(count-degree+1)) + [count-degree]*degree,dtype='int')
    def spl(u, normalize=True):
        """normalize： normalize the parameter u so, u=1 at the end of the curve"""
        # Calculate result
        u = np.array(u)
        if normalize:
            if periodic:
                u *= count - degree - 1
            else:
                u *= count - degree
        points = np.zeros((len(u), cv.shape[1]))
        for i in range(cv.shape[1]):
            points[:,i] = splev(u, (kv,cv[:,i],degree))
        return points
    return spl

if __name__ == '__main__':
    import matplotlib.pyplot as plt
    n_cpoints = 4
    sep = 1 / (n_cpoints - 2)
    cv = np.zeros((n_cpoints, 2))
    for i in range(1, n_cpoints-1):
        cv[i,0] = np.random.random() * sep + sep * (i-1)
    cv[-1,0] = 1
    cv[1:,1] = np.random.random(cv.shape[0]-1) * 2 -1


    plt.plot(cv[:, 0], cv[:, 1], 'o', label='Control Points')

    us = np.linspace(0, 1, 100)
    for d in range(1, 4):
        res =  bspline(cv, degree=d, periodic=False)(us)
        x = res[:,0]
        y = res[:,1]
        plt.plot(x, y, '-', label='Degree %s' % d)

    plt.minorticks_on()
    plt.legend()
    plt.xlabel('x')
    plt.ylabel('y')
    plt.xlim(-0.1, 1.1)
    plt.ylim(-1, 1)
    plt.gca().set_aspect('equal', adjustable='box')
    plt.show()
@@ -1,12 +1,35 @@
+#!/usr/bin/env python%0D%0A
 import numpy
@@ -584,24 +584,28 @@
 od(count
-+
+ + 
 degree
-+
+ + 
 1, count
@@ -728,18 +728,20 @@
 (degree,
+ 
 1,
+ 
 degree)%0D
@@ -844,16 +844,20 @@
 ree,
+ 
 1,
+ 
 count
--
+ - 
 1)%0D%0A
@@ -931,38 +931,48 @@
 ge(0
--
+ - 
 degree,
+ 
 count
-+
+ + 
 degree
-+
+ + 
 degree
--
+ - 
 1,
+ 
 dtyp
@@ -1017,17 +1017,19 @@
 rray(%5B0%5D
-*
+ * 
 degree +
@@ -1049,16 +1049,20 @@
 ount
--
+ - 
 degree
-+
+ + 
 1)) 
@@ -1073,24 +1073,29 @@
 ount
--
+ - 
 degree%5D
-*
+ * 
 degree,
+ 
 dtyp
@@ -1100,24 +1100,26 @@
 ype='int')%0D%0A
+%0D%0A
     def spl(
@@ -1531,16 +1531,17 @@
 oints%5B:,
+ 
 i%5D = spl
@@ -1554,16 +1554,19 @@
 (kv,
+ 
 cv%5B:,
+ 
 i%5D,
+ 
 degr
@@ -1590,24 +1590,26 @@
 urn points%0D%0A
+%0D%0A
     return s
@@ -1614,16 +1614,18 @@
  spl%0D%0A%0D%0A
+%0D%0A
 if __nam
@@ -1677,24 +1677,26 @@
 lot as plt%0D%0A
+%0D%0A
     n_cpoint
@@ -1799,17 +1799,19 @@
 _cpoints
--
+ - 
 1):%0D%0A   
@@ -1820,16 +1820,17 @@
    cv%5Bi,
+ 
 0%5D = np.
@@ -1861,17 +1861,19 @@
 sep * (i
--
+ - 
 1)%0D%0A    
@@ -1878,16 +1878,17 @@
   cv%5B-1,
+ 
 0%5D = 1%0D%0A
@@ -1897,16 +1897,17 @@
   cv%5B1:,
+ 
 1%5D = np.
@@ -1935,17 +1935,19 @@
 e%5B0%5D
--
+ - 
 1) * 2 -
 1%0D%0A%0D
@@ -1942,19 +1942,18 @@
 1) * 2 -
+ 
 1
-%0D%0A
 %0D%0A%0D%0A    
@@ -2086,17 +2086,16 @@
    res =
- 
  bspline
@@ -2148,16 +2148,17 @@
 = res%5B:,
+ 
 0%5D%0D%0A    
@@ -2171,16 +2171,17 @@
 = res%5B:,
+ 
 1%5D%0D%0A    
@@ -2428,8 +2428,10 @@
 t.show()
+%0D%0A

Fix utilities