This Sierpinski triangle is created out of rectangles. If you zoom in this becomes self-evident. The algorithm for generating these fractals is very simple. Start with the base case of a rectangle with points {x0, y0, x1, y1}. Then Subdivide that rectangle into 3 sub rectangles each with ½ the width and height. Then arrange those rectangles so that a corner touches 2 other corners. Then repeat the process on the sub rectangles. This triangle was subdivided 10 times.


Here is program itself. To change the number of times subdivided simply modify the ITER_COUNT flag. To run the program have the SDL development library installed and run:

		
gcc -g sier.c  -w -lSDL2 -o build 
./build
		
	


Here's the actual algorithm

	    
void sier(float x0, float y0,
		  float x1, float y1,
		  color col,
		  int current_depth, int max_iter)
{
	float dx = x1-x0;
	float dy = y1-y0;
	
	if(dx > 1 && dy > 1)
	{
	//int[4] foo = {x0, y0, x1, y1};
		float tile_1[4] = {x0+dx/4, y0, x0 + dx*3/4, y0+dy/2};
		float tile_2[4] = {x0, y0+dy/2, x0+dx/2, y1};
		float tile_3[4] = {x0+dx/2, y0+dy/2, x0+dx, y0+dy};
		
		//shrink tile to 1/2 original size
		if(current_depth == max_iter)
		{
			draw_rect((int)tile_1[0], (int)tile_1[1],
					  (int)tile_1[2], (int)tile_1[3],
					  &col);
			draw_rect((int)tile_2[0], (int)tile_2[1],
					  (int)tile_2[2], (int)tile_2[3],
					  &col);
			draw_rect((int)tile_3[0], (int)tile_3[1],
					  (int)tile_3[2], (int)tile_3[3],
					  &col);
		}
		
		else
		{
			int next_depth = current_depth +1;
			sier(tile_1[0], tile_1[1],
				 tile_1[2], tile_1[3],
				 col, next_depth, max_iter);
			sier(tile_2[0], tile_2[1],
				 tile_2[2], tile_2[3],
				 col, next_depth, max_iter);
			sier(tile_3[0], tile_3[1],
				 tile_3[2], tile_3[3],
				 col, next_depth, max_iter);
			
		}
	}
}
	    
	    
sier.c