⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0303.c

📁 计算机图形学
💻 C
📖 第 1 页 / 共 2 页
字号:
   }
  q=h;
  line(sx,sy,q->dx,q->dy);
}
  mo1(void)
{
   int i;
  struct node *head,*sh,*q,*r;
  int sx,sy,color,dd,left,right,xx,xl,xr,yy,flag,xxr;
  struct point
   { int d[10][2];
     int len;
    } top,bot;
  initgr(); /* BGI初始化 */
   outtextxy(320,200,"Press any key to begin...");
   getch();
   head=create();
  draw_polo(head);
/*  scanf("%d,%d",&sx,&sy);*/
  sx=115;
  dd=105;
  left=70;
  right=220;
  sh=(struct node *)malloc(LEN);
  sh->dx=sx;
  sh->dy=dd;
  sh->next=NULL;
  while (sh!=NULL)
  { sx=sh->dx;
    sy=sh->dy;
    r=sh;
    sh=sh->next;
    free(r);
    xx=sx;
    while ((xx>left)&&(getpixel(xx,sy)!=14))
     { putpixel(xx,sy,12);
       xx=xx-1;
     }
     xl=xx=1;
    xx=sx+1;
    while ((xx<right)&&(getpixel(xx,sy)!=14))
     { putpixel(xx,sy,12);
       xx=xx+1;
     }
    xr=xx-1;
    xx=xl;
    yy=sy+1;
    xxr=left;
    while (getpixel(xxr,yy)!=14)
     xxr=xxr+1;
    while (xx<=xr)
    { flag=0;
      while ((getpixel(xx,yy)!=14)&&(getpixel(xx,yy)!=12)&&(xx<xr))
       { if (flag==0) flag=1;
     xx=xx+1;
       }
     if (flag==1)
      { if ((getpixel(xx,yy)!=14)&&(getpixel(xx,yy)!=12)&&(xx==xr))
     {
       r=(struct node *)malloc(LEN);
       r->dx=xx;
       r->dy=yy;
       r->next=sh;
       sh=r;
      }
    else
      {if (xx-1>xxr)
       {
       r=(struct node *)malloc(LEN);
       r->dx=xx-1;
       r->dy=yy;
       r->next=sh;
       sh=r;
      }
      }
      flag=0;
       }
      dd=xx;
      while (((getpixel(xx,yy)==14)||(getpixel(xx,yy)==12))&&(xx<xr))
       xx=xx+1;
      if (xx==dd) xx=xx+1;
     }

    xr=xx-1;
    xx=xl;
    yy=sy-1;
    xxr=left;
    while (getpixel(xxr,yy)!=14)
     xxr=xxr+1;
    while (xx<=xr)
    { flag=0;
      while ((getpixel(xx,yy)!=14)&&(getpixel(xx,yy)!=12)&&(xx<xr))
       { if (flag==0) flag=1;
     xx=xx+1;
       }
     if (flag==1)
      { if ((getpixel(xx,yy)!=14)&&(getpixel(xx,yy)!=12)&&(xx==xr))
     {
       r=(struct node *)malloc(LEN);
       r->dx=xx;
       r->dy=yy;
       r->next=sh;
       sh=r;
      }
    else
      {if (xx-1>xxr)
       {
       r=(struct node *)malloc(LEN);
       r->dx=xx-1;
       r->dy=yy;
       r->next=sh;
       sh=r;
      }
      }
      flag=0;
       }
      dd=xx;
      while (((getpixel(xx,yy)==14)||(getpixel(xx,yy)==12))&&(xx<xr))
       xx=xx+1;
      if (xx==dd) xx=xx+1;
     }
   }
  outtextxy(320,220,"AreaFilled Finish.");
  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
  closegr(); /* 恢复TEXT屏幕模式 */
  return 0;
}
/*---------------------------------------------------------------------*/
 wang1()
 {   initgr(); 
   setbkcolor(BLUE);
   setcolor(BROWN);
   settextjustify(LEFT_TEXT,0);
   settextstyle(DEFAULT_FONT,HORIZ_DIR,2);


   setcolor(RED);
   setfillstyle(1,RED);
   line(107,104,393,104);                           /*划定区域*/
   line(162,321,393,104);
   line(250,0,162,321);
   line(250,0,338,321);
   line(107,104,338,321);                           /*绘出五角星*/
   floodfill(250,200,RED);
   floodfill(250,100,RED);
   floodfill(120,106,RED);
   floodfill(360,106,RED);
   floodfill(170,300,RED);
   floodfill(330,300,RED);                  /*填充颜色*/

 }
/*--------------------------------------Pro.5 Linewidth---------------------------------*/
void linewidth(int x0,int y0,int x1,int y1,int color,int k)
/*假定x0<x1,y0<y1,-1<m<1*/
{
  int dx, dy,incrE,incrNE,d,x,y;
  int i;
  dx=x1-x0;
  dy=y1-y0;
  d=dx-2*dy;
  incrE=-2*dy;
  incrNE=2*(dx-dy);
  x=x0;
  y=y0;
  for(i=1;i<=k;i++)
  putpixel(x,y+i,color);
  while(x<x1)
   {
    if (d>0)
     d+=incrE;
     else
     {
       d+=incrNE;
       y++;
     }
     x++;
     for (i=1;i<=k;i++)
     if(x%16>3)
     putpixel(x,y+i,color);
    }

}

  mo2(void)
{ int x0,y0,x1,y1,color,k;
  initgr(); /* BGI初始化 */


  linewidth(50,50,220,300,2,1);
  outtextxy(100,350,"Width=1.");
  getch();
  linewidth(150,50,320,300,2,3);
  outtextxy(200,350,"Width=3.");
  getch();
  linewidth(250,50,420,300,YELLOW,5);
  outtextxy(300,350,"Width=5.");
  getch();
  linewidth(350,50,520,300,YELLOW,7);
  outtextxy(400,350,"Width=7.");
  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
  closegr(); /* 恢复TEXT屏幕模式 */
  return 0;
}
/*------------------------Pro.6  The cut of pic------------------------------------*/
typedef int bool;



typedef struct{
               bool all;
               unsigned  left,right,top,bottom;
               }OutCode;   /*定义编码标志*/
typedef struct{
               int xmin,xmax,ymax,ymin;
               }Rectangle;



/*Choen-Sutherland线段裁减算法*/
void  CohenSutherlandLineClip(float x0,float y0,float x1,float y1,Rectangle  *rect)
  {
   void  CompOutCode(float, float,Rectangle *,OutCode *); /*对ComOutCode函数的声明*/
    bool accept,done;
   OutCode  outCode0,outCode1;
   OutCode *outCodeOut;
   float x,y;   /*存储交点坐标*/
   accept=0;
   done=0;
   CompOutCode(x0,y0,rect,&outCode0);
   CompOutCode(x1,y1,rect,&outCode1);
   do{
        if(outCode0.all==0&&outCode1.all==0)  /*完全可见*/
        {accept=1;
        done=1;
        printf("the line is in the rect.");
        }
        else if((outCode0.all&outCode1.all)!=0)  /*完全不可见*/
        {done=1;
         printf("The line cannot be seen.");
        }
        else           /*进行求交测试*/
        {
          if(outCode0.all!=0)   /*判断哪一点位于窗口之外*/
             outCodeOut=&outCode0;
           else
              outCodeOut=&outCode1;
              if(outCodeOut->left)  /*线段与窗口的左边求交*/
              {
                y=y0+(y1-y0)*(rect->xmin-x0)/(x1-x0);
                x=(float)rect->xmin;

              }
             else if (outCodeOut->top)   /*线段与窗口的上边求交*/
             {
               x=x0+(x1-x0)*(rect->ymax-y0)/(y1-y0);
               y=(float)rect->ymax;
             }
             else if (outCodeOut->right)  /*线段与窗口的右边求交*/
             { y=y0+(y1-y0)*(rect->xmax-x0)/(x1-x0);
             x=(float)rect->xmax;

             }
             else if(outCodeOut->bottom)  /*线段与窗口的下边求交*/
             {
             x=x0+(x1-x0)*(rect->ymin-y0)/(y1-y0);
             y=(float)rect->ymin;
             }
             if(outCodeOut->all==outCode0.all)/*以交点为界,将线段位于窗口外侧的部分丢弃.对剩余部分裁减*/
             {
               x0=x;
               y0=y;
               CompOutCode(x0,y0,rect,&outCode0);
             }
             else
             {
               x1=x;
               y1=y;
               CompOutCode(x1,y1,rect,&outCode1);

             }
        }
   } while(!done);
   if(accept)
   line((int)x0,(int)y0,(int)x1,(int)y1);

 }


 /*计算点(x,y)的编码*/
 void CompOutCode(float x,float y,Rectangle *rect,OutCode *outCode)
 {
  outCode->all=0;
  outCode->top=outCode->bottom=0;
  if(y>(float)rect->ymax)
  {
   outCode->top=1;
   outCode->all+=1;
  }

  else if(y<(float)rect->ymin)
  {
   outCode->bottom=1;
   outCode->all+=2;
  }

  outCode->right=outCode->left=0;
   if(x>(float)rect->xmax)
  {
   outCode->right=1;
   outCode->all+=4;
  }
  else if(x<(float)rect->xmin)
  {
   outCode->left=1;
   outCode->all+=8;
  }
}


feng()
{ float x0,y0,x1,y1;Rectangle rect;

  initgr();/*  BGI初始化 */

  printf("please input rect.xmin:");
  scanf("%d",&rect.xmin);
  printf("rect.xmax:");
  scanf("%d",&rect.xmax);
  printf("rect.ymin:");
  scanf("%d",&rect.ymin);
  printf("rect.ymax:");
  scanf("%d",&rect.ymax);

  printf("x0:");
  scanf("%f",&x0);
  printf("y0:");
  scanf("%f",&y0);
  printf("x1:");
  scanf("%f",&x1);
  printf("y1:");
  scanf("%f",&y1);
  setcolor(14);
  cleardevice();
  rectangle(rect.xmin,rect.ymin,rect.xmax,rect.ymax);
  line(x0,y0,x1,y1);
  getch();

  setcolor(BLACK);
  line(x0,y0,x1,y1);

   /*画出矩形裁剪窗口*/
  setcolor(YELLOW);
  CohenSutherlandLineClip(x0,y0,x1,y1,&rect); /*调用函数*/
  getch();
  /*closegraph();   关闭*/
}
/*-----------------------------------Pro. 7  The character-------------------*/
 wang2()
 { initgr();
   line2(50,50,550,50,9);
   line2(50,100,550,100,9);
   line2(50,150,177,150,9);line2(383,150,550,150,9);
   line2(50,200,550,200,9);
   line2(50,250,285,250,9);line2(315,250,550,250,9);
   line2(50,300,285,300,9);line2(315,300,550,300,9);
   line2(50,350,285,350,9);line2(315,350,550,350,9);
   line2(50,400,550,400,9);                    /*背景线*/
   setcolor(YELLOW);
   line(285,75,285,375);
   line(285,75,315,75);
   line(315,75,315,375);
   line(285,375,315,375);
   line(393,122,217,122);
   line(210,152,393,152);
   line(393,152,393,122);
   line(134,220,466,220);
   line(134,250,466,250);
   line(134,220,134,250);
   line(466,220,466,250);
   line(190,100,170,175);
   line(170,175,210,152);
   line(190,100,230,100);
   line(230,100,217,122);
   setfillstyle(1,YELLOW);
   floodfill(286,76,YELLOW);
   floodfill(286,123,YELLOW);
   floodfill(300,199,YELLOW);
   floodfill(300,240,YELLOW);
   floodfill(286,374,YELLOW);
   floodfill(216,123,YELLOW);
   floodfill(382,123,YELLOW);
   floodfill(135,229,YELLOW);
   floodfill(465,229,YELLOW);
   setcolor(RED);
   settextjustify(LEFT_TEXT,0);
   settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
 }


int main(void)
{ int n;
  loop1:initgr(); /* BGI初始化 */

  outtextxy(50,50,"***************************************************************");
  outtextxy(50,300,"***************************************************************");
  outtextxy(90,280,"WELCOME YOU!!");
  setbkcolor(BLUE);
  getch();
  outtextxy(70,70,"Pro.1    The DDALine and MidLine");
  outtextxy(90,90,"Pro.2    The Bezier curve ang B yangtiao curve");
  outtextxy(110,110,"Pro.3    The Midcircle,DDAcircle and DDAEllipse");
  outtextxy(130,130,"Pro.4    The AreaFilled");
  outtextxy(150,150,"Pro.5    The Linewidth");
  outtextxy(170,170,"Pro.6    The cut of the pic");
  outtextxy(190,190,"Pro.7    The character");
  outtextxy(210,210,"Pro.8    EXIT");
  outtextxy(90,280,"WELCOME YOU!!");
  getch();
  outtextxy(110,240,"Please input a number...");

  scanf("%d",&n);

  loop2:switch (n)
    {
   case 1:{zheng();
       goto loop1;break;
       }
   case 2:{liu();
       goto loop1;break;
       }
   case 3:{tang();
       goto loop1;break;
       }
   case 4:{mo1();
       /*wang1();
       getch();*/
       goto loop1;break;
       }
   case 5:{mo2();
       goto loop1;break;
       }
   case 6:{feng();
       goto loop1;break;
       }
   case 7:{wang2();
       getch();
       outtextxy(220,220,"That's all! Thank you!!");
       getch();
       goto loop1;break;
       }
   case 8:exit();break;
   default:{outtextxy(90,260,"INPUT ERROR!!Please input again!");
            scanf("%d",&n); goto loop2;
            }
      }

  /*****此部分添加你自己的代码,例如
  line(25, 25, 220, 220);
  circle(100, 100, 50);
  等等*****/

  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
  closegr(); /* 恢复TEXT屏幕模式 */
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -