fractaltreetask1.c

来自「图形学课件 图形学课件 图形学课件」· C语言 代码 · 共 98 行

C
98
字号
/* gasket2.c   */

/* E. Angel, Interactive Computer Graphics */
/* A Top-Down Approach with OpenGL, Third Edition */
/* Addison-Wesley Longman, 2003 */
/* Recursive subdivision of triangle to form Sierpinski gasket */#include <GL/glut.h>
#include "math.h"
#define PI 3.14159
#define HEIGHT  400
#define PROPORTION 0.618
#define ANGLE  PI/3
#define MIN_LENGTH 10typedef float point2[2];//----------------------------------------------
void tree( float x, float y,float h, float a){
	int i;
	float p;
	float newh;
	int lineWidth;

//	glLineWidth(2);

	glColor3f(0.0,1.0,0.0);
	glBegin(GL_LINES);
       glVertex2f(x,y);
       glVertex2f(x+h*cos(a),y+h*sin(a));
    glEnd();


	newh=h*(1-PROPORTION);
	if(h>=MIN_LENGTH)
	{		
		p=0;
		i=0;
		do
		{
			p=p+pow(PROPORTION,i);
			tree(x+newh*p*cos(a), y+newh*p*sin(a), newh*(pow(PROPORTION,i)) ,a+ANGLE);
			tree(x+newh*p*cos(a), y+newh*p*sin(a), newh*(pow(PROPORTION,i)) ,a-ANGLE);
			i++;
		}
		while((h-newh*p)>10);
	}
	else
	{
		glColor3f(1.0,0.0,0.0);
		glPointSize(3);
		glBegin(GL_POINTS);
			glVertex2f(x+h*cos(a),y+h*sin(a));
		glEnd();
	}
}//-----------------------------------------------void display(void){    glClear(GL_COLOR_BUFFER_BIT);	tree(250,50, HEIGHT, PI/2);    glFlush();}


//------------------------------------------------void myinit(){    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    gluOrtho2D(0.0, 500.0, 0.0, 500.0);    glMatrixMode(GL_MODELVIEW);    glClearColor (0.0, 0.0, 0.0, 1.0);	glColor3f(0.0,1.0,0.0);

	glEnable(GL_COLOR_LOGIC_OP);
	glClear(GL_COLOR_BUFFER_BIT);}
//---------------------------------------------
void main(int argc, char **argv){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );    glutInitWindowSize(500, 500);    glutCreateWindow("Fractal Tree");

    glutDisplayFunc(display);
	myinit();    glutMainLoop();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?