Thread: Help with bezier functions

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    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. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Output your coordinates into a file and study how they differ from what you were expecting.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You put () on the end of it, like you do with all the other functions you seem to be calling successfully.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    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. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    We can't help you unless you post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    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. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM