I want to draw b spline curves. I used the binomial expansion for this and I created a function that calculates factorial.
int faktoriyel(int don)
{
int a=1;
if(don!=0)
{
for(int i=don-1; i>0; i--)
{
don=don*i;
}
}
else
{
don=a;
}
return don;
}
when I want to draw with too many pixels.The for loop is problematic.I have 2 variables:t and k.It needs to form the binomial expansion according to the number of points entered, but it causes problems because of nested loops.
double xtoplam1;
double ytoplam1;
int a,b,c;
int n=sayac-2,k=0;
double t=0.0;
int u=sayac-1;
double xtoplam=0;
double ytoplam=0;
for (t=0.0; t<1.0; t=t+0.0005)
{
xtoplam1= pow(1-t,u-1)*noktalar[0].x;
ytoplam1= pow(1-t,u-1)*noktalar[1].y;
while(k+1<=sayac-1)
{
a=faktoriyel(n);
printf("a:%d\n",a);
b=faktoriyel(k);
printf("b:%d\n",b);
c=faktoriyel(n-k);
printf("c:%d\n",c);
xtoplam = (a/(b*c))*pow(1-t,k+1)*pow(t,n)*noktalar[k+1].x;
ytoplam = (a/(b*c))*pow(1-t,k+1)*pow(t,n)*noktalar[k+1].y;
k++;
}
putpixel (getmaxx()/2+((xtoplam+xtoplam1)*20),getmaxy()/2-((ytoplam+ytoplam1)*20), RED);
printf("x:%f\ny:%f\n",xtoplam+xtoplam1,ytoplam+ytoplam1);
}
How can I fix that.I mean for example I have 2 points.Then function must be pow(1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3] Then "t" should go from 0.0005 to 1
EDIT: I can also use tables for this situation but I think it is so primitive for me. For example:
EDIT:
if(sayac-1==4)
{
for (t=0.0; t<1.0; t=t+0.0005)
{
xtoplam = pow(1-t,3)*noktalar[0].x+3*pow(1-t,2)*pow(t,1)*noktalar[1].x+3*pow(1-t,1)*pow(t,2)*noktalar[2].x+pow(t,3)*noktalar[3].x;
ytoplam = pow(1-t,3)*noktalar[0].y+3*pow(1-t,2)*pow(t,1)*noktalar[1].y+3*pow(1-t,1)*pow(t,2)*noktalar[2].y+pow(t,3)*noktalar[3].y;
putpixel (getmaxx()/2+((xtoplam)*20),getmaxy()/2-((ytoplam)*20), RED);
}
}
This is working well.