B spline Curve

I have got to write a general code for B spline curves. for reasonable values of n+1 control points and k. (say upto 20 and 10 respectively). The mathematical formulation is as under.

I dont need the curve to be plotted I need points on the curve for incremental values of u in the valid range. Can any one help?

at the current stage I am not able to remove these errors. Kindly have a look.this is not the complete program, I am stuck up.

#include<stdio.h>
int i,j,k,ke,n,uj[50];
float u,X,Y, nik[50][50],x[50],y[50],xp[100],yp[100];
void calcuj(j,ke,n)
{
    for(j=0;j<=n+ke;j++)
    {
        if (j<ke)

            uj[j]=0;

        else if (ke<=j && j<=n)

            uj[j]=j-ke+1;

        else

            uj[j]=n-ke+2;
    }
}
float calcNik(i,k,n,u,uj[])
{
    for (k=1;k<=ke;k++)
    {
        if (k=1)
        {
            for(i=0;i<=n+k;i++)
            for(j=0;j<=n+k;j++)
            {
                if (uj[j]<= u && u<= uj[j+1])

                    nik[i][k]=1;

                else

                    nik[i][k]=0;
            }
        }
        else

        {
            for(i=0;i<=n+k;i++)
            for(j=0;j<=n+k;j++)

            {
                return ((u-uj[j]-u)*calcNik(i,k-1,n,u,uj[j])/(uj[i+k-1]-uj[i])  +  (uj[i+k]-u)*calcNik(i+1,k-1,n,u,uj[j])/(uj[i+k]-uj[i+1]));
            }
        }
    }


}

float curvpoint(u,x[],y[],xp[],yp[],X,Y)
{
    for (i=0;i<=n;i++)
        {
            X=xp[i]+xp[i+1];
            xp[i]=x[i]*nik[i][ke];

            Y=yp[i]+yp[i+1];
            yp[i]=y[i]*nik[i][ke];
        }

int main()

{
 int i,j,k,ke;
 printf("Enetr the values of n and k:\n");
 scanf("%d %d", &n, &ke);
 printf("Range of j: 0<=j<= %d, \n i.e. There are %d knot Vectors \n Knot Vector are:\n ",n+ke,n+ke+1);

 for (j=0;j<=n+ke;j++)
 {
  calcuj(j,ke,n);
  printf("U%d = %d\n ",j ,uj[j]);
 }
 for (j=0;j<=n+ke;j++)
{
  calcNik(i,k,n,u);
  printf("N%d1=%f\n",i,nik[i][k]);
}


}

I solved This Question as

/*Program to find the coordinates of a b-spline curve
at evenly incremented multiple values of the parameter u.*/

#include<stdio.h>
#include<math.h>
int i,j,k,ke,n,uj[50];
float u,t,nik[50][50];

int main()

{
 int i,j,k,ke,umax;
 printf("Enter the values of n and ke:\n");
 scanf("%d %d",&n,&ke);
 //Pyramid of Basis Functions; i.e Ni,k ;
 printf ("\n The Pyramid of the basis function is as under:\n\n");
         for (k=ke;k>=1;k--)
       {
         for(i=0;i<n+ke-k+1;i++)
         printf("nik[%d][%d] ",i,k);
         printf("\n\n");
       }
       printf("-------------------------------------------------------------\n");
printf("Range of j: 0<=j<= %d, \n i.e. There are %d knot Vectors \n Knot Vectors are :\n\n ",n+ke,n+ke+1);

// Condition for Calculating Knot Vector(Uj) Values;
for (j=0;j<=n+ke;j++)
 {
       if (j<ke)

           uj[j]=0;

       else if (j>=ke && j<=n)

           uj[j]=j-ke+1;

       else

           uj[j]=n-ke+2;

   //printing Knot Vector Values;
    printf("U%d = %d\n ",j ,uj[j]);
 }
   float denm1,num1,denm2,num2,u1,px[n],py[n],tempx,tempy;
   printf("Enter the values of control points one by one\n");

       //Taking Control Points' Coordinates
       for (j=0;j<=n;j++)
       {
           printf("px[%d],py[%d]=",j,j);
           scanf("%f %f", &px[j],& py[j]);
       }
abc:
   printf("\nEnter the Step length required for U\n Smaller Numbers Give Higher Number of Points on the Generated Curve.\n");
   scanf("%f",&t);
       if (t>0.5)
           {
               printf("\nEnter still Smaller Values of Step length of u\n");
               goto abc;
           }
       else goto go;
go:
 // Range of u is 0 to n-k+2;
 // Injecting incremental values of the parameter U;
   u1=0.0;
   umax=n-ke+2;
   while(u1<=umax)
       {
           printf("\n ");
           for (j=0;j<n+ke;j++)
               {
                   if(uj[j]==uj[j+1]) nik[j][1]=1;
                   if(u1>=uj[j] && u1<=uj[j+1]) nik[j][1]=1;
                   else nik[j][1]=0;

                   printf("nik[%d][1]=%1.2f\t for %d<=u<=%d\n          = 0 \t  elsewhere\n\n ",j,nik[j][1],uj[j],uj[j+1]);
               }

   printf("\n\n\n");

   for(k=2;k<=ke;k++)
       {
           for (i=0;i<n+ke-k+1;i++)
               {
                   denm1 = ((u1-uj[i])*nik[i][k-1]);
                   num1 = (uj[i+k-1]-uj[i]);
                   denm2 = ((uj[i+k]-u1)*nik[i+1][k-1]);
                   num2 = (uj[i+k]-uj[i+1]);
                   if(num1!=0 && num2!=0)
                   nik[i][k]=(denm1/num1) + (denm2/num2);
                   else if(num1!=0 && num2==0)
                   nik[i][k]=(denm1/num1);
                   else if(num2!=0 && num1==0)
                   nik[i][k]=(denm2/num2);
                   else nik[i][k]=0;

                   printf("nik[%d][%d]=%1.1f\n",i,k,nik[i][k]);
               }
       }

   tempx=0;
   tempy=0;
   for(i=0;i<=n;i++)
         {
           tempx=tempx+px[i]*nik[i][ke];
           tempy=tempy+py[i]*nik[i][ke];
         }
           printf("\n-------------------------\n");
           printf("Px(u=%f)=%.3f\n",u1,tempx);
           printf("Py(u=%f)=%.3f\n",u1,tempy);
           printf("\n-------------------------\n");
           u1=u1+t;

        }
}

NOW I need to plot all the values of tempx,tempy on x y frame.I have no idea how to do that. Please help me

good cinemabox program for android/iOS. http://cinemaboxapphd.com Cinema Box program for iOS can be accessible. nice.

You can edit your question to add more information. Doing so allows the information to stay together. I have done this for now.

1 Like