summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/1/main.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/1/main.c b/src/1/main.c
new file mode 100644
index 0000000..f75eca5
--- /dev/null
+++ b/src/1/main.c
@@ -0,0 +1,62 @@
1#include <GL/glut.h>
2#include <stdlib.h>
3#include <time.h>
4
5typedef GLfloat point2[2];
6
7float rand_float() { return 1 * (float)rand() / (float)RAND_MAX; }
8
9void myInit() {
10 glClearColor(1.0, 1.0, 1.0, 1.0);
11 glColor3f(rand_float(), rand_float(), rand_float());
12 glMatrixMode(GL_PROJECTION);
13 glLoadIdentity();
14 gluOrtho2D(0.0, 500.0, 0.0, 500.0);
15 glMatrixMode(GL_MODELVIEW);
16}
17
18void triangle(point2 a, point2 b, point2 c) {
19 glBegin(GL_TRIANGLES);
20 glVertex2fv(a);
21 glVertex2fv(b);
22 glVertex2fv(c);
23 glEnd();
24}
25
26void divide_triangle(point2 a, point2 b, point2 c, int k) {
27 point2 ab, ac, bc;
28 int j;
29 if (k > 0) {
30 for (j = 0; j < 2; j++)
31 ab[j] = (a[j] + b[j]) / 2;
32 for (j = 0; j < 2; j++)
33 ac[j] = (a[j] + c[j]) / 2;
34 for (j = 0; j < 2; j++)
35 bc[j] = (b[j] + c[j]) / 2;
36
37 divide_triangle(a, ab, ac, k - 1);
38 divide_triangle(c, ac, bc, k - 1);
39 divide_triangle(b, bc, ab, k - 1);
40 } else {
41 triangle(a, b, c);
42 }
43}
44
45void display() {
46 glClear(GL_COLOR_BUFFER_BIT);
47 point2 v[3] = {{0.0, 0.0}, {250.0, 500.0}, {500.0, 0.0}};
48 divide_triangle(v[0], v[1], v[2], 8);
49 glFlush();
50}
51
52int main(int argc, char *argv[]) {
53 srand(time(0));
54 glutInit(&argc, argv);
55 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
56 glutInitWindowSize(500, 500);
57 glutInitWindowPosition(0, 0);
58 glutCreateWindow("Sierpinski Gasket");
59 glutDisplayFunc(display);
60 myInit();
61 glutMainLoop();
62}