计算机图形学实验C++代码 联系客服

发布时间 : 星期三 文章计算机图形学实验C++代码更新完毕开始阅读

一、 bresenham算法画直线

#include #include #include

void draw_pixel(int ix,int iy) { glBegin(GL_POINTS); glVertex2i(ix,iy); glEnd();

}

void Bresenham(int x1,int y1,int xEnd,int yEnd) { int dx=abs(xEnd-x1),dy=abs(yEnd-y1); int p=2*dy-dx;

int twoDy=2*dy,twoDyMinusDx=2*dy-2*dx; int x,y; if (x1>xEnd) { x=xEnd;y=yEnd; xEnd=x1; } else { x=x1; y=y1;

}

draw_pixel(x,y); while(x

p+=twoDy; else { y++;

p+=twoDyMinusDx; draw_pixel(x,y);

}

}

}

void display() { glClear(GL_COLOR_BUFFER_BIT); Bresenham(0,0,400,400); glFlush();

1

}

void myinit() { }

void main(int argc,char **argv ) { }

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500);

glutInitWindowPosition(200.0,200.0);

glutCreateWindow(\); glutDisplayFunc(display); myinit(); glutMainLoop();

glClearColor(0.8,1.0,1.0,1.0); glColor3f(0.0,0.0,1.0); glPointSize(1.0);

glMatrixMode(GL_PROJECTION); glLoadIdentity();

gluOrtho2D(0.0,500.0,0.0,500.0);

二、 中点法绘制椭圆

#include #include #include

inline int round(const float a){return int (a+0.5);} void setPixel(GLint xCoord,GLint yCoord) { }

void ellipseMidpoint(int xCenter,int yCenter,int Rx,int Ry) {

int Rx2=Rx*Rx; int Ry2=Ry*Ry; int twoRx2=2*Rx2; int twoRy2=2*Ry2; int p; int x=0; int y=Ry; int px=0; int py=twoRx2*y;

void ellipsePlotPoints(int,int,int,int);

2

glBegin(GL_POINTS); glVertex2i(xCoord,yCoord); glEnd();

}

ellipsePlotPoints(xCenter,yCenter,x,y); p=round(Ry2-(Rx2*Ry)+(0.25*Rx2)); while(px

p=round(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2); while(y>0){ }

y--; py-=twoRx2; if(p>0) }

ellipsePlotPoints(xCenter,yCenter,x,y);

p+=Rx2-py; x++; px+=twoRy2; p+=Rx2-py+px; x++; px+=twoRy2; if(p<0)

p+=Ry2+px; else{ }

ellipsePlotPoints(xCenter,yCenter,x,y);

y--; py-=twoRx2; p+=Ry2+px-py;

else{

void ellipsePlotPoints(int xCenter,int yCenter,int x,int y) { }

void display() { }

void myinit() {

glClearColor(0.8,1.0,1.0,1.0); glColor3f(0.0,0.0,1.0); glPointSize(1.0);

3

glClear(GL_COLOR_BUFFER_BIT); ellipseMidpoint(200,200,50,30); glFlush();

setPixel(xCenter+x,yCenter+y); setPixel(xCenter-x,yCenter+y); setPixel(xCenter+x,yCenter-y); setPixel(xCenter-x,yCenter-y);

}

glMatrixMode(GL_PROJECTION); glLoadIdentity();

gluOrtho2D(0.0,300.0,0.0,300.0);

void main(int argc,char **argv ) { }

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300);

glutInitWindowPosition(200.0,200.0); glutCreateWindow(\); glutDisplayFunc(display); myinit(); glutMainLoop();

三、 抛物线

#include #include #include

inline int round(const float a){return int (a+0.5);} void setPixel(GLint xCoord,GLint yCoord) { }

void ellipseMidpoint(int xCenter,int yCenter,int a,int b) {

int p;

int x=xCenter; int y=yCenter; int px=0,py=0;

void ellipsePlotPoints(int,int,int,int); ellipsePlotPoints(xCenter,yCenter,px,py);

p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5; do{

if(p<0) { } else{

x=x+1; y=y-1;

4

x=x+1; y=y;

p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;

glBegin(GL_POINTS); glVertex2i(xCoord,yCoord); glEnd();