-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythagoras tree fractal.cpp
79 lines (67 loc) · 1.57 KB
/
pythagoras tree fractal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
// Dimitris Spathis <sdimitris@csd.auth.gr>
// Ergasia 1b, Pythagoras Tree
void drawtree(int n)
{
if(n>0)
{
//////////////////////////
//stack of matrices
glPushMatrix();
glTranslatef(-0.5,1.0,0);
glRotatef(45, 0.0, 0.0, 1.0);
// 1/sqrt(2)=0.707
glScalef(0.707,0.707,0.707);
//recursion
drawtree(n-1);
glPopMatrix();
////////////////////////////
glPushMatrix();
glTranslatef(0.5,1.0,0);
glRotatef(-45, 0.0, 0.0, 1.0);
glScalef(0.707,0.707,0.707);
drawtree(n-1);
glPopMatrix();
//draw a cube
glutSolidCube(1);
}
}
void treeInit(int n)
{
// draw a red tree
glColor3f(1.0, 0.0, 0.0);
drawtree(n);
}
void display (void)
{
glClearColor (1.0,1.0,1.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//initialize with 15 iterations
treeInit(15);
glFlush();
}
void reshape (int w, int h)
{
//reshaoe to aspect ratio
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (80, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (900, 700);
glutInitWindowPosition (300, 100);
glutCreateWindow ("Pythagoras Tree");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}