From 09475f0100ac382eef60365fe58f1115baaca747 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 11 Nov 2021 12:53:28 +0530 Subject: add 3d serpinsky gasket --- src/2/main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/2/main.c (limited to 'src/2') 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 @@ +/* 3-d serpinsky gasket + * + * Akshay Oppiliappan + */ + +#include +#include +#include +#include + +typedef float point[3]; + +point tetra[4] = { + {0, 250, -250}, + {0, 0, 250}, + {250, -250, 250}, + {-250, -250, -250}, +}; + +void myInit() { + glClearColor(1.0, 1.0, 1.0, 1.0); + glOrtho(-300.0, 300.0, -300.0, 300.0, -300.0, 300.0); +}; + +void drawTriangle(point p1, point p2, point p3) { + glBegin(GL_TRIANGLES); + glVertex3fv(p1); + glVertex3fv(p2); + glVertex3fv(p3); + glEnd(); +} + +void drawTetra(point p1, point p2, point p3, point p4) { + glColor3f(1.0, 1.0, 0.0); + drawTriangle(p1, p2, p3); + + glColor3f(0.0, 1.0, 0.0); + drawTriangle(p1, p3, p4); + + glColor3f(1.0, 0.0, 0.0); + drawTriangle(p1, p4, p2); + + glColor3f(0.0, 0.0, 1.0); + drawTriangle(p2, p3, p4); +} + +void divideTetra(point p1, point p2, point p3, point p4, int iter) { + point mid[6]; + int j; + + if (iter > 0) { + for (j = 0; j < 3; j++) + mid[0][j] = (p1[j] + p2[j]) / 2; + for (j = 0; j < 3; j++) + mid[1][j] = (p1[j] + p3[j]) / 2; + for (j = 0; j < 3; j++) + mid[2][j] = (p1[j] + p4[j]) / 2; + for (j = 0; j < 3; j++) + mid[3][j] = (p2[j] + p3[j]) / 2; + for (j = 0; j < 3; j++) + mid[4][j] = (p3[j] + p4[j]) / 2; + for (j = 0; j < 3; j++) + mid[5][j] = (p2[j] + p4[j]) / 2; + + divideTetra(p1, mid[0], mid[1], mid[2], iter - 1); + divideTetra(mid[0], p2, mid[3], mid[5], iter - 1); + divideTetra(mid[1], mid[3], p3, mid[4], iter - 1); + divideTetra(mid[2], mid[4], p4, mid[5], iter - 1); + } else { + drawTetra(p1, p2, p3, p4); + } +} + +void display() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBegin(GL_TRIANGLES); + divideTetra(tetra[0], tetra[1], tetra[2], tetra[3], 5); + glEnd(); + glFlush(); +} + +int main(int argc, char *argv[]) { + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); + glutInitWindowSize(600, 600); + glutInitWindowPosition(0, 0); + glutCreateWindow("Sierpinski Gasket"); + glutDisplayFunc(display); + glEnable(GL_DEPTH_TEST); + myInit(); + glutMainLoop(); +} -- cgit v1.2.3