Sierpinsky Gasket

Click here to download

/*
* Status: Working
* Title: Sierpinsky Gasket
* Name: Akshay Thakare
*/

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void Init()
{
	glClearColor(0,0,0,0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1,1,1);
	gluOrtho2D(-5,5,0,5);
}

void sg(double a[],double b[],double c[],double current_depth)
{
	int j;
	double ab[3],ac[3],bc[3];
	
	if(current_depth>10)
		return;
	
	glBegin(GL_LINES);
		glVertex3dv(a);
		glVertex3dv(b);
		glVertex3dv(c);
	glEnd();
	
	for(j=0;j<3;j++)
	{
		ab[j] = (a[j] + b[j])/2.0;
		ac[j] = (a[j] + c[j])/2.0;
		bc[j] = (b[j] + c[j])/2.0;
	}
	sg(a, ab, ac, current_depth+1);
	sg(b, ab, bc, current_depth+1);
	sg(c, ac, bc, current_depth+1);
}

void disp()
{
	double v1[3] = {5,0,0};
	double v2[3] = {-5,0,0};
	double v3[3] = {0,5,0};
	sg(v1,v2,v3,0);
	glFlush();
}

int main(int argc, char ** argv)
{
	glutInit(&argc,argv);
	glutInitWindowSize(300,300);
	glutInitWindowPosition(300,300);
	glutCreateWindow("Sierpinsky Gasket");	
	Init();
	glutDisplayFunc(disp);
	glutMainLoop();
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s