Exercises Computer Graphics

Session 5 - Escape-Time Fractals Solutions

Solutions

Exercises:

Useful Links:

Introduction

All solutions for session 5.

Exercises

Exercise 1: the Julia Fractal

void drawJulia(double cx, double cy) {
	double x, y, t; 
	int k; 
		 
	for (double i=-2; i<2; i+=4.0/width) 
		for (double j=-2; j<2; j+=4.0/height) {
			x=i; y=j; 
			for (k=0; k<30; k++) { 
				t = x*x-y*y+cx; 
				y = 2*x*y+cy; 
				x = t; 
				if (x*x+y*y>=4) break; 
			}
 			 
			stroke(colorscheme[k]); 
			point((int)((i+2)*width/4), (int)((j+2)*height/4)); 
	  } 
}
Julia

Exercise 2: the Mandelbrot Fractal

void drawMandelBrot() {
	double x, y, t, cx, cy; 
	int k; 
		 
	for (double i=-2; i<2; i+=4.0/width) 
		for (double j=-2; j<2; j+=4.0/height) { 
			x=0; y=0; // difference with Julia: reset to 0,0
			cx=i; cy=j; // new c value
			for (k=0; k<30; k++) { 
				t = x*x-y*y+cx; 
				y = 2*x*y+cy; 
				x = t; 
				if (x*x+y*y>=4) break; 
			} 
  			 
			stroke(colorscheme[k]); 
			point((int)((i+2)*width/4), (int)((j+2)*height/4)); 
		}
}
Valid HTML 4.01 Transitional ©2008-2009 Tom Van Cutsem