# Thread: Help with bezier functions

1. ## Help with bezier functions

I am trying to get a bezier curve drawn in a program.
These are the functions i have written for calculating the points, however it doesnt quite work and my brain has started to hurt so i need a bit of help.
Can anyone spot where ive gone wrong??

Code:
```int fact(int i)
{
if (i<=1) return 1;
else return i*fact(i-1);
}

int nCi(int degrees, int i)
{
return fact(degrees)/(fact(degrees-i)*fact(i));
}

double bu(int i,double u)
{
return nCi(degrees,i)*pow(1-u,degrees-1)*pow(u,i);
}

void C(double u, double Pt[3])
{
for (int k=0; k<3; k++)
{
Pt[k]=0.0;
for (int i=0; i<degrees+1; i++)
{
Pt[k]+=bu(i,u)*controlpoint[i][k];
}
}
}

void calc_bezier_pts()
{
double u;

for (int i=0;i<number;i++)
{
u=(i)/(number-1);

C(u, bezier_curve[i]);
}
}

int draw_bez()
{
calc_bezier_pts;

for (int i=0;i<number;i++)
{
draw_line(x_bez[i],y_bez[i],x_bez[i+1],y_bez[i+1],9);
}

return 1;
}```
More code can be posted if you need to see the bigger picture.

2. Output your coordinates into a file and study how they differ from what you were expecting.

3. Code:
```int draw_bez()
{
/*!!  warning: statement is a reference, not call, to function calc_bezier_pts */
/*!!  warning: statement has no effect */
calc_bezier_pts;

for (int i=0;i<number;i++)
{
draw_line(x_bez[i],y_bez[i],x_bez[i+1],y_bez[i+1],9);
}

return 1;
}```
Turn up the warning level on your compiler.

Also, your fact() function is horribly expensive compared to a simple loop, plus you need to know that 12! is the max factorial you can calculate in an int.

4. plus you need to know that 12! is the max factorial you can calculate in an int.
You can store 12! in 32 bits, but an int isn't guaranteed to be 32 bits. A long is, though, so you meant "12! is the highest factorial you can store in a long".

5. yeah i get that warning that the statment has no effect.
So how do i get the statement to actually execute and run the functions, rather than just being a reference to it?

6. You put () on the end of it, like you do with all the other functions you seem to be calling successfully.

7. Bollocks !!
Thanks for pointing that out.
You don't know how long i spent trying to figure out what was going wrong there as well.
Blonde moment there!

8. Now im getting a few run time errors, and a warning saying int nCi has not been used, when it looks to me that is has been used.
I am in a world of pain.

9. We can't help you unless you post your latest code.

10. Here is the code
Code:
```int fact(double i)
{
if (i<=1) return 1;
else return i*fact(i-1);
}

int nCi(double i)
{
return fact(degrees)/(fact(degrees-i)*fact(i));
}

double bu(double i,double u)
{
return nCi(i)*pow(1-u,degrees-1)*pow(u,i);
}

void C(double u, double Pt[3])
{
for (int k=0; k<3; k++)
{
Pt[k]=0.0;
for (int i=0; i<degrees+1; i++)
{
Pt[k]+=bu(i,u)*controlpoint[i][k];
}
}
}

void calc_bezier_pts()
{
double u;

for (int i=0;i<number;i++)
{
u=(i)/(number-1);

C(u, bezier_curve[i]);
}
}

int draw_bez()
{
calc_bezier_pts();

for (int i=0;i<number;i++)
{
draw_line(bezier_curve[i][0],bezier_curve[i][1],bezier_curve[i+1][0],bezier_curve[i+1][1],9);
}

return 1;
}```
and here are the variables functions etc...

Code:
```int controlpoints();
int translate();
int clearscreen();
int replot();
int scale();
int fact(int i);
int nCi(int n, int i);
double bu(int i,double u);
void C(double u, double Pt[3]);
void calc_bezier_pts();
int draw_bez();

int degrees=3;
double controlpoint[8][3]={{0,350,0},{250,350,0},{300,100,0},{350,500,0},{400,200,0},{450,450,0},{500,350,0},{650,350,0}};
double controlpointT[8][3];
double translation[3];
int handle;
double scalingfactor;
int a=1;
int b=0;

const int number=1000;

double bezier_curve[number][3];```
I would post the entire code but i know other people from my uni course look at this forum and i dont want someone to cut/paste my whole programme and hand it in as their own.

Cheers for all the help though everyone.

11. > and a warning saying int nCi has not been used, when it looks to me that is has been used.

This one takes 1 parameter
int nCi(double i)

This one takes 2
int nCi(int n, int i);

If you make them agree, I'm sure the warning will go away.

As for your runtime errors, you need to run the code inside a debugger, or put some cout statements in some strategic places to find out what is really going on.

Popular pages Recent additions