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
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);
}
}