-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgasket2_course.c
More file actions
67 lines (59 loc) · 1.44 KB
/
gasket2_course.c
File metadata and controls
67 lines (59 loc) · 1.44 KB
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
/* Recursive subdivision of triangle to form Sierpinski gasket */
//v1 flat triangle
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
/* initial triangle */
typedef GLfloat point2[2]; // a 2D point data type
point2 v[]={{-1.0f, -0.58f}, {1.0f, -0.58f}, {0.0f, 1.15f}};
int n = 3; /* number of recursive steps */
void triangle( point2 a, point2 b, point2 c)
{/* draw one triangle */
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
}
void divide_triangle(point2 a, point2 b, point2 c, int m)
{/* triangle subdivision using vertex numbers */
point2 v0, v1, v2;
int j;
if(m > 0)
{
for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
divide_triangle(a, v0, v1, m-1);
divide_triangle(c, v1, v2, m-1);
divide_triangle(b, v2, v0, m-1);
}
else triangle(a,b,c); /* draw triangle at end of recursion */
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
divide_triangle(v[0], v[1], v[2], 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);
}
int main(int argc, char **argv)
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("2D Sierpinski Gasket");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}