计算机图形学Cohen-Sutherland直线裁剪算法实验.doc
文本预览下载声明
《计算机图形学实验》报告
任课教师:钱文华
2016年春季学期
实验:
实验时间:2016年11月3日
实验地点:信息学院2204
实验目的:掌握Cohen-Sutherland直线裁剪算法
程序代码:
#include stdlib.h
#include math.h
#include iostream
#includestdio.h
#include Windows.h
#include GL/GL.h
#include glut.h
int a,b,a1,b1,pp0,pq0,pp1,pq1;
void setPixel(GLint x,GLint y){
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void init(void){
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,200.0,0.0,150.0);
}
void LineDDA(int x0,int y0,int xEnd,int yEnd){
int dx = xEnd - x0;
int dy = yEnd - y0;
int steps,k;
float xIncrement,yIncrement,x = x0,y = y0;
if(abs(dx)abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xIncrement = float(dx)/float(steps);
yIncrement = float(dy)/float(steps);
for (k = 0;ksteps;k++)
{
x+=xIncrement;
y+=yIncrement;
setPixel(x,y);
}
}
//裁剪
class wcPt2D{
public:
GLfloat x,y;
};
inline GLint round(const GLfloat a){
return GLint(a+0.5);
}
const GLint winLeftBitCode = 0x1;
const GLint winRightBitCode = 0x2;
const GLint winBottomBitCode = 0x4;
const GLint winTopBitCode = 0x8;
inline GLint inside(GLint code){
return GLint(!code);
}
inline GLint reject(GLint code1,GLint code2){
return GLint(code1code2);
}
inline GLint accept(GLint code1,GLint code2){
return GLint(!(code1|code2));
}
GLubyte encode(wcPt2D pt,wcPt2D winMin,wcPt2D winMax){
GLubyte code = 0x00;
if(pt.xwinMin.x)
code = code|winLeftBitCode;
if(pt.xwinMax.x)
code = code|winRightBitCode;
if(pt.ywinMin.y)
code = code|winBottomBitCode;
if(pt.ywinMax.y)
code = code|winTopBitCode;
return(code);
}
void swapPts(wcPt2D *p1,wcPt2D *p2){
wcPt2D tmp;
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void swapCodes(GLubyte *c1,GLubyte *c2){
GLubyte tmp;
tmp = *c1;
*c1 = *c2;
*c2 = tmp;
}
void lineClipCohSuth(wcPt2D winMin,wcPt2D winMax,wcPt2D p1,wcPt2D p2){
GLubyte code1,code2;
GLint done = false,plotLine = false;
GLfloat m;
int x0=0;int y0=0;int x1=0;int y1=0;
while(!done){
code1 = encode(p1,winMin,winMax);
code2 = encode(p2,w
显示全部