Von Koch and friends
About the code
The von Koch curve
 
				 
				 
				 
				
		void vonKoch(Vec2f p1, Vec2f p2, int level)
		{
				
			Vec2f   n[3];
			Vec2f   v12 = p2 - p1;
			n[0] = p1 + v12 * 1 / 3;
			n[2] = p1 + v12 * 2 / 3;
			Vec2f   v02 = n[2] - n[0];
			v02.rotate(DEG_TO_RAD(-60));
			n[1] = n[0] + v02;
				
			vonKoch(  p1, n[0], level + 1);
			vonKoch(n[0], n[1], level + 1);
			vonKoch(n[1], n[2], level + 1);
			vonKoch(n[2],   p2, level + 1);
		}
				
		int maxLevel = 5;
		void vonKoch(Vec2f p1, Vec2f p2, int level)
		{
			if (level >= maxLevel)
			{
				
				gfx.line(p1.x, p1.y, p2.x, p2.y, Color(255, 255, 255));
				return;
			}
				
		Vec2f   left (             0, SCREEN_HEIGHT/2);
		Vec2f   right(SCREEN_WIDTH-1, SCREEN_HEIGHT/2);
		vonKoch(left, right, 0);
		Download source code
		Download executable for Windows
				
 
				
Animating
		if (level >= maxLevel)
		{
			gfx.line(p1.x, p1.y, p2.x, p2.y, Color(255, 255, 255));
			gfx.render();
			sys.wait(20);
			return;
		}
		Download source code
		Download executable for Windows
				
		int cnt = 0;
		// wait until we quit
		while (sys.isQuitRequested() == false)
		{
			if (cnt == 0)
			{
				gfx.clearScreen(Color(0, 0, 0, SDL_ALPHA_OPAQUE));
				vonKoch(left, right, 0);
			}
				
			cnt++;
			if (maxLevel < 6 && cnt == 50)
			{
				cnt = 0;
				maxLevel++;
			}
				
			gfx.render();
			sys.wait(20);
			sys.processEvents();
		}
		Download source code
		Download executable for Windows
				
The von Koch snowflake
 
				
		Vec2f   left (SCREEN_WIDTH/2 - SCREEN_WIDTH/4, (SCREEN_HEIGHT*5)/18);
		Vec2f   right(SCREEN_WIDTH/2 + SCREEN_WIDTH/4, (SCREEN_HEIGHT*5)/18);
		Vec2f   v = right - left;
		v.rotate(DEG_TO_RAD(60));
		Vec2f   bottom = left + v;
				
		if (cnt == 0)
		{
			gfx.clearScreen(Color(0, 0, 0, SDL_ALPHA_OPAQUE));
			vonKoch(left,   right,  0);
			vonKoch(right,  bottom, 0);
			vonKoch(bottom, left,   0);
		}
		Download source code
		Download executable for Windows
				 
				
The Cesàro fractal
 
				
		Vec2f   n[3];
		Vec2f   v12 = p2 - p1;
		n[0] = p1 + v12 * 0.427f;
		n[2] = p2 - v12 * 0.427f;
		Vec2f   v = n[0] - p1;
		v.rotate(DEG_TO_RAD(-80));
		n[1] = n[0] + v;
		vonKoch(  p1, n[0], level + 1);
		vonKoch(n[0], n[1], level + 1);
		vonKoch(n[1], n[2], level + 1);
		vonKoch(n[2],   p2, level + 1);
		Download source code
		Download executable for Windows
				
 
				
The squares pyramid
 
				
		Vec2f   n[3];
		Vec2f   v12 = p2 - p1;
		n[0] = p1 + v12 / 2;
		n[2] = n[0];
		Vec2f   v = n[0] - p1;
		v.rotate(DEG_TO_RAD(-90));
		n[1] = n[0] + v;
		Download source code
		Download executable for Windows
				
 
				
The Cantor set
 
				
		Vec2f   n[2];
		Vec2f   v12 = p2 - p1;
		n[0] = p1 + v12 * 1 / 3;
		n[1] = p1 + v12 * 2 / 3;
		vonKoch(  p1, n[0], level + 1);
		vonKoch(n[1],   p2, level + 1);
		Download source code
		Download executable for Windows
				
 
				
The quadratic von Koch
 
				
		Vec2f   n[7];
		Vec2f   v  = (p2 - p1) / 4;
		Vec2f   v2 = v;
		v2.rotate(DEG_TO_RAD(-90));
		n[0] = p1 + v;
		n[6] = p2 - v;
		n[1] = n[0] + v2;
		n[5] = n[6] - v2;
		n[2] = n[1] + v;
		n[4] = n[5] - v;
		n[3] = (n[2] + n[4]) / 2;
		vonKoch(  p1, n[0], level + 1);
		vonKoch(n[0], n[1], level + 1);
		vonKoch(n[1], n[2], level + 1);
		vonKoch(n[2], n[3], level + 1);
		vonKoch(n[3], n[4], level + 1);
		vonKoch(n[4], n[5], level + 1);
		vonKoch(n[5], n[6], level + 1);
		vonKoch(n[6],   p2, level + 1);
		Download source code
		Download executable for Windows
				
 
				
The quadratic island
		Vec2f   p[4];
		p[0] = Vec2f(SCREEN_WIDTH/2 - SCREEN_HEIGHT/4,  SCREEN_HEIGHT   /4);
		p[1] = Vec2f(SCREEN_WIDTH/2 + SCREEN_HEIGHT/4,  SCREEN_HEIGHT   /4);
		p[3] = Vec2f(SCREEN_WIDTH/2 - SCREEN_HEIGHT/4, (SCREEN_HEIGHT*3)/4);
		p[2] = Vec2f(SCREEN_WIDTH/2 + SCREEN_HEIGHT/4, (SCREEN_HEIGHT*3)/4);
		[...]
		for (int i = 0; i < 4; i++)
		{
			int n = (i + 1) %4;
			vonKoch(p[i], p[n], 0);
		}
		Download source code
		Download executable for Windows
				 
				
The "C" curve
 
				
		Vec2f v12 = p2 - p1;
		v12 /= sqrt(2);
		v12.rotate(DEG_TO_RAD(-45));
		Vec2f n = p1 + v12;
		vonKoch(p1,  n, level + 1);
		vonKoch( n, p2, level + 1);
		Download source code
		Download executable for Windows
				
 
				
The Peano-like curve
 
				
		Vec2f   n[8];
		Vec2f   v12  = (p2 - p1) * 4.0f / 15.0f;
		Vec2f   v12b = v12;
		Vec2f   v12c = v12;
		v12b.rotate(DEG_TO_RAD(-80));
		v12c.rotate(DEG_TO_RAD(80));
		n[0] = p1 + v12;
		n[7] = p2 - v12;
		n[1] = n[0] + v12b;
		n[6] = n[7] - v12b;
		n[2] = n[1] + v12;
		n[5] = n[6] - v12;
		n[3] = n[2] + v12c;
		n[4] = n[5] - v12c;
		vonKoch(  p1, n[0], level + 1);
		vonKoch(n[0], n[1], level + 1);
		vonKoch(n[1], n[2], level + 1);
		vonKoch(n[2], n[3], level + 1);
		vonKoch(n[3], n[4], level + 1);
		vonKoch(n[4], n[5], level + 1);
		vonKoch(n[5], n[6], level + 1);
		vonKoch(n[6], n[7], level + 1);
		vonKoch(n[7],   p2, level + 1);
		Download source code
		Download executable for Windows
				
