/** This sketch illustrates how to use recursion to draw various pictures. These pictures range from a bulls eye, to a staircase fractal, to the t-square fractal */ // The background red, green, blue values final int BACK_R = 100; final int BACK_G = 155; final int BACK_B = 200; // Values used during recursion final int MIN_WIDTH = 10; final int OFFSET = 50; final int BLACK = 0; final int WHITE = 255; // Starting values for recursion final int BULLS_X = 250; final int BULLS_Y = 250; final int STAIR_X = 50; final int STAIR_Y = 250; final int TSQUARE_X = 150; final int TSQUARE_Y = 150; final int SIZE = 200; void setup() { size(500, 500); } void draw() { background(BACK_R, BACK_G, BACK_B); //bullsEyeRecursive(BULLS_X, BULLS_Y, SIZE, SIZE, BLACK); //stairCaseRecursive(STAIR_X, STAIR_Y, SIZE, SIZE); fill(BLACK); tSquare(TSQUARE_X, TSQUARE_Y, SIZE); } /*------------------------------------------ RECURSIVE METHODS *------------------------------------------*/ /** This function uses recursion to draw a bulls eye @param x x-coordinate of the center of the bullseye @param y y-coordinate of the center of the bullseye @param ringWidth The width of the circle to be drawn @param ringHeight The height of the circle to be drawn @param ringColor The color of the circle to be drawn */ void bullsEyeRecursive(int x, int y, int ringWidth, int ringHeight, int ringColor) { // the basecase if (ringWidth < MIN_WIDTH) { return; } fill(ringColor); ellipse(x, y, ringWidth, ringHeight); // the recursive call bullsEyeRecursive(x, y, ringWidth-OFFSET, ringHeight-OFFSET, flipColor(ringColor)); System.out.println("ringWidth: " + ringWidth); } /** This function also uses recursion to draw a bulls eye but the base case is now implicit. */ void anotherBullsEye(int x, int y, int ringWidth, int ringHeight, int ringColor) { // An implicit base case if (ringWidth >= MIN_WIDTH) { fill(ringColor); ellipse(x, y, ringWidth, ringHeight); // the recursive call anotherBullsEye(x, y, ringWidth-OFFSET, ringHeight-OFFSET, flipColor(ringColor)); } } /** This function uses recursion to draw a staircase of boxes @param x x-coordinate of the upper-left corner of the box @param y y-coordinate of the upper-left corner of the box @param boxWidth The width of the box @param boxHeight The height of the box */ void stairCaseRecursive(int x, int y, int boxWidth, int boxHeight) { // the base case if (boxWidth < MIN_WIDTH) { return; } stroke(BLACK); fill(WHITE); rect(x, y, boxWidth, boxHeight); // here, we make 2 recursive calls int newWidth = boxWidth/2; int newHeight = boxHeight/2; stairCaseRecursive(x, y-newHeight, newWidth, newHeight); // the box above stairCaseRecursive(x+boxWidth, y+boxHeight - newHeight, newWidth, newHeight); // the box to the right } /** This function draws a T-square fractal @param x x-coordinate of the upper-left corner of the box @param y y-coordinate of the upper-left corner of the box @param boxWidth The width of the box */ void tSquare(int x, int y, int boxWidth) { if (boxWidth < MIN_WIDTH) { return; } rect(x, y, boxWidth, boxWidth); // here we make s: one for each smaller box int quarterW = boxWidth/4; tSquare(x-quarterW, y-quarterW, boxWidth/2); tSquare(x+boxWidth-quarterW, y-quarterW, boxWidth/2); tSquare(x-quarterW, y+boxWidth-quarterW, boxWidth/2); tSquare(x+boxWidth-quarterW, y+boxWidth-quarterW, boxWidth/2); } /*------------------------------------------ HELPER METHODS *------------------------------------------*/ /** Flips the input back and forth between black and white */ int flipColor(int old) { if (old == BLACK) { return WHITE; } else { return BLACK; } }