计算机图形学编码裁剪实例,计算机图形学(3.2)——编码裁剪算法实现

当线段所有在边界外时,将线段的两个端点坐标都改成(0,0),来消去线段。3d

修改后的编码裁剪算法程序:code

// CG_4_编码裁剪算法.cpp :定义控制台应用程序的入口点。orm

//blog

#include"stdafx.h"ip

#includeit

#include

#include

#defineLEFT_EDGE 1

#defineRIGHT_EDGE 2

#defineBOTTOM_EDGE 4

#defineTOP_EDGE 8

voidLineGL(intx0,inty0,intx1,inty1)

{

glBegin(GL_LINES);

glColor3f(1.0f,0.0f,0.0f);  glVertex2f(x0,y0);

glColor3f(0.0f,1.0f,0.0f);  glVertex2f(x1,y1);

glEnd();

}

structRectangle

{

floatxmin,xmax,ymin,ymax;

};

Rectangle rect;

intx0,y0,x1,y1;

intCompCode(intx,inty,Rectangle rect)

{

intcode=0x00;

if(y

code=code|4;

if(y>rect.ymax)

code=code|8;

if(x>rect.xmax)

code=code|2;

if(x

code=code|1;

returncode;

}

intcohensutherlandlineclip(Rectangle rect,int&x0,int&y0,int&x1,int&y1)

{

intaccept,done;

floatx,y;

accept=0;

done=0;

intcode0,code1,codeout;

code0=CompCode(x0,y0,rect);

code1=CompCode(x1,y1,rect);

do{

if(!(code0 | code1))

{

accept=1;

done=1;

}

//else if(code0 & code1)

//   done=1;

elseif(code0 & code1)//当线段所有都在边界外时

{

done=1;

x1=0;y1=0;x0=0;y0=0;

returnaccept;

}

else

{

if(code0!=0)

codeout=code0;

else

codeout=code1;

if(codeout&LEFT_EDGE)//线段与左边界相交

{

y=y0+(y1-y0)*(rect.xmin-x0)/(x1-x0);

x=(float)rect.xmin;

}

elseif(codeout&RIGHT_EDGE)//线段与右边界相交

{

y=y0+(y1-y0)*(rect.xmax-x0)/(x1-x0);

x=(float)rect.xmax;

}

elseif(codeout&BOTTOM_EDGE)//线段与下边界相交

{

x=x0+(x1-x0)*(rect.ymin-y0)/(y1-y0);

y=(float)rect.ymin;

}

elseif(codeout&TOP_EDGE)//线段与上边界相交

{

x=x0+(x1-x0)*(rect.ymax-y0)/(y1-y0);

y=(float)rect.ymax;

}

if(codeout==code0)

{

x0=x;y0=y;

code0=CompCode(x0,y0,rect);

}

else

{

x1=x;y1=y;

code1=CompCode(x1,y1,rect);

}

}

}while(!done);

if(accept)

LineGL(x0,y0,x1,y1);

returnaccept;

}

voidmyDisplay()

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f,0.0f,0.0f);

glRectf(rect.xmin,rect.ymin,rect.xmax,rect.ymax);

LineGL(x0,y0,x1,y1);

glFlush();

}

voidInit()

{

glClearColor(0.0,0.0,0.0,0.0);

glShadeModel(GL_FLAT);

rect.xmin=100;

rect.xmax=300;

rect.ymin=100;

rect.ymax=300;

x0=200,y0=0,x1=0,y1=100;//设置线段的两个端点坐标

printf("Press key 'c' to Clip!\nPress key 'r' to Restore!\n");

}

voidReshape(intw,inth)

{

glViewport(0,0,(GLsizei)w, (GLsizei)h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,(GLdouble) w,0.0,(GLdouble) h);

}

voidkeyboard(unsignedcharkey,intx,inty)

{

switch(key)

{

case'c':

cohensutherlandlineclip(rect,x0,y0,x1,y1);

glutPostRedisplay();

break;

case'r':

Init();

glutPostRedisplay();

break;

case'x':

exit(0);

break;

default:

break;

}

}

intmain(intargc,char* argv[])

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_RGB |GLUT_SINGLE);

glutInitWindowPosition(100,100);

glutInitWindowSize(640,480);

glutCreateWindow("Hello World!");

Init();

glutDisplayFunc(myDisplay);

glutReshapeFunc(Reshape);

glutKeyboardFunc(keyboard);

glutMainLoop();

return0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值