# B-splines implementation

**URL:** <https://discourse.processing.org/t/b-splines-implementation/31991>\
**Category:** Coding Questions\
**Created:** [August 29, 2021, 3:23am UTC](https://discourse.processing.org/t/b-splines-implementation/31991 "2021-08-29T03:23:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![molo32](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@molo32](https://discourse.processing.org/u/molo32)\
**Post date:** [August 29, 2021, 3:23am UTC](https://discourse.processing.org/t/b-splines-implementation/31991/1 "2021-08-29T03:23:33Z")

</div>

I am trying to draw a b-spline, i don’t want to use third party libraries I want to implement it myself.,  
the code below does not work correctly. control point px [0] p and [0] appears to be at 0.0.

but it should be x = 22 y = 33, that is, the curve should start from 22.33.

someone who understands spline curves can tell me it’s wrong.

source:[B-splines](https://opensourc.es/blog/b-spline/)

```
float[] px2={ 22,44,55};
float[] py2={ 33, 150,35};

float[] k={0,0,0,1,1,1};

void setup(){
  size(300,300);
}

void draw(){

  //draw control points    
         stroke(255,255,0);
  for (int i = 0; i <= px2.length - 1; i++)
        {
  ellipse(px2[i],py2[i],5,5);
        }

 b_spline(px2,py2, 2, k, 100);  
}

     /////////////////// B-SPLINE /////
     

    float b_spline_basis(float i,float r,float t, float[] u){
       float u_i = u[int(i+1)];
       float u_i1 = u[int(i+2)];
       float u_ir = u[int(i+r+1)];
       float u_i1r = u[int(i+r+2)];
    
        if (r == 0 ){
            if( u_i <= t && t<= u_i1)
               { return 1.0;}
            else 
               { return 0;}
        } 
        else{
             
            float left = 0;
            if ((u_ir-u_i) != 0){
                left = (t-u_i)/(u_ir-u_i) * b_spline_basis(i,r-1,t,u);
            }
    
            float right = 0;
            if ((u_i1r-u_i1) != 0){
                right = (u_i1r-t)/(u_i1r-u_i1) * b_spline_basis(i+1,r-1,t,u);
            }
            return left + right ;
        
        }  
    }    
        
        
    void b_spline(float[] dBx,float[] dBy, int n, float[] knot_vector, int steps){
      
        float m = dBx.length;
        
       float u_min = knot_vector[1];
       float u_max = knot_vector[knot_vector.length-1];
       float step_size = (u_max-u_min)/(steps);
       
       
        for(float t=u_min; t<u_max; t+=step_size){
   
           float[] pos = new float[2];
            for(int i=0; i<m; i++){
             // if(i<dBy.length && i+1+n<knot_vector.length){
             
               if (knot_vector[i+1] != knot_vector[i+1+n]){
               
                    pos[0] = pos[0]+dBx[i+1]*b_spline_basis(float(i),float(n),t,knot_vector);
                     pos[1] = pos[1] + dBy[i+1] *b_spline_basis(float(i),float(n),t,knot_vector);
                   
               // }
              }
            }
          
          stroke(255,0,0);
            ellipse( pos[0],pos[1] ,5,5);
             
        }
       
    }
```

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [August 29, 2021, 7:19am UTC](https://discourse.processing.org/t/b-splines-implementation/31991/2 "2021-08-29T07:19:17Z")

</div>

If you do weaken, check out [toxiclibs](http://toxiclibs.org/docs/core/toxi/geom/Spline2D.html).

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 2, 2021, 10:11pm UTC](https://discourse.processing.org/t/b-splines-implementation/31991/3 "2021-09-02T22:11:42Z")

</div>

Guesses (I haven’t examined it carefully):

1. this has something to do with a flipped sign – the original Julia example being modeled with an inverted y axis

2. it has something to do with the translation of Julia’s broadcast operators (.)

3. The domain of the original example is offset, and the result of b\_spline should be something more like: `ellipse(pos[0] + dBx[0], pos[1] + dBy[0], 5, 5);`

These are just guesses. I actually think reading the code of a correct implementation – either processing or toxiclibs – might shed light on this, rather than just focusing on translating Julia.
