




已阅读5页,还剩54页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
643附录A 实 例 程 序A.1 Sierpinski镂垫程序/* two-dimensional Sierpinski gasket */* generated using randomly selected vertices */* and bisection */#include /*you may have to change the include to orelsewhere depending on where it is stored on your system */* glut.h usually has included for gl.h and glu.h */void myinit(void)/* attributes */ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */* set up viewing */* 50.0 50.0 camera coordinate window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 50.0, 0.0, 50.0); glMatrixMode(GL_MODELVIEW);void display(void)/* A triangle */ GLfloat vertices32=0.0,0.0,25.0,50.0,50.0,0.0; int i, j, k; int rand(); /* standard random number generator */ GLfloat p2 =7.5,5.0; /* an arbitrary initial point inside traingle */ glClear(GL_COLOR_BUFFER_BIT); /* clear the window */ glBegin(GL_POINTS);/* compute and plots 5000 new points */ for( k=0; k5000; k+) j=rand()%3; /* pick a vertex at random */ /* Compute point halfway between selected vertex and old point */ p0 = (p0+verticesj0)/2.0; p1 = (p1+verticesj1)/2.0; /* plot new point */ glVertex2fv(p); glEnd(); glFlush(); /* clear buffers */ void main(int argc, char* argv)/* Standard GLUT initialization */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); /* default, not needed */ glutInitWindowSize(500,500); /* 500 500 pixel window */ glutInitWindowPosition(0,0); /* place window top left on display */ glutCreateWindow(Sierpinski Gasket); /* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */ myinit(); /* set attributes */ glutMainLoop(); /* enter event loop */A.2 生成Sierpinski镂垫的递归程序/* Recursive subdivision of triangle to form Sierpinski gasket */* number of recursive steps given on command line */#include #include/* initial triangle */GLfloat v32=-1.0, -0.58, 1.0, -0.58, 0.0, 1.15;int n;void triangle( GLfloat *a, GLfloat *b, GLfloat *c)/* display one triangle */ glVertex2fv(a); glVertex2fv(b); glVertex2fv(c);void divide_triangle(GLfloat *a, GLfloat *b, GLfloat *c, int m)/* triangle subdivision using vertex numbers */ GLfloat v02, v12, v22; int j; if(m0) for(j=0; j2; j+) v0j=(aj+bj)/2; for(j=0; j2; j+) v1j=(aj+cj)/2; for(j=0; j2; j+) v2j=(bj+cj)/2; divide_triangle(a, v0, v1, m-1); divide_triangle(c, v1, v2, m-1); divide_triangle(b, v2, v0, m-1); elsetriangle(a,b,c); /* draw triangle at end of recursion */void display(void) glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_TRIANGLES);divide_triangle(v0, v1, v2, n);glEnd(); glFlush();void myinit() glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-2.0, 2.0, -2.0, 2.0); glMatrixMode(GL_MODELVIEW); glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(0.0,0.0,0.0);void main(int argc, char *argv) n=atoi(argv1);/* or set number of subdivision steps here */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow(Sierpinski Gasket); glutDisplayFunc(display);myinit(); glutMainLoop();A.3 三维Sierpinski镂垫的递归程序/* Recursive subdivision of a tetrahedron to form 3D Sierpinski gasket */* number of recursive steps given on command line */#include #include /* initial tetrahedron */GLfloat v43=0.0, 0.0, 1.0,0.0, 0.942809, -0.33333, -0.816497, -0.471405, -0.333333,0.816497, -0.471405, -0.333333;GLfloat colors43=1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0;int n;void triangle( GLfloat *va, GLfloat *vb, GLfloat *vc) glVertex3fv(va); glVertex3fv(vb); glVertex3fv(vc);void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d) glColor3fv(colors0); triangle(a,b,c); glColor3fv(colors1); triangle(a,c,d); glColor3fv(colors2); triangle(a,d,b); glColor3fv(colors3); triangle(b,d,c);void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m) GLfloat mid63; int j; if(m0) /* compute six midpoints */ for(j=0; j3; j+) mid0j=(aj+bj)/2; for(j=0; j3; j+) mid1j=(aj+cj)/2; for(j=0; j3; j+) mid2j=(aj+dj)/2; for(j=0; j3; j+) mid3j=(bj+cj)/2; for(j=0; j3; j+) mid4j=(cj+dj)/2; for(j=0; j3; j+) mid5j=(bj+dj)/2; /* create 4 tetrahedrons by subdivision */ divide_tetra(a,mid0,mid1,mid2, m-1); divide_tetra(mid0,b,mid3,mid5, m-1); divide_tetra(mid1,mid3,c,mid4, m-1); divide_tetra(mid2,mid4,d,mid5, m-1); else tetra(a,b,c,d); /* draw tetrahedron at end of recursion */void display(void) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES);divide_tetra(v0,v1,v2,v3,n);glEnd();glFlush();void myReshape(int w, int h) glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w = h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glutPostRedisplay();void main(int argc, char *argv) n=atoi(argv1);/* or enter number of subdivision steps here */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow(3D Gasket); glutReshapeFunc(myReshape); glutDisplayFunc(display); glEnable(GL_DEPTH_TEST); glClearColor (1.0, 1.0, 1.0, 1.0); glutMainLoop();A.4 Marching-Square程序/* generates contours using marching squares */* region size */#define X_MAX 1.0#define Y_MAX 1.0#define X_MIN -1.0#define Y_MIN -1.0/* number of cells */#define N_X 50#define N_Y 50/* contour value */#define THRESHOLD 0.0#includevoid display()double f(double,double);int cell(double,double,double,double);void lines(int,int,int,double,double,double,double);double dataN_XN_Y;int i,j;int c;glClear(GL_COLOR_BUFFER_BIT);/* form data array from function */for(i=0;iN_X;i+)for(j=0;jN_Y;j+)dataij=f(X_MIN+i*(X_MAX-X_MIN) /(N_X-1.0),Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0);/* process each cell */for(i=0;iN_X;i+)for(j=0;jTHRESHOLD) n+=1;if(bTHRESHOLD) n+=8;if(cTHRESHOLD) n+=4;if(dTHRESHOLD) n+=2;return n ;/* draw line segments for each case */void lines(int num,int i,int j,double a ,double b,double c,double d)void draw_one(int,int,int,double,double,double,double);void draw_adjacent(int,int,int, double,double,double,double);void draw_opposite(int,int,int,double,double,double,double);switch(num) case 1 : case 2: case 4 : case 7: case 8: case 11: case 13: case 14:draw_one(num,i,j,a,b,c,d);break;case 3: case 6: case 9: case 12:draw_adjacent(num,i,j,a,b,c,d);break;case 5: case 10:draw_opposite(num,i,j,a,b,c,d);break;case 0: case 15:break;void draw_one(int num,int i,int j,double a,double b,double c,double d)double x1,y1,x2,y2;double ox,oy;double dx,dy;dx=(X_MAX-X_MIN)/(N_X-1.0);dy=(Y_MAX-Y_MIN)/(N_Y-1.0);ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0);oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0);switch(num) case 1 : case 14:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-a)/(b-a);y2=oy;break;case 2: case 13:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-d)/(c-d);y2=oy+dy;break;case 4: case 11:x1=ox+dx*(THRESHOLD-d)/(c-d);y1=oy+dy;x2=ox+dx;y2=oy+dy*(THRESHOLD-b)/(c-b);break;case 7: case 8:x1=ox+dx*(THRESHOLD-a)/(b-a);y1=oy;x2=ox+dx;y2=oy+dy*(THRESHOLD-b)/(c-b);break;glBegin(GL_LINES);glVertex2d(x1,y1);glVertex2d(x2,y2);glEnd();void draw_adjacent(int num,int i,int j,double a,double b,double c ,double d)double x1,y1,x2,y2;double ox,oy;double dx,dy;dx=(X_MAX-X_MIN)/(N_X-1.0);dy=(Y_MAX-Y_MIN)/(N_Y-1.0);ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0);oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0);switch(num) case 3 : case 12:x1=ox+dx*(THRESHOLD-a)/(b-a);y1=oy;x2=ox+dx*(THRESHOLD-d)/(c-d);y2=oy+dy;break;case 6: case 9:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx;y2=oy+dy*(THRESHOLD-b)/(c-b);break;glBegin(GL_LINES);glVertex2d(x1,y1);glVertex2d(x2,y2);glEnd();void draw_opposite(int num,int i,int j,double a,double b,double c ,double d)double x1,y1,x2,y2,x3,y3,x4,y4;double ox,oy;double dx,dy;dx=(X_MAX-X_MIN)/(N_X-1.0);dy=(Y_MAX-Y_MIN)/(N_Y-1.0);ox=X_MIN+i*(X_MAX-X_MIN)/(N_X-1.0);oy=Y_MIN+j*(Y_MAX-Y_MIN)/(N_Y-1.0);switch(num) case 5 :x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-a)/(b-a);y2=oy;x3=ox+dx*(THRESHOLD-d)/(c-d);y3=oy+dy;x4=ox+dx;y4=oy+dy*(THRESHOLD-b)/(c-b);break;case 10:x1=ox;y1=oy+dy*(THRESHOLD-a)/(d-a);x2=ox+dx*(THRESHOLD-d)/(c-d);y2=oy+dy;x3=ox+dx*(THRESHOLD-d)/(c-d);y3=oy;x4=ox+dx;y4=oy+dy*(THRESHOLD-b)/(c-b);break;glBegin(GL_LINES);glVertex2d(x1,y1);glVertex2d(x2,y2);glVertex2d(x3,y3);glVertex2d(x4,y4);glEnd();void myReshape(int w, int h) glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w = h) gluOrtho2D(X_MIN, X_MAX, Y_MIN * (GLfloat) h / (GLfloat) w, Y_MAX * (GLfloat) h / (GLfloat) w); else gluOrtho2D(X_MIN * (GLfloat) w / (GLfloat) h, X_MAX * (GLfloat) w / (GLfloat) h, Y_MIN, Y_MAX); glMatrixMode(GL_MODELVIEW);void main(int argc, char *argv) glutInit(&argc, argv);glutInitWindowSize(500, 500); glutCreateWindow(contour plot); glutReshapeFunc(myReshape); glutDisplayFunc(display); glClearColor(0.0, 0.0, 0.0, 1.0);glColor3f(1.0,1.0,1.0); glutMainLoop();A.5 生成正方形的程序/* This program illustrates the use of the GLUT library forinterfacing with a Window System */* The program opens a window, clears it to black,then draws a box at the location of the mouse each time theleft button is clicked. The right button exits the program.The program also reacts correctly when the window ismoved or resized by clearing the new window to black. */#include #include /* globals */GLsizei wh = 500, ww = 500; /* initial window size */GLfloat size = 3.0; /* half side length of square */void drawSquare(int x, int y) y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON);glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush();/* reshaping routine called whenever window is resizedor moved */void myReshape(GLsizei w, GLsizei h)/* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */glViewport(0,0,w,h); glClear(GL_COLOR_BUFFER_BIT); glFlush();/* set global size for use by drawing routine */ww = w; wh = h; void myinit(void) glViewport(0,0,ww,wh);/* Pick 2D clipping window to match size of screen window. This choice avoids having to scale object coordinateseach time window is resized. */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww, 0.0, (GLdouble) wh, -1.0, 1.0);/* set clear color to black and clear window */ glClearColor (0.0, 0.0, 0.0, 1.0);glClear(GL_COLOR_BUFFER_BIT);glFlush();void mouse(int btn,int state,int x,int y)if(btn=GLUT_RIGHT_BUTTON &state=GLUT_DOWN) exit(0)/* display callback required by GLUT*/viod display()int main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow(square); myinit(); glutReshapeFunc (my Reshape); glutMouseFunc (mouse) glutMotionFunc (drawSqure); glutDisp layFunc (display); glutMainLoop();A.6 画图程序/* simple painting program with text, lines, triangles,rectangles,and points */#define NULL 0#define LINE 1#define RECTANGLE 2#define TRIANGLE 3#define POINTS 4#define TEXT 5#include #include void mouse(int, int, int, int);void key(unsigned char, int, int);void display(void);void drawSquare(int, int);void myReshape(GLsizei, GLsizei);void myinit(void);void screen_box(int, int, int);void right_menu(int);void middle_menu(int);void color_menu(int);void pixel_menu(int);void fill_menu(int);int pick(int, int);/* globals */GLsizei wh = 500, ww = 500; /* initial window size */GLfloat size = 3.0; /* half side length of square */int draw_mode = 0; /* drawing mode */int rx, ry; /* raster position */GLfloat r = 1.0, g = 1.0, b = 1.0; /* drawing color */int fill = 0; /* fill flag */void drawSquare(int x, int y) y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd();/* rehaping routine called whenever window is resizedor moved */void myReshape(GLsizei w, GLsizei h)/* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); display(); glFlush();/* set global size for use by drawing routine */ ww = w; wh = h; void myinit(void)glViewport(0,0,ww,wh);/* Pick 2D clipping window to match size of X window. This choice avoids having to scale object coordinateseach time window is resized.*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);/* set clear color to black and clear window */ glClearColor (0.8, 0.8, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); glFlush();void mouse(int btn, int state, int x, int y) static int count; int where;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《数理统计》第8章 正态总体均值的假设检验
- 异地采集指纹办理流程
- 中医诊断学培训课件
- 维修电工高级试题含答案(附解析)
- 计算机基础模拟考试题与参考答案解析
- 为老年人提供必要的医疗和健康服务确保他们得到及时治疗和关怀
- 2024年5月配电线路工专业试题+参考答案解析
- 5月1+x无损检测模拟试题与答案(附解析)
- 种子种苗遗传改良方法考核试卷
- 如何培养自律的孩子家庭教育
- 公路工程质量试题及答案
- 中央贸促会面试题及答案
- 产业链购销合同
- 出口美国合同范本
- 2025-2030中国香紫苏醇市场发展形势及未来投资风险预警研究报告
- 2024年市场营销师品牌宣传技巧试题及答案
- 教育机构与旅行社合作合同新规定
- 脑-肠轴与肠道菌群互作-深度研究
- 体重管理培训课件
- 住院糖尿病血糖管理课件
- 人教版八年级英语下册导学案(全册 共10个单元)
评论
0/150
提交评论