Skip to content

Instantly share code, notes, and snippets.

@Katharine
Created March 29, 2012 01:55
Show Gist options
  • Save Katharine/2232404 to your computer and use it in GitHub Desktop.
Save Katharine/2232404 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/**
* Decides if a point at a specific location is filled or not.
* @param x is the x coordinate of the point being checked
* @param y is the y coordinate of the point being checked
* @param width is the width of the Sierpinski Carpet being checked
* @param height is the height of the Sierpinski Carpet being checked
* @return 1 if it is to be filled or 0 if it is not
*/
int isSierpinskiCarpetPixelFilled(int x, int y, int width, int height)
{
// base case 1 of 2
if ((x <= 0)||(y <= 0)||(x>=width)||(y>=height)) //top row or left column or out of bounds should be full
{
return 1;
}
{
/*
If the grid was split in 9 parts, what part(x2,y2) would x,y fit into?
*/
int x2 = x * 3 / width; // an integer from 0..2 inclusive
int y2 = y * 3 / height; // an integer from 0..2 inclusive
// base case 2 of 2
if (x2 == 1 && y2 == 1) // if in the center square, it should be empty
return 0;
// general case
/* offset x and y so it becomes bounded by 0..width/3 and 0..height/3
and prepares for recursive call
some offset is added to make sure the parts have all the correct size when
width and height isn't divisible by 3
*/
x -= (x2 * width+2) / 3;
y -= (y2 * height+2) / 3;
width = (width +2-x2)/3;
height = (height+2-y2)/3;
}
return isSierpinskiCarpetPixelFilled(x, y, width, height);
}
system("PAUSE");
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment