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

📄 4.7.1.c

📁 计算机图形学
💻 C
字号:
/* WIN-TC BGI 图形编程模板 */

#include "Conio.h"
#include "graphics.h"
#define closegr closegraph

void initgr(void) /* BGI初始化 */
{ int gd = DETECT, gm = 0; /* 和gd = VGA,gm = VGAHI是同样效果 */
  registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */
  initgraph(&gd, &gm, "");
}


struct OutCode{ /*编码的数据组织*/
int all;
int left,right,top,bottom;
 };
struct Rectangle{    /*矩形*/
int ymin,ymax,xmin,xmax;
 };
int encode(float x,float y,struct Rectangle *rect)
{int c=0;             /*对直线顶点进行二进制编码,以确定直线位置*/
 if (x<rect->xmin) c=c|1;
 else if (x>rect->xmax) c=c|2;
 if (y<rect->ymin) c=c|4;
 else if (y>rect->ymax) c=c|8;
 return c;
}

void CompOutCode(float x,float y,struct Rectangle *rect,struct OutCode *outcode)
{outcode->all=encode(x,y,rect);/*对直线顶点进行二进制编码,以确定直线位置*/
 outcode->top=outcode->bottom=0;
 if(y>(float)rect->ymax)
    outcode->top=1;
 else if(y<(float)rect->ymin)
   outcode->bottom=1;
 outcode->right=outcode->left=0;
 if(x>(float)rect->xmax)
   outcode->right=1;
 else if(x<(float)rect->xmin)
   outcode->left=1;
}
void CohenSutherlandLineClip(float x0,float y0,float x1,float y1,struct Rectangle *rect)
{void CompOutCode(float,float,struct Rectangle *,struct OutCode *);
 int accept,done;
 struct OutCode outcode0,outcode1,*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; } /*完全可见*/
    else if((outcode0.all&outcode1.all)!=0)
      done=1;             /*完全不可见*/
    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);
         }
      }/*else*/
  }while(!done);
 if(accept)
   line((int)x0,(int)y0,(int)x1,(int)y1);
 }

int main(void)
{ struct Rectangle *rect;
  initgr();/* BGI初始化 */
  rect=(struct Rectangle *)malloc(sizeof(struct Rectangle));
  rect->xmin=50; /*初始化矩形*/
  rect->xmax=150;
  rect->ymin=100;
  rect->ymax=180;
  setcolor(14);/*矩形画线颜色值为14*/
  line(rect->xmin,rect->ymin,rect->xmin,rect->ymax);/*画出矩形*/
  line(rect->xmin,rect->ymin,rect->xmax,rect->ymin);
  line(rect->xmax,rect->ymin,rect->xmax,rect->ymax);
  line(rect->xmax,rect->ymax,rect->xmin,rect->ymax);
  setcolor(3); /*直线画线颜色为3*/
  line(20,70,300,150);/*被裁剪的曲线顶点为(20,70),(300,150)*/
  getch(); /*任意键显示裁剪效果*/
  setcolor(YELLOW);
  CohenSutherlandLineClip(20,70,300,150,rect);
  getch(); /* 暂停一下,看看前面绘图代码的运行结果 */
  closegr(); /* 恢复TEXT屏幕模式 */
  return 0;
}

⌨️ 快捷键说明

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