diff options
author | Akshay <[email protected]> | 2021-11-11 07:23:28 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-11-11 07:23:28 +0000 |
commit | 09475f0100ac382eef60365fe58f1115baaca747 (patch) | |
tree | 91b94ab4449e4af84079d99df5e97aebac09d800 /src | |
parent | 1b92bebcce7f85691619713c03d500ed554bd66a (diff) |
Diffstat (limited to 'src')
-rw-r--r-- | src/1/main.c | 5 | ||||
-rw-r--r-- | src/2/main.c | 92 |
2 files changed, 97 insertions, 0 deletions
diff --git a/src/1/main.c b/src/1/main.c index f75eca5..ad35c47 100644 --- a/src/1/main.c +++ b/src/1/main.c | |||
@@ -1,3 +1,8 @@ | |||
1 | /* 2-d serpinski gasket | ||
2 | * | ||
3 | * Akshay Oppiliappan | ||
4 | */ | ||
5 | |||
1 | #include <GL/glut.h> | 6 | #include <GL/glut.h> |
2 | #include <stdlib.h> | 7 | #include <stdlib.h> |
3 | #include <time.h> | 8 | #include <time.h> |
diff --git a/src/2/main.c b/src/2/main.c new file mode 100644 index 0000000..2d78d35 --- /dev/null +++ b/src/2/main.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* 3-d serpinsky gasket | ||
2 | * | ||
3 | * Akshay Oppiliappan | ||
4 | */ | ||
5 | |||
6 | #include <GL/glut.h> | ||
7 | #include <stdio.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <time.h> | ||
10 | |||
11 | typedef float point[3]; | ||
12 | |||
13 | point tetra[4] = { | ||
14 | {0, 250, -250}, | ||
15 | {0, 0, 250}, | ||
16 | {250, -250, 250}, | ||
17 | {-250, -250, -250}, | ||
18 | }; | ||
19 | |||
20 | void myInit() { | ||
21 | glClearColor(1.0, 1.0, 1.0, 1.0); | ||
22 | glOrtho(-300.0, 300.0, -300.0, 300.0, -300.0, 300.0); | ||
23 | }; | ||
24 | |||
25 | void drawTriangle(point p1, point p2, point p3) { | ||
26 | glBegin(GL_TRIANGLES); | ||
27 | glVertex3fv(p1); | ||
28 | glVertex3fv(p2); | ||
29 | glVertex3fv(p3); | ||
30 | glEnd(); | ||
31 | } | ||
32 | |||
33 | void drawTetra(point p1, point p2, point p3, point p4) { | ||
34 | glColor3f(1.0, 1.0, 0.0); | ||
35 | drawTriangle(p1, p2, p3); | ||
36 | |||
37 | glColor3f(0.0, 1.0, 0.0); | ||
38 | drawTriangle(p1, p3, p4); | ||
39 | |||
40 | glColor3f(1.0, 0.0, 0.0); | ||
41 | drawTriangle(p1, p4, p2); | ||
42 | |||
43 | glColor3f(0.0, 0.0, 1.0); | ||
44 | drawTriangle(p2, p3, p4); | ||
45 | } | ||
46 | |||
47 | void divideTetra(point p1, point p2, point p3, point p4, int iter) { | ||
48 | point mid[6]; | ||
49 | int j; | ||
50 | |||
51 | if (iter > 0) { | ||
52 | for (j = 0; j < 3; j++) | ||
53 | mid[0][j] = (p1[j] + p2[j]) / 2; | ||
54 | for (j = 0; j < 3; j++) | ||
55 | mid[1][j] = (p1[j] + p3[j]) / 2; | ||
56 | for (j = 0; j < 3; j++) | ||
57 | mid[2][j] = (p1[j] + p4[j]) / 2; | ||
58 | for (j = 0; j < 3; j++) | ||
59 | mid[3][j] = (p2[j] + p3[j]) / 2; | ||
60 | for (j = 0; j < 3; j++) | ||
61 | mid[4][j] = (p3[j] + p4[j]) / 2; | ||
62 | for (j = 0; j < 3; j++) | ||
63 | mid[5][j] = (p2[j] + p4[j]) / 2; | ||
64 | |||
65 | divideTetra(p1, mid[0], mid[1], mid[2], iter - 1); | ||
66 | divideTetra(mid[0], p2, mid[3], mid[5], iter - 1); | ||
67 | divideTetra(mid[1], mid[3], p3, mid[4], iter - 1); | ||
68 | divideTetra(mid[2], mid[4], p4, mid[5], iter - 1); | ||
69 | } else { | ||
70 | drawTetra(p1, p2, p3, p4); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | void display() { | ||
75 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
76 | glBegin(GL_TRIANGLES); | ||
77 | divideTetra(tetra[0], tetra[1], tetra[2], tetra[3], 5); | ||
78 | glEnd(); | ||
79 | glFlush(); | ||
80 | } | ||
81 | |||
82 | int main(int argc, char *argv[]) { | ||
83 | glutInit(&argc, argv); | ||
84 | glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); | ||
85 | glutInitWindowSize(600, 600); | ||
86 | glutInitWindowPosition(0, 0); | ||
87 | glutCreateWindow("Sierpinski Gasket"); | ||
88 | glutDisplayFunc(display); | ||
89 | glEnable(GL_DEPTH_TEST); | ||
90 | myInit(); | ||
91 | glutMainLoop(); | ||
92 | } | ||