📄 cohen.bak
字号:
//Implementation of Cohen-Sutherland Line Clipping Algorithm
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int Left=0,Right=1,Top=2,Bottom=3;
int wx1,wy1,wx2,wy2;
int getCode(int x,int y){
int code=0;
if(x<wx1) code+=(1<<Left);
else if(x>wx2) code+=(1<<Right);
if(y<wy1) code+=(1<<Top);
else if(y>wy2) code+=(1<<Bottom);
return code;
}
int Clip(int &x1,int &y1,int x2,int y2){
float m,tx,ty;
int code1,code2;
m=(y2-y1)/(float)(x2-x1);
code1=getCode(x1,y1);
while(code1!=0){
//Check Left Boundary
if(code1&(1<<Left)){
tx=wx1-x1;
ty=y1+(tx*m);
x1=wx1; y1=ty;
printf("\nClipping on Left: %d, %d",x1,y1);
code1=getCode(x1,y1);
}
//Check Right Boundary
if(code1&(1<<Right)){
tx=wx2-x1;
ty=y1+(tx*m);
x1=wx2; y1=ty;
printf("\nClipping on Right: %d, %d",x1,y1);
code1=getCode(x1,y1);
}
//Check Top Boundary
if(code1&(1<<Top)){
ty=wy1-y1;
tx=x1+(ty/m);
x1=tx; y1=wy1;
printf("\nClipping on Top: %d, %d",x1,y1);
code1=getCode(x1,y1);
}
//Check Bottom Boundary
if(code1&(1<<Bottom)){
ty=wy2-y1;
tx=x1+(ty/m);
x1=tx; y1=wy2;
printf("\nClipping on Bottom: %d, %d",x1,y1);
code1=getCode(x1,y1);
}
code2=getCode(x2,y2);
if((code2&code1)!=0) return 0;
}
return 1;
}
int main(){
int x1,y1,x2,y2;
int code1,code2,flag;
int gdriver=VGA,gmode=VGAHI;
initgraph(&gdriver,&gmode,"");
printf("\n C o h e n S u t h e r l a n d L i n e C l i p p i n g A l g o r i t h m");
printf(" 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -