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

📄 4-1.c

📁 小试验
💻 C
字号:
/*c-s线段裁剪*/
/*坐标系:屏幕左上角为原点,向下为纵坐标正方向*/
#include<graphics.h>/*加入c图形库*/
#include<conio.h>/*键盘控制*/
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
#define XL 150
#define XR 350
#define YB 100
#define YH 200/*定义视窗四边*/
#include <math.h>
encode(x,y,code)/*编码算法*/
int x,y;
int *code;
{int c;
 c=0;
 if(x<XL)c=c|LEFT;
 else if(x>XR)c=c|RIGHT;
 if(y<YB)c=c|BOTTOM;
 else if(y>YH)c=c|TOP;
 *code=c;
 return;
}
draw_ett()
{
int x1,x2,y1,y2,x,y;
int code1,code2,code;
x1=50;
y1=50;
x2=400;
y2=200;/*待剪切线两顶点*/
setcolor(1);
line(x1,y1,x2,y2);
encode(x1,y1,&code1);
encode(x2,y2,&code2);
while((code1!=0)||(code2!=0))
{if(getch()==17)exit();/*读入键盘按键*/
if((code1&code2)!=0) return;
code=code1;
if(code1==0)code=code2;
if((LEFT&code)!=0)
	{x=XL;
	y=y1+(y2-y1)*(XL-x1)/(x2-x1);
	}
else if((RIGHT&code)!=0)
	{x=XR;
	y=y1+(y2-y1)*(XR-x1)/(x2-x1);
	}
else if((BOTTOM&code)!=0)
	{y=YB;
	 x=x1+(x2-x1)*(YB-y1)/(y2-y1);
	}
else if((TOP&code)!=0)
	{y=YH;
	 x=x1+(x2-x1)*(YH-y1)/(y2-y1);
	 }
if(code==code1)
	{x1=x;
	 y1=y;
	 encode(x,y,&code1);
	}
else 	{
	x2=x;
	y2=y;
	encode(x,y,&code2);
	}
}
setcolor(14);
line(x1,y1,x2,y2);/*剪切后线*/
return;
}
void main()
{
int driver,mode;
driver=DETECT;/*初始化显示模式参数*/
initgraph(&driver,&mode,"..\\bgi");/*初始化显示为VGA驱动的640*480、16色模式*/
printf("Press any key to continue except 'Ctrl+Q' to quit.\n");
setcolor(12);
line(XL,YH,XR,YH);
line(XL,YB,XR,YB);
line(XL,YH,XL,YB);
line(XR,YH,XR,YB);/*视窗边框*/
setcolor(14);
draw_ett();
if(getch()==17)exit();/*读入键盘按键*/
closegraph();/*关闭图形模式*/
}

⌨️ 快捷键说明

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