计算机图形学实验C++代码(共11页).doc
《计算机图形学实验C++代码(共11页).doc》由会员分享,可在线阅读,更多相关《计算机图形学实验C++代码(共11页).doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上一、 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 (x1xEnd)x=xEnd;y=yEnd;
2、xEnd=x1;elsex=x1;y=y1;draw_pixel(x,y);while(xxEnd)x+;if(p0)p+=twoDy;elsey+;p+=twoDyMinusDx;draw_pixel(x,y);void display()glClear(GL_COLOR_BUFFER_BIT);Bresenham(0,0,400,400);glFlush();void myinit()glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoad
3、Identity();gluOrtho2D(0.0,500.0,0.0,500.0);void main(int argc,char *argv )glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(200.0,200.0);glutCreateWindow(CG_test_Bresenham_Line example);glutDisplayFunc(display);myinit();glutMainLoop();二
4、、 中点法绘制椭圆#include #include #include inline int round(const float a)return int (a+0.5);void setPixel(GLint xCoord,GLint yCoord)glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();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;
5、int p;int x=0;int y=Ry;int px=0;int py=twoRx2*y;void ellipsePlotPoints(int,int,int,int);ellipsePlotPoints(xCenter,yCenter,x,y);p=round(Ry2-(Rx2*Ry)+(0.25*Rx2);while(pxpy)x+;px+=twoRy2;if(p0)y-;py-=twoRx2;if(p0)p+=Rx2-py;elsex+;px+=twoRy2;p+=Rx2-py+px;ellipsePlotPoints(xCenter,yCenter,x,y);void ellip
6、sePlotPoints(int xCenter,int yCenter,int x,int y)setPixel(xCenter+x,yCenter+y);setPixel(xCenter-x,yCenter+y);setPixel(xCenter+x,yCenter-y);setPixel(xCenter-x,yCenter-y);void display()glClear(GL_COLOR_BUFFER_BIT);ellipseMidpoint(200,200,50,30);glFlush();void myinit()glClearColor(0.8,1.0,1.0,1.0);glCo
7、lor3f(0.0,0.0,1.0);glPointSize(1.0);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(circleMId ex
8、ample);glutDisplayFunc(display);myinit();glutMainLoop();三、 抛物线#include #include #include inline int round(const float a)return int (a+0.5);void setPixel(GLint xCoord,GLint yCoord)glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();void ellipseMidpoint(int xCenter,int yCenter,int a,int b)int p;int x
9、=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;doif(p0)x=x+1;y=y;p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;elsex=x+1;y=y-1;p=yCenter+a*(x+1-xCenter
10、)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;px=x-xCenter;py=y-yCenter;ellipsePlotPoints(xCenter,yCenter,px,py);while(pxpy);for(;)if(p0)x=x-1;y=y+1;p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;elsex=x;y=y+1;p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;px=x-xCenter;
11、py=y-yCenter;ellipsePlotPoints(xCenter,yCenter,px,py);void ellipsePlotPoints(int xCenter,int yCenter,int x,int y)setPixel(xCenter+x,yCenter+y);setPixel(xCenter-x,yCenter+y);void display()glClear(GL_COLOR_BUFFER_BIT);ellipseMidpoint(150,150,1,0);glFlush();void myinit()glClearColor(0.8,1.0,1.0,1.0);gl
12、Color3f(0.0,0.0,1.0);glPointSize(1.0);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(500,500);glutInitWindowPosition(200.0,200.0);glutCreateWindow(circleMId
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 图形学 实验 C+ 代码 11
限制150内