1、openGL虚线点划线KOCK曲线实验报告实验四实验报告1、 实验目的和要求利用OpenGL实现实线,虚线,点划线的绘制,及kock曲线。2、 实验内容1) 用OpenGL程序绘制实现,虚线和点划线2) 用OpenGL程序,分别以直线和正三角形为初始生成员,实现迭代次数在6次以内的kock曲线,要求用键盘交互控制迭代次数。3、 实验步骤1) 相关算法及原理描述1 直线的绘制在OpenGL中绘制直线通过指定直线段的端点来实现,用glVertex函数指定直线段端点的坐标位置,用glBegin/glEnd函数对包含一系列的点坐标,并利用符号常量解释这些点构成直线的方式。2 虚线的绘制绘制虚线需要先调
2、用函数过了Enable(GL_LINE_STIPPLE);打开划线模式。然后,函数glLineStipple将建立用于划线的模式glLineStipple(Glint factor, GLushort pattern);3 Kock曲线Kock曲线的初始生成员是一条直线,生成规则是将直线段分为三等分,首尾两段保持不变,中间用两段等长且互成60角的直线段代替。这样,直线段被分成四段,每段长度都只有原来的1/3。Kock曲线的分形维数为 D=ln4/ln31.26186假设原直线的首尾点是P0(x0,y0),P1(x1,y1),则新的四段直线段的五个端点坐标分别为(x0 , y0) (x0+(x1
3、-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) 程序调试、测试与运行结果分析1 直线的绘制结果2 虚线的绘制结果3 以直线作为初始生成元的Kock曲线4 以三角形作为初始生成元的Kock曲线4、 实验总结在上一次的实验基础上,利用 GL_LINES画直线,这项实验内容比较容易。然后,利用glEnable(GL_LINE_STIPPL
4、E)函数和glLineStiopple(Glint factor ,GLushort pattern)进行虚线和点画线的实现。也是比较简单的。第二个实验是利用Kock曲线实现迭代生成图形,这个实验很难。用了两节课的时间,最终也没有成功。我自己没有做成功,去请教别人,看别人的才做出来的。看到生成的图像很漂亮不管结果怎么样,我在这次的实验中学到了很多知识,对编程也好,对openGL也好,都有了更好更深的理解。5、附录1直线的绘制#includeint winWidth=400,winHeight=300;void Initial(void)glClearColor(1.0f,1.0f,1.0f,1
5、.0f);void ChangeSize(int w,int h)winWidth=w;winHeight=h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);void Display(void)glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glBegin(GL_LINES);glVertex2i(100,80);glVertex2i(220,250);glEnd();gl
6、utSwapBuffers();int main(int argc,char*argv)glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,100);glutCreateWindow(直线);glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);Initial();glutMainLoop();return 0;2虚线及点划线的绘制#includeint win
7、Width=400,winHeight=300;void Initial(void)glClearColor(1.0f,1.0f,1.0f,1.0f);void ChangeSize(int w,int h)winWidth=w; winHeight=h;glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,winWidth,0.0,winHeight);void Display(void)glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0
8、.0f);glBegin(GL_LINES);glVertex2i(100,80);glVertex2i(220,250);glEnd();glutSwapBuffers();int main(int argc,char*argv)glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,100);glutCreateWindow(虚线);glEnable(GL_LINE_STIPPLE);glLineStipple(
9、2,0X00FF);glutDisplayFunc(Display);glutReshapeFunc(ChangeSize);Initial();glutMainLoop();return 0;3以直线为生成元的Kock曲线#include #include #include #include #define PI 3.1415926 using namespace std; struct Point float x; float y; ; static vector count; static int m = 0; /迭代次数 float line ; void first_state(ve
10、ctor &count) /初始情况是只有两个点 Point first = -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); void Draw_pic(vector &count) for (vector:size_type i = 0; i != (count.size()-1) ; i+) glBegin(GL_LINES); glVertex2f(counti.
11、x, counti.y); glVertex2f(counti+1.x, counti+1.y); glEnd(); void Calculate_point(vector &count) vector new_count; float pline = line /(float) pow(float)3,(int)m) ; vector:size_type i; for ( i= 0; i != (count.size() - 1); i+ ) Point p1 , p2 , pmid; p1.x = counti.x + (counti+1.x - counti.x) / 3; p1.y =
12、 counti.y + (counti+1.y - counti.y) / 3; p2.x = counti+1.x - (counti+1.x - counti.x) / 3; p2.y = counti+1.y - (counti+1.y - counti.y) / 3; double alpha = 0.0; if (counti+1.y = counti.y) alpha = atan(double) (counti+1.y - counti.y) / (counti+1.x - counti.x) ); if (counti+1.x counti.x) pmid.x = p1.x + pline * cos(alpha + PI / 3); pmid.y = p1.y + pline * sin(alpha + PI / 3);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1