1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

help with c++ bezier programme

Discussion in 'C++' started by Rich999, Mar 19, 2006.

Thread Status:
Not open for further replies.
  1. Rich999

    Rich999 New Member

    Joined:
    Mar 19, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Ive been trying to write a programme to draw a bezier curve, and have got stuck cos i cant debug it anymore. Alot of runtime errors. Doh! :eek:
    My last option is to just post the whole damn thing on a forum and hope someone can help me.
    Here is the code

    Code:
    #pragma windows 500000,500000
    #include <windows.h>
    #include <dbos/graphics.h>
    #include <iostream.h>
    #include <math.h>
    
    int controlpoints();
    int translate();
    int clearscreen();
    int replot();
    int scale();
    int fact(int i);
    int nCi(int i);
    double bu(int i,double u);
    void C(double u);
    void calc_bez();
    void 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 bez_points[number][3];
    
    int main()
    {  int width=650;  int height=650;
    
       winio("%gr[black]&", width, height);
       winio("%ca[Bezier Curve]&");
       winio("%ww&");
       winio("\t%bfBezier Curve Options%sf\n&");
       winio("\n \tDegrees Of Bezier Curve\t%dd%1rd\n&",1, &degrees);
       winio("\n \t%^bt[Change Control Points]\n&", controlpoints);
       winio("\n \t%2`ga%rb[Show Control Lines]\n \t%rb[Don`t Show Control Lines]\n\n&",&a,&b,&a,&b);
       winio("\n \t%bfTRANSFORMATIONS:%sf\n&");
       winio("\n \t%^bt[Translate]\n&", translate);
       winio("\n \t%^bt[Scale]\n&", scale);
       winio("\n \t%bt[Rotate]\n&");
       winio("\n\n \t%bf%bt[Quit]%sf&");
       winio("%lw", &handle);
    }
    
    int translate()
    {
       if (degrees<1)
       {
          winio("%ca[Error]&");
          winio("The bezier curve must have at least 1 degree.\n\nPlease increase the number of degrees&");
          winio("\n\n%bt[Back to main menu]");
       }
    
       else if (degrees>7)
       {
          winio("%ca[Error]&");
          winio("The bezier curve cannot have more than 7 degrees.\n\nPlease decrease the number of degrees&");
          winio("\n\n%bt[Back to main menu]");
       }
    
       else if  (degrees<=7)
       {
       winio("%ca[Translating]%ww&");
       winio("Please enter the amounts by which you wish to translate your bezier curve\n\n&");
       winio("Enter translaton co-ordinates for the x co-ordinate:\t%rf\n\n&",&translation[0]);
       winio("Enter translation co-ordinates for the y co-ordinate:\t%rf\n\n&",&translation[1]);
       winio("Enter translation co-ordinates for the z co-ordinates:\t%rf\n\n&",&translation[2]);
       winio("%bt[Next]");
    
       for (int i=0; i<8; i++)
       {
          for (int j=0; j<3; j++)
          {
             controlpointT[i][j]=controlpoint[i][j]+translation[j];
    
             controlpoint[i][j]=controlpointT[i][j];
          }
       }
    
       winio("%ca[Translating]&");
       winio("%^bt[Replot Graph]\t\t&", replot);
       winio("%bt[Return To Main Menu Without Re-Plotting]");
       }
    
       return 1;
    }
    
    int scale()
    {
       winio("%ca[Scaling]&");
       winio("Please enter the scaling factor, by which you wish to scale your bezier curve\n\n&");
       winio("Please note, the scaling factor must be between 0 and 1:\t%rf\n\n&", &scalingfactor);
       winio("%bt[Next]");
    
    
       winio("%ca[Scaling]&");
       winio("%^bt[Replot Graph]\t\t&", replot);
       winio("%bt[Return To Main Menu Without Re-Plotting]");
    
       return 1;
    }
    
    int clearscreen()
    {
       clear_screen();
       return 1;
    }
    
    int controlpoints()
    {
       if (degrees>7)
       {
          winio("%ca[Error]&");
          winio("The bezier curve must cannot have more than 7 degrees.\n\nPlease decrease the number of degrees and re-plot the control points&");
          winio("\n\n%bt[Back to main menu]");
       }
    
       else if (degrees<1)
       {
          winio("%ca[Error]&");
          winio("The bezier curve must have at least 1 degree.\n\nPlease increase the number of degrees and re-plot the control points&");
          winio("\n\n%bt[Back to main menu]");
       }
    
       else if (degrees<=7)
       {
          winio("%ca[Control Points]&");
          winio("%ww&");
          for (int y=0; y<degrees+1; y++)
          {
             winio("Enter control point %wd co-ordinates (x,y,z):\t%rf\t%rf\t%rf\n\n&",y+1,&controlpoint[y][0],&controlpoint[y][1],&controlpoint[y][2]);
          }
          winio("%bt[Done]");
    
          clearscreen();
    
          if (a==1)
          {
             for (int i=0; i<degrees; i++)
             {
                
                draw_line(controlpoint[i][0],controlpoint[i][1],controlpoint[i+1][0],controlpoint[i+1][1],9);
             }
          }
          // draw curve
          
          draw_bez();
       }
    
       return 1;
    }
    
    int replot()
    {
       for (int d=1; d<degrees+1; d++)
       {
             clearscreen();
    
             if (a==1)
             {
                for (int i=0; i<degrees; i++)
                {
                   
                   draw_line(controlpoint[i][0],controlpoint[i][1],controlpoint[i+1][0],controlpoint[i+1][1],9);
                }
             }
       }
    
       return 0;
    }
    
    int fact(int i)
    {
       if (i<=1) return 1;
       else return i*fact(i-1);
    }
    
    int nCi(int i)
    {
       return fact(degrees)/(fact(degrees-i)*fact(i));
    }
    
    
    double bu(int i,double u)
    {
       return nCi(i)*pow(1-u,degrees-1)*pow(u,i);
    }
    
    void C(double u)
    {
       double point[3];
      
       for (int k=0;k<3;k++)
       {
          for (int i=0;i<degrees+1;i++)
          {
             for (int j=0;j<number;j++)
             {
                point[k]=bu(i,u)*controlpoint[i][k];
    
                bez_points[j][k]=point[k];            
             }
          }
       }
    }
    
    void calc_bez()
    {
       double u;
    
       for (int i=0;i<number;i++)
       {
          u=(i)/(number-1);
          
          C(u);
       }
    }
    
    void draw_bez()
    {
       calc_bez();
    
       for (int i=0;i<number;i++)
       {
          draw_line(bez_points[i][0],bez_points[i][1],bez_points[i+1][0],bez_points[i+1][1],9);
       }
    }
    
    Thanks in advance.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    [thread=648]Duplicate thread[/thread] closed.
     
Thread Status:
Not open for further replies.

Share This Page