Koch Snowflake
Programmed in C using OpenGL
Code for Koch Snowflake
// appropriate includes here
#include <GL.h>
#include <Gl/glut.h>
#include <stdlib.h>
#include <math.h>
class GLintPoint{
public:
GLint x, y;
};
// function prototypes
void myInit(void);
void myDisplay(void);
void lineThing(double x1, double x2, double y1, double y2, int level);
void makeLine(double x1, double x2, double y1, double y2);
void drawDot(void);
int x, y ;
//<<<<<<<<<<<<<<< myinit >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0) ; // set white background color
glColor3f(1.0f, 0.0f, 0.5f) ; // set the drawing color
glPointSize(2.0) ; // a dot is 2 by 2 pixels
glMatrixMode (GL_PROJECTION) ;
glLoadIdentity () ;
gluOrtho2D(0.0, 640.0, 0.0, 640.0) ;
}
//<<<<<<<<<<<<<<< lineThing >>>>>>>>>>>>>>>>>>>>>>>>>>>>
void lineThing(double x1, double x2, double y1, double y2, int level)
{
double angle;
double length;
const double PI = 3.1411592653;
if(level <= 0)
{
x1 = (x1 - 0)*640/2;
y1 = (1 - y1)*640/2;
x2 = (x2 - 0)*640/2;
y2 = (1 - y2)*640/2;
makeLine(x1, y1, x2, y2);
}
else
{
angle = atan2((y2 - y1), (x2-x1));
length = sqrt( pow((y2-y1),2) + pow ((x2-x1), 2))/3.0;
x2 = (x1 + length*cos(angle));
y2 = (y1 + length*sin(angle));
lineThing(x1, x2, y1, y2, level - 1);
x1 = x2;
y1 = y2;
angle = angle - PI/3;
x2 = (x1 + length*cos(angle));
y2 = (y1 + length*sin(angle));
lineThing(x1, x2, y1, y2, level - 1);
x1 = x2;
y1 = y2;
angle = angle + 2*PI/3;
x2 = (x1 + length*cos(angle));
y2 = (y1 + length*sin(angle));
lineThing(x1, x2, y1, y2, level - 1);
x1 = x2;
y1 = y2;
angle = angle - PI/3;
x2 = (x1 + length*cos(angle));
y2 = (y1 + length*sin(angle));
lineThing(x1, x2, y1, y2, level - 1);
}
glFlush();
}
//<<<<<<<<<<<<<<< makeLine >>>>>>>>>>>>>>>>>>>>>>>>>>>>
void makeLine(double x1, double y1, double x2, double y2)
{
glBegin(GL_LINES); //use constant GL_LINES here
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
}
//<<<<<<<<<<<<<<< drawDot >>>>>>>>>>>>>>>>>>>>>>>>>>>>
void drawDot()
{
glClear(GL_COLOR_BUFFER_BIT);
lineThing(0.2, 1.8, -0.5, -0.5, 6);
lineThing(1.8, 1.0, -0.5, 0.885, 6);
lineThing(1.0, 0.2, .885, -0.5, 6);
}
//<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
glutInit(&argc, argv) ; //initializes the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) ; //set the display mode
glutInitWindowSize(640, 640) ; // setwindow size
glutCreateWindow("Iminhell"); // open the screen window
glutDisplayFunc(drawDot) ; // register the redraw functions
myInit(); //additional initializations as necessary
glutMainLoop() ; // go into a perpetual loop
}