From 1b92bebcce7f85691619713c03d500ed554bd66a Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 11 Nov 2021 12:03:13 +0530 Subject: init --- src/1/main.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/1/main.c (limited to 'src') 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 @@ +#include +#include +#include + +typedef GLfloat point2[2]; + +float rand_float() { return 1 * (float)rand() / (float)RAND_MAX; } + +void myInit() { + glClearColor(1.0, 1.0, 1.0, 1.0); + glColor3f(rand_float(), rand_float(), rand_float()); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D(0.0, 500.0, 0.0, 500.0); + glMatrixMode(GL_MODELVIEW); +} + +void triangle(point2 a, point2 b, point2 c) { + glBegin(GL_TRIANGLES); + glVertex2fv(a); + glVertex2fv(b); + glVertex2fv(c); + glEnd(); +} + +void divide_triangle(point2 a, point2 b, point2 c, int k) { + point2 ab, ac, bc; + int j; + if (k > 0) { + for (j = 0; j < 2; j++) + ab[j] = (a[j] + b[j]) / 2; + for (j = 0; j < 2; j++) + ac[j] = (a[j] + c[j]) / 2; + for (j = 0; j < 2; j++) + bc[j] = (b[j] + c[j]) / 2; + + divide_triangle(a, ab, ac, k - 1); + divide_triangle(c, ac, bc, k - 1); + divide_triangle(b, bc, ab, k - 1); + } else { + triangle(a, b, c); + } +} + +void display() { + glClear(GL_COLOR_BUFFER_BIT); + point2 v[3] = {{0.0, 0.0}, {250.0, 500.0}, {500.0, 0.0}}; + divide_triangle(v[0], v[1], v[2], 8); + glFlush(); +} + +int main(int argc, char *argv[]) { + srand(time(0)); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + glutInitWindowSize(500, 500); + glutInitWindowPosition(0, 0); + glutCreateWindow("Sierpinski Gasket"); + glutDisplayFunc(display); + myInit(); + glutMainLoop(); +} -- cgit v1.2.3