0

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.

  • I can see several problems: You must initialize `k` to zero at the beginning of each iteration through the `for` loop. (Better yet, make `int k = 0` local to that loop.) Then you must sum the coordinates, so at the end of the `while` loop, you should have `xtoplam = xtoplam + ...` or `xtoplam += ...` and `xtoplam` should be initialized to your `xtolpam1`. – M Oehm Nov 07 '20 at 08:44
  • Also, calculate the binomial coefficients only once. You're probably not going to plot splines of very huge degrees, so you can create a table of these coefficients up to, say, `n == 5`. – M Oehm Nov 07 '20 at 08:47
  • But then I cant use the while loop because of k – Cihan Icelliler Nov 07 '20 at 08:50
  • I created a table, but it is very troublesome. Thats why I want to more practical way – Cihan Icelliler Nov 07 '20 at 08:51
  • You find building a table (by hand, by writing in Pascal's triangle into a static array) more troublesome than fiddling with powers and factorials? C(n, k) is a constant, you don't need to calculate them from scratch every time, and neither should the computer. – M Oehm Nov 07 '20 at 08:54
  • I understand you now, but I wonder if you could give me an example – Cihan Icelliler Nov 07 '20 at 08:57
  • Look [here](https://ideone.com/8rqi3K). This example uses a lookup table for the binomial coefficients. Like your code, it has two nested loops: The outer loop controls the variable 0 ≤ _t_ ≤ 1. The inner loop creates the point coordinates P(_t_) by summing the terms. – M Oehm Nov 07 '20 at 09:43
  • Thank you. I am grateful for your comments. I will try this way. When I am done I'll get back to you. – Cihan Icelliler Nov 07 '20 at 10:33

0 Answers0