Koch Curve

Click here to download

/*
* Status: Tested OK
* Title: Koch Curve
* Name: Akshay Thakare
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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(-300,500,-300,500);
}

void ks(int lev, double x1, double y1, double x5, double y5)
{
	double deltaX, deltaY, x2, y2, x3, y3, x4, y4;

          if (lev == 0){
 		glBegin(GL_LINES);
 			glVertex2d(x1,y1);
 			glVertex2d(x5,y5);
            glEnd();
          }
          else{
                deltaX = x5 - x1;
                deltaY = y5 - y1;

                x2 = x1 + deltaX / 3;
                y2 = y1 + deltaY / 3;

                x3 = (int) (0.5 * (x1+x5) + sqrt(3) * (y1-y5)/6);
                y3 = (int) (0.5 * (y1+y5) + sqrt(3) * (x5-x1)/6);

                x4 = x1 + 2 * deltaX /3;
                y4 = y1 + 2 * deltaY /3;

                ks (lev-1, x1, y1, x2, y2);
                ks (lev-1, x2, y2, x3, y3);
                ks (lev-1, x3, y3, x4, y4);
                ks (lev-1, x4, y4, x5, y5);
          }
}

void disp()
{
	ks(3,20,280,280,280);
	ks(3,280,280,150,20);
	ks(3,150,20,20,280);
	glFlush();
}

int main(int argc, char ** argv)
{
	glutInit(&argc,argv);
	glutInitWindowSize(350,350);
	glutInitWindowPosition(300,300);
	glutCreateWindow("Koch Curve");
	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