/** 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 */ int minWidth = 10; // When the width goes below this threshold, we stop drawing void setup() { size(500, 500); } void draw() { background(100, 155, 200); //bullsEyeRecursive(250, 250, 200, 200, 0); // stairCaseRecursive(50, 250, 200, 200); //fill(0); //tSquare(150, 150, 200); } /*------------------------------------------ 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 < minWidth) { return; } fill(ringColor); ellipse(x, y, ringWidth, ringHeight); // the recursive call bullsEyeRecursive(x, y, ringWidth-50, ringHeight-50, flipColor(ringColor)); } /** This function also uses recursion to draw a bulls eye but the base case has been negated. */ void anotherBullsEye(int x, int y, int ringWidth, int ringHeight, int ringColor) { // we negated the base case if (ringWidth >= minWidth) { fill(ringColor); ellipse(x, y, ringWidth, ringHeight); // the recursive call anotherBullsEye(x, y, ringWidth-50, ringHeight-50, 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 < minWidth) { return; } stroke(0); fill(255); 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 < minWidth) { return; } rect(x, y, boxWidth, boxWidth); // here we make 4 recursive calls: one for each smaller box int newWidth = boxWidth/2; tSquare(x-newWidth/2, y-newWidth/2, newWidth); tSquare(x+boxWidth-newWidth/2, y-newWidth/2, newWidth); tSquare(x-newWidth/2, y+boxWidth-newWidth/2, newWidth); tSquare(x+boxWidth-newWidth/2, y+boxWidth-newWidth/2, newWidth); } /*------------------------------------------ HELPER METHODS *------------------------------------------*/ /** Flips the input back and forth between 0 and 255 */ int flipColor(int old) { if (old == 0) { return 255; } else { return 0; } }