您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 数据通信与网络 > openGL虚线-点划线-KOCK曲线实验报告
实验四实验报告1、实验目的和要求利用OpenGL实现实线,虚线,点划线的绘制,及kock曲线。2、实验内容1)用OpenGL程序绘制实现,虚线和点划线2)用OpenGL程序,分别以直线和正三角形为初始生成员,实现迭代次数在6次以内的kock曲线,要求用键盘交互控制迭代次数。3、实验步骤1)相关算法及原理描述①直线的绘制在OpenGL中绘制直线通过指定直线段的端点来实现,用glVertex函数指定直线段端点的坐标位置,用glBegin/glEnd函数对包含一系列的点坐标,并利用符号常量解释这些点构成直线的方式。②虚线的绘制绘制虚线需要先调用函数过了Enable(GL_LINE_STIPPLE);打开划线模式。然后,函数glLineStipple将建立用于划线的模式glLineStipple(Glintfactor,GLushortpattern);③Kock曲线Kock曲线的初始生成员是一条直线,生成规则是将直线段分为三等分,首尾两段保持不变,中间用两段等长且互成60°角的直线段代替。这样,直线段被分成四段,每段长度都只有原来的1/3。Kock曲线的分形维数为D=ln4/ln3≈1.26186假设原直线的首尾点是P0(x0,y0),P1(x1,y1),则新的四段直线段的五个端点坐标分别为(x0,y0)(x0+(x1-x0)/3,y0+(y1-y0)/3)((x1+x0)/2±(y0-y1)√3/6,(y1+y0)/2±(x1-x0)√3/6)(x0+2(x1-x0)/3,y0+2(y1-y0)/3)(x1,y1)其中,第三个点坐标公式中的正负号表示中间两条心直线段处于元直线段的哪一侧。根据这一规则迭代六次。2)程序调试、测试与运行结果分析①直线的绘制结果②虚线的绘制结果③以直线作为初始生成元的Kock曲线④以三角形作为初始生成元的Kock曲线4、实验总结在上一次的实验基础上,利用GL_LINES画直线,这项实验内容比较容易。然后,利用glEnable(GL_LINE_STIPPLE)函数和glLineStiopple(Glintfactor,GLushortpattern)进行虚线和点画线的实现。也是比较简单的。第二个实验是利用Kock曲线实现迭代生成图形,这个实验很难。用了两节课的时间,最终也没有成功。我自己没有做成功,去请教别人,看别人的才做出来的。看到生成的图像很漂亮不管结果怎么样,我在这次的实验中学到了很多知识,对编程也好,对openGL也好,都有了更好更深的理解。5、附录1直线的绘制#includegl/glut.hintwinWidth=400,winHeight=300;voidInitial(void){glClearColor(1.0f,1.0f,1.0f,1.0f);}voidChangeSize(intw,inth){winWidth=w;winHeight=h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2i(100,80);glVertex2i(220,250);glEnd();glutSwapBuffers();}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,100);glutCreateWindow(直线);glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);Initial();glutMainLoop();return0;}2虚线及点划线的绘制#includegl/glut.hintwinWidth=400,winHeight=300;voidInitial(void){glClearColor(1.0f,1.0f,1.0f,1.0f);}voidChangeSize(intw,inth){winWidth=w;winHeight=h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);}voidDisplay(void){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2i(100,80);glVertex2i(220,250);glEnd();glutSwapBuffers();}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,100);glutCreateWindow(虚线);glEnable(GL_LINE_STIPPLE);glLineStipple(2,0X00FF);glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);Initial();glutMainLoop();return0;}3以直线为生成元的Kock曲线#includeGL/glut.h#includeiostream#includevector#includemath.h#definePI3.1415926usingnamespacestd;structPoint{floatx;floaty;};staticvectorPointcount;staticintm=0;//迭代次数floatline;voidfirst_state(vectorPoint&count)//初始情况是只有两个点{Pointfirst={-1.0f,0.0f},end={1.0f,0.0f};line=sqrt(pow(first.x-end.x,2)+pow(first.y-end.y,2));count.push_back(first);count.push_back(end);}voidDraw_pic(vectorPoint&count){for(vectorPoint::size_typei=0;i!=(count.size()-1);i++){glBegin(GL_LINES);glVertex2f(count[i].x,count[i].y);glVertex2f(count[i+1].x,count[i+1].y);glEnd();}}voidCalculate_point(vectorPoint&count){vectorPointnew_count;floatpline=line/(float)pow((float)3,(int)m);vectorPoint::size_typei;for(i=0;i!=(count.size()-1);i++){Pointp1,p2,pmid;p1.x=count[i].x+(count[i+1].x-count[i].x)/3;p1.y=count[i].y+(count[i+1].y-count[i].y)/3;p2.x=count[i+1].x-(count[i+1].x-count[i].x)/3;p2.y=count[i+1].y-(count[i+1].y-count[i].y)/3;doublealpha=0.0;if(count[i+1].y=count[i].y){alpha=atan((double)(count[i+1].y-count[i].y)/(count[i+1].x-count[i].x));if(count[i+1].xcount[i].x){pmid.x=p1.x+pline*cos(alpha+PI/3);pmid.y=p1.y+pline*sin(alpha+PI/3);}else{pmid.x=p1.x-pline*cos(alpha+PI/3);pmid.y=p1.y-pline*sin(alpha+PI/3);}}else{alpha=atan((double)(count[i].y-count[i+1].y)/(count[i+1].x-count[i].x));if(count[i+1].xcount[i].x){pmid.x=p2.x-pline*cos(alpha+PI/3);pmid.y=p2.y+pline*sin(alpha+PI/3);}else{pmid.x=p2.x+pline*cos(alpha+PI/3);pmid.y=p2.y-pline*sin(alpha+PI/3);}}new_count.push_back(count[i]);new_count.push_back(p1);new_count.push_back(pmid);new_count.push_back(p2);}new_count.push_back(count[i]);count.clear();count=new_count;//之前要清空}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);switch(m){case0:case1:case2:case3:case4:case5:case6:case7:case8:case9:case10:Draw_pic(count);break;default:break;}glFlush();}voidKeyboard(unsignedcharkey,intx,inty){if(key==''){++m;Calculate_point(count);}glutPostRedisplay();}intmain(intargc,char*argv[]){first_state(count);glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowPosition(100,100);glutInitWindowSize(400,400);glutCreateWindow(直线Kock曲线);glutDisplayFunc(&myDisplay);glutKeyboardFunc(Keyboard);//键盘响应回调函数glutMainLoop();return0;}4以三角形为初始生成元的Kock曲线#includeGL/glut.h#includeiostream#includevector#includemath.h#definePI3.1415926usingnamespacestd;structPoint{floatx;floaty;};staticvectorPointcount1,count2,count3;constPointfirst={-0.5f,0.1f},end={0.5f,0.1f},second={0.0f,-0.766f};staticintm=0;//迭代次数floatline;voidfirst_state(vectorPoin
本文标题:openGL虚线-点划线-KOCK曲线实验报告
链接地址:https://www.777doc.com/doc-4559739 .html