home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 545a.lha / bezier_v0.0001 / bezier.c < prev    next >
C/C++ Source or Header  |  1991-09-06  |  4KB  |  208 lines

  1. /*bezier.c*/
  2.  
  3. #define MAXPTS 2000        /* sets max # points to read */
  4. #define MAXBEZ 2000        /* sets max # points to write */
  5.  
  6. /*#include <m68881.h>*/        /* include for math coprocessor */
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. unsigned short int cnt=0,numpnts=0;
  12. static double outarr[MAXBEZ][2];
  13.  
  14.  
  15. main(argc,argv)
  16. char *argv[];
  17. {
  18.  
  19. double array[MAXPTS][2];
  20. unsigned short int q,pstat=0,wb=1;
  21.  
  22. FILE *infile=stdin,*outfile=stdout,*fopen();
  23.  
  24.     if(argc==1)
  25.         {
  26.         fprintf(stderr," Use: %s [-nod] <# BezPoints> [infile][outfile]\n",argv[0]);
  27.         exit(20);
  28.         }
  29.     for(q=1;q<argc;q++)
  30.         {
  31.         if(strncmp(argv[q],"-nod",4)==0)    /* this option allows files without DATA statement */
  32.             {
  33.             wb=0;
  34.             continue;
  35.             }
  36.         else if(pstat==0)
  37.             {
  38.             if(sscanf(argv[q],"%hd",&numpnts)!=1)    /* number of points to generate */
  39.                 {
  40.                 fprintf(stderr," Strange number!\n");
  41.                 exit(20);
  42.                 }
  43.             else ++pstat;
  44.             }
  45.         else if(pstat==1)
  46.             {
  47.             if((infile=fopen(argv[q],"r"))==NULL)
  48.                 {
  49.                 fprintf(stderr," %s cant open file %s for read\n",argv[0],argv[q]);
  50.                 exit(20);
  51.                 }
  52.             else ++pstat;
  53.             }
  54.         else if(pstat==2)
  55.             {
  56.             if((outfile=fopen(argv[q],"w"))==NULL)
  57.                 {
  58.                 fprintf(stderr," %s cant open file %s for write\n",argv[0],argv[q]);
  59.                 exit(20);
  60.                 }
  61.             else ++pstat;
  62.             }
  63.         }
  64.  
  65.  
  66.     ReadData(infile,array,&cnt,wb);
  67.     if(cnt==0)
  68.         {
  69.         fprintf(stderr," %s problem reading data\n",argv[0]);
  70.         exit(20);
  71.         }
  72.     else fprintf(stderr," %s read %hd data pairs\n",argv[0],cnt);
  73.  
  74.     if(infile)fclose(infile);
  75.  
  76.     CalcBez(array,outarr,cnt,&numpnts);
  77.  
  78.     WriteData(outfile,outarr,numpnts,wb);
  79.  
  80.     fprintf(stderr,"\n %s wrote %hd data pairs\n",argv[0],numpnts);
  81.  
  82.     if(outfile)fclose(outfile);
  83.     exit(0);
  84. }
  85.  
  86.  
  87.  
  88. ReadData(ifp,data,count,byers)    /* this routine reads xy data pairs into array */
  89.  
  90. unsigned short int *count,byers;
  91. double data[MAXPTS][2];
  92. FILE *ifp;
  93.  
  94. {
  95.  
  96. register unsigned short int ctr;
  97. char cm,key[50];
  98.  
  99.     while(byers)                /* if true search for keyword DATA */
  100.         {                    /* Byers is the twisted person who requires this format */
  101.  
  102.         if(fscanf(ifp,"%s",key)!=1)
  103.             {
  104.             fprintf(stderr," keyword DATA not found!\n");
  105.             exit(20);
  106.             }
  107.         if(strcmp("DATA",key)==0)break;
  108.         }
  109.  
  110.     for(ctr=0;ctr<MAXPTS;ctr++)
  111.         if(fscanf(ifp,"%lf%c%lf",&data[ctr][0],&cm,&data[ctr][1])!=3)break;
  112.     if(ctr==MAXPTS)fprintf(stderr," MAXPTS reached exiting ReadData\n");
  113.     *count=ctr;
  114.     return(0);
  115. }
  116.  
  117.  
  118. WriteData(ofp,bezdat,numb,byers)    /* this function writes array to file da */
  119.  
  120. unsigned short int numb,byers;
  121. double bezdat[MAXBEZ][2];
  122. FILE *ofp;
  123.  
  124. {
  125. register unsigned short int q;
  126.  
  127.     if(byers)fprintf(ofp," DATA\n");
  128.     for(q=0;q<numb;q++)fprintf(ofp,"%-14le%15le\n",bezdat[q][0],bezdat[q][1]);
  129.     return(0);
  130. }
  131.  
  132.  
  133.  
  134.  
  135. CalcBez(orgarr,bezarr,orgcnt,bezcnt)    /* this function calculates an array of */
  136.                                 /* bezier points from the orginal array */
  137. unsigned short int *bezcnt,orgcnt;
  138. double orgarr[MAXPTS][2], bezarr[MAXBEZ][2];
  139.  
  140. {
  141. register unsigned short int i,j,k,n;
  142.  
  143. double t,step,mr[MAXPTS];
  144. double coef[MAXPTS];
  145.  
  146.  
  147.     if(*bezcnt>MAXBEZ)
  148.         {
  149.             *bezcnt=MAXBEZ;
  150.             fprintf(stderr," bezpnts set to MAXBEZ\n");
  151.         }
  152.  
  153.     CalcCoef(orgcnt,coef);
  154.  
  155.     n=orgcnt-1;
  156.     k=1;
  157.  
  158.     step=1.0/(*bezcnt-1);
  159.  
  160.     for(j=0;j<2;j++)bezarr[0][j]=orgarr[0][j];
  161.  
  162.     for(t=step;t<=1.0;t+=step)        /* The big loop */
  163.         {
  164.         fprintf(stderr,"  t == %.3lf\r",t);
  165.  
  166.         for(i=0;i<=n;i++)
  167.             {
  168.             mr[i]=coef[i]*pow((1.0-t),(double)(n-i))*pow(t,(double)i);
  169.             for(j=0;j<2;j++)bezarr[k][j]+=mr[i]*orgarr[i][j];
  170.             }
  171.         k++;
  172.         }
  173.  
  174.     for(j=0;j<2;j++)bezarr[k][j]=orgarr[n][j];k++;
  175.  
  176.     return(0);
  177. }
  178.  
  179.  
  180.  
  181. CalcCoef(numpts,coarr)    /* this function calcs coeficients from Pascals triangle */
  182.  
  183. unsigned short int numpts;
  184. double coarr[MAXPTS];
  185.  
  186. {
  187. register unsigned short int y,z;
  188. static double temparr[MAXPTS];
  189.  
  190.     coarr[0]=coarr[1]=temparr[0]=1.0;
  191.  
  192.     for(y=1;y<=numpts;y++)
  193.         {
  194.         for(z=1;z<y;z++)temparr[z]=coarr[z-1]+coarr[z];
  195.         for(z=1;z<=y;z++)coarr[z]=temparr[z];
  196.         }
  197.     return(0);
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.