2.c
资源名称:0.rar [点击查看]
上传用户:jsyccqq
上传日期:2022-08-09
资源大小:2k
文件大小:3k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include<math.h>
  3. #include<conio.h>
  4. #include <time.h>
  5. #include<graphics.h>
  6. #define PI 3.1415926
  7. void   Sleeptime(int s)     /*延时函数*/
  8. {
  9.   clock_t start, end;
  10.   start=  clock();
  11.   do
  12.   {
  13.      end   =   clock();
  14.   }while(end-start<s);
  15.   return;
  16. }
  17. float Casteljau(int k,float *controls,float t) /*bezier曲线子函数一*/
  18.       {   int r,i;
  19.            float t1,controlsa[10];
  20.            t1=1.0-t;
  21.            for(i=0;i<=k;i++)
  22.              {controlsa[i]=controls[i];}
  23.            for(r=1;r<=k;r++)
  24.                for(i=0;i<=k-r;i++)
  25.                    {controlsa[i]=t1*controlsa[i]+t*controlsa[i+1];}
  26.            return controlsa[0];
  27.        }
  28. void bezier_to_points(int k,int npoints,float* controls,float* points)/*bezier曲线子函数二*/
  29.        {
  30.        float t,delt;
  31.            int i;
  32.            delt=1.0/npoints;
  33.            t=0.0;
  34.            for(i=0;i<=npoints;i++)
  35.               {   points[i]=Casteljau(k,controls,t);
  36.                    t=t+delt;
  37.                }
  38.        }
  39.     int drawbz(int x,int y,int r) /*画图函数*/
  40.     {
  41.     int  npoints=400;
  42.     int i,color=1;
  43.     float a ;
  44.     float controlsx[4];
  45.     float controlsy[4];
  46.     float points_x[401];
  47.     float points_y[401];
  48.     int k=3;
  49.     for(a=0.0;a<2*PI;a+=2*PI/180)
  50.      {
  51.          controlsx[0]=x;controlsx[1]=x+300*cos(a);
  52.          controlsx[2]=x+1200*cos(a);controlsx[3]=x+1500*cos(a);
  53.          controlsy[0]=y;controlsx[1]=y+100*sin(a);
  54.          controlsx[2]=y+300*sin(a); controlsx[3]=y+600*sin(a);
  55.         bezier_to_points(k,npoints,controlsx,points_x);
  56.         bezier_to_points(k,npoints,controlsy,points_y);
  57.         for(i=0;i<400;i++)
  58.            {
  59.             putpixel(points_x[i],points_y[i],color);
  60.             }
  61.            color++;
  62.       }
  63.     return 0;
  64.     }
  65.   int main()
  66. {
  67.     int i,j,k,t, n;
  68.     int r,x0,y0;
  69.     int graphdriver=DETECT,graphmode;
  70.     printf("input the number of paozhu:n");
  71.     scanf("%d",&n);
  72.     srand((unsigned)time(NULL));
  73.     /*registerbgidriver(EGAVGA_driver);*/        /*大多数编译环境,运行环境都不用这句*/
  74.     initgraph(&graphdriver,&graphmode,"");
  75.     for(i=0;i<n;i++)
  76.     {
  77.         x0=250+ rand()%140;
  78.         y0=240 + rand()%80;
  79.         r=20+rand()%20;
  80.       for(j=480;j>y0;j--)
  81.         {
  82.             for(k=10;k>0;k--)
  83.             {
  84.                 putpixel(x0,j+k,RED);
  85.                 putpixel(x0-1,j+k,RED);
  86.                 putpixel(x0+1,j+k,RED);
  87.             }
  88.           Sleeptime(0.1);
  89.           cleardevice();
  90.         }
  91.         drawbz(x0,y0,r);
  92.         t=5+rand()%10;
  93.         Sleeptime(t);
  94.         cleardevice();
  95.         }
  96.     closegraph();
  97.     return 0;
  98. }