<p>In line 15 use the half-open interval, i.e.,</p>

<pre><code>if u&gt;=t[0][i] and u&lt;t[0][i+1]:
</code></pre>

<p>Otherwise, at knot values, you evaluate two basis functions at the k=1 basis when you only want one. This causes the wrong evaluation of the basis function at the knot values and therefore the spikes.  </p>

CC BY-SA 4.0
2018-05-17T13:30:48.573Z
2018-05-17T13:30:48.573Z
Iddo Hanniel
<p>I am using the de boors algorithm to generate B splines in python. However I am getting spikes in the final figure. I do not understand why this happens. I am posting my code here for reference </p>

<pre><code>def knot_values(n,k):
      t = np.zeros((1,n+k+1))
      for i in range(0,n+k+1):
            if i&lt;k:
               t[0][i] = 0
            if i&gt;=k and i&lt;=n:
               t[0][i] = i-k+1
            if i&gt;n:
               t[0][i] = n-k+2

      return t

def basis_spline(u,t,i,k):
    if k==1:
        if u&gt;=t[0][i] and u&lt;=t[0][i+1]:
             sol = 1
        else:
             sol = 0
    else:
        a = (u - t[0][i])*basis_spline(u,t,i,k-1)
        b = t[0][i+k-1] - t[0][i]
        c = (t[0][i+k] - u)*basis_spline(u,t,i+1,k-1)
        d = t[0][i+k] - t[0][i+1]       
        if b == 0:
            temp1 = 0
        else:
            temp1 = a/b

        if d == 0:
            temp2 = 0
        else:
            temp2 = c/d    


        sol = (temp1) + (temp2)

    return sol

def curve_generator(n,k,ctrl_x,ctrl_y):

    t = knot_values(n,k)
    print(t)
    u = np.arange(t[0][k-1],t[0][n+1],0.001);
    length = u.shape;

    x = np.zeros((1,length[0]));
    y = np.zeros((1,length[0]));

    for i in range(0,n+1):
        for j in range(0,length[0]): 
            x[0][j] = x[0][j] + basis_spline(u[j],t,i,k)*ctrl_x[i];
            y[0][j] = y[0][j] + basis_spline(u[j],t,i,k)*ctrl_y[i];

    return x,y 

    n = 6
    k = 3
    ctrl_x = np.array([0,3,6,9,10,12,15])
    ctrl_y = np.array([0,4,2,3,7,8,5])

    [x,y] = curve_generator(n,k,ctrl_x,ctrl_y)

    n = x.shape
    a = [0]*n[1]
    b = [0]*n[1]
    for i in range(0,n[1]):
        a[i] = x[0][i]
        b[i] = y[0][i]

    plt.plot(a,b)
    plt.show()
</code></pre>

<p><a href="https://i.sstatic.net/5ag7U.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5ag7U.jpg" alt="enter image description here"></a></p>

CC BY-SA 4.0
2018-05-13T10:02:37.603Z
Don't post the same question again to add new details. Instead, use the **edit** button when necessary.
Dan Hulme
<bezier-curve><curve><nurbs>
B spline curve generation in Python
Jose Kurian
# B spline curve generation in Python

(Asked by: username_0 on 2018-05-13)

I am using the de boors algorithm to generate B splines in python. However I am getting spikes in the final figure. I do not understand why this happens. I am posting my code here for reference

```
def knot_values(n,k):
      t = np.zeros((1,n+k+1))
      for i in range(0,n+k+1):
            if i<k:
               t[0][i] = 0
            if i>=k and i<=n:
               t[0][i] = i-k+1
            if i>n:
               t[0][i] = n-k+2

      return t

def basis_spline(u,t,i,k):
    if k==1:
        if u>=t[0][i] and u<=t[0][i+1]:
             sol = 1
        else:
             sol = 0
    else:
        a = (u - t[0][i])*basis_spline(u,t,i,k-1)
        b = t[0][i+k-1] - t[0][i]
        c = (t[0][i+k] - u)*basis_spline(u,t,i+1,k-1)
        d = t[0][i+k] - t[0][i+1]       
        if b == 0:
            temp1 = 0
        else:
            temp1 = a/b

        if d == 0:
            temp2 = 0
        else:
            temp2 = c/d    

        sol = (temp1) + (temp2)

    return sol

def curve_generator(n,k,ctrl_x,ctrl_y):

    t = knot_values(n,k)
    print(t)
    u = np.arange(t[0][k-1],t[0][n+1],0.001);
    length = u.shape;

    x = np.zeros((1,length[0]));
    y = np.zeros((1,length[0]));

    for i in range(0,n+1):
        for j in range(0,length[0]): 
            x[0][j] = x[0][j] + basis_spline(u[j],t,i,k)*ctrl_x[i];
            y[0][j] = y[0][j] + basis_spline(u[j],t,i,k)*ctrl_y[i];

    return x,y 

    n = 6
    k = 3
    ctrl_x = np.array([0,3,6,9,10,12,15])
    ctrl_y = np.array([0,4,2,3,7,8,5])

    [x,y] = curve_generator(n,k,ctrl_x,ctrl_y)

    n = x.shape
    a = [0]*n[1]
    b = [0]*n[1]
    for i in range(0,n[1]):
        a[i] = x[0][i]
        b[i] = y[0][i]

    plt.plot(a,b)
    plt.show()

```

[![enter image description here](https://i.sstatic.net/5ag7U.jpg)](https://i.sstatic.net/5ag7U.jpg)

**Tags:** bezier-curve, curve, nurbs

> **Comments:**
> **username_1**: Don't post the same question again to add new details. Instead, use the **edit** button when necessary.  

---

## 1 answers

### Answer by username_2 on 2018-05-17. Score: 2 | Accepted answer

In line 15 use the half-open interval, i.e.,

```
if u>=t[0][i] and u<t[0][i+1]:

```

Otherwise, at knot values, you evaluate two basis functions at the k=1 basis when you only want one. This causes the wrong evaluation of the basis function at the knot values and therefore the spikes.


---

