/* CSC480Recursion2Ddemo, D. Parson, Spring 2020 */ void setup() { fullScreen(P2D); // size(1000, 750, P2D); background(255); // RGB color model } int lastdepth = -1, nextdepth = 0 ; void draw() { if (nextdepth != lastdepth) { push(); stroke(0); strokeWeight(1); shapeCCurveA(width/2, 3*height/4, height/2, nextdepth); //shapeDragonCurve(width/2, 3*height/4, height/2, nextdepth, 1); // shapeTree(width/2, 3*height/4, height/2, nextdepth); pop(); lastdepth = nextdepth ; } } /* draw a vertical C Curve of length, depth levels of recursion, where x1,y1 is starting point coordinates. Do not scale stroke. */ void shapeCCurveA(int x1, int y1, int length, int depth) { push(); translate(x1, y1); if (depth <= 0) { // base case line(0, 0, 0, -length); // go up } else { // TWO_PI is 360 degrees, PI 180, HALF_PI 90, so this is 45 degrees. // The angle turning back is -90, it joins up with another 45 degress. // Hypotenuse is our original length, so: // cos(QUARTER_PI) = adjacent / length // cos(QUARTER_PI) * length = adjacent float adjacent = cos(QUARTER_PI) * length ; rotate(-QUARTER_PI); // counterclockwise shapeCCurveA(0, 0, round(adjacent), depth-1); translate(0, -adjacent); rotate(HALF_PI); // clockwise shapeCCurveA(0, 0, round(adjacent), depth-1); rotate(-QUARTER_PI); // counterclockwise } pop(); } /* Same as shapeCCurveA, but scale stroke. */ void shapeCCurveB(int x1, int y1, int length, int depth) { pushMatrix(); translate(x1, y1); if (depth <= 0) { // base case line(0, 0, 0, -length); // go up } else { // TWO_PI is 360 degrees, PI 180, HALF_PI 90, so this is 45 degrees. // The angle turning back is -90, it joins up with another 45 degress. // Hypotenuse is our original length, so: // cos(QUARTER_PI) = adjacent / length // cos(QUARTER_PI) * length = adjacent float adjacent = cos(QUARTER_PI) * length ; scale(adjacent / length); // scale to ratio, keep saying "length" rotate(-QUARTER_PI); // counterclockwise shapeCCurveA(0, 0, length, depth-1); translate(0, -length); rotate(HALF_PI); // clockwise shapeCCurveA(0, 0, length, depth-1); rotate(-QUARTER_PI); // counterclockwise } popMatrix(); } /* Like C Curve but rotate opposite dir on the first half of each recursive leg */ void shapeDragonCurve(int x1, int y1, int length, int depth, int dir) { push(); translate(x1, y1); if (depth <= 0) { // base case line(0, 0, 0, -length); // go up } else { // TWO_PI is 360 degrees, PI 180, HALF_PI 90, so this is 45 degrees. // The angle turning back is -90, it joins up with another 45 degress. // Hypotenuse is our original length, so: // cos(QUARTER_PI) = adjacent / length // cos(QUARTER_PI) * length = adjacent float adjacent = cos(QUARTER_PI) * length ; rotate(-dir * QUARTER_PI); // counterclockwise shapeDragonCurve(0, 0, round(adjacent), depth-1, -1); translate(0, -adjacent); rotate(dir * HALF_PI); // clockwise shapeDragonCurve(0, 0, round(adjacent), depth-1, 1); rotate(-dir * QUARTER_PI); // counterclockwise } pop(); } void keyPressed() { if (key >= '0' && key <= '9') { nextdepth = key - '0' ; // distance from character '0' } else if (key >= 'a' && key <= 'j') { // 10 to 19 nextdepth = key - 'a' + 10 ; } else if (key == 'C') { // for clear background(255); } } /* draw a vertical tree of length, depth levels of recursion, where x1,y1 is starting point coordinates. Do not scale stroke. */ void shapeTree(int x1, int y1, int length, int depth) { push(); translate(x1, y1); if (depth <= 0) { line(0, 0, 0, -length); // go up // base case // line(0, 0, 0, -length); // go up } else { // TWO_PI is 360 degrees, PI 180, HALF_PI 90, so this is 45 degrees. // The angle turning back is -90, it joins up with another 45 degress. // Hypotenuse is our original length, so: // cos(QUARTER_PI) = adjacent / length // cos(QUARTER_PI) * length = adjacent float adjacent = cos(QUARTER_PI) * length / 2.0 ; // Go halfway up, make side branches, then do it again. for (int i = 0 ; i < 2 ; i++) { shapeTree(0, 0, length/2, depth-1); translate(0, -length/2); rotate(-QUARTER_PI); // counterclockwise shapeTree(0, 0, round(adjacent), depth-1); rotate(HALF_PI); // clockwise shapeTree(0, 0, round(adjacent), depth-1); rotate(-QUARTER_PI); // counterclockwise } } pop(); }