Fractals
Example Program
(:source lang=C++ :)
- include <GL/glut.h>
- include <stdio.h>
- include <stdlib.h>
int iterations = 4; float ystart = 0.5;
void recursion(int depth, float x1, float x2){
float mid1 = 1.0/3.0; float mid2 = 2.0/3.0; float x_mid1 = x1 + mid1 * (x2 - x1); float x_mid2 = x1 + mid2 * (x2 - x1); float y = ystart - (iterations-depth) * 0.1; glBegin(GL_LINES); glVertex2f(x1, y); glVertex2f(x2, y); glEnd(); if(depth > 0){ recursion(depth - 1, x1, x_mid1); recursion(depth - 1, x_mid2, x2); }
}
void display(void) {
glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); recursion(iterations, -0.5, 0.5); glFlush();
}
void keyboard(unsigned char key, int x, int y) {
if(key == 27) exit(0);
else if(key > 47 && key < 58){
iterations = key - 48;
}
printf("You requested d\n",iterations, key);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); glutCreateWindow("Empty Window"); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;
} (:sourceend:)
Your tasks:
- adapt the make file from last class
- give the lines random colours
- adapt selection of iterations to accept numbers >9
- make it possible to step to the next and previous iteration