0% found this document useful (0 votes)
2K views45 pages

16 Recursion

The document discusses recursion and fractal patterns generated using recursion. It provides examples of recursive functions to draw nested shapes like circles, boxes, and a golden spiral. The recursive functions divide and draw the shapes at smaller sizes within prior shapes to generate the fractal patterns.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views45 pages

16 Recursion

The document discusses recursion and fractal patterns generated using recursion. It provides examples of recursive functions to draw nested shapes like circles, boxes, and a golden spiral. The recursive functions divide and draw the shapes at smaller sizes within prior shapes to generate the fractal patterns.

Uploaded by

Gene Kao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Processing

16
Recursion

Recursion
Recursion

1.
2.
3.
4.
void A () {
A;}
void A () {
B;}

void B () {
C;}

void C () {
A;}
void A () {
B;}

void B () {
A;}
void

Debug

Divide and Conquer
void setup () {
factorial(5);
factorial(10);
factorial(0);
factorial(1);
}

void factorial (int n) {
int result = 1;
if (n == 0) {println(result);} else {
for (int i = 1; i <= n; i++) {
result = result * i;
}
}
println(result);
}
void setup () {
fact(5,1);
fact(10,1);
fact(0,1);
fact(1,1);
}

void fact (int n, int result) {
if (n == 0) {println(result);} else {
result = result * n;
fact(n-1,result);
}
}

fact(5,1)
fact(4,5)
fact(3,20)
fact(2,60)
fact(1,120)
fact(0,120)
void setup () {
int m = factorial(5);
println(m);
m = factorial(10);
println(m);
m = factorial(0);
println(m);
m = factorial(1);
println(m);
}

int factorial (int n) {
int result = 1;
if (n == 0) {return result;} else {
for (int i = 1; i <= n; i++) {
result = result * i;
}
}
return result;
}
void setup () {
int m = fact(5);
println(m);
m = fact(10);
println(m);
m = fact(0);
println(m);
m = fact(1);
println(m);
}

int fact (int n) {
int result = 1;
if (n == 0) {return result;} else {
result = n * fact(n-1);
}
return result;
}
size(400,400);

int x = 40;
for (int n = 12; n >= 0; n -= 1) {
line(x, 100, x, 300);
x += 20;
}
void setup() {
size(400,400);
drawLines(40, 12);
}

void drawLines(int x, int n) {
line(x,100, x, 300);
if (n > 0) {drawLines (x+20, n-1);}
}



40,12
60,11
80,10

300,0
size(400,400);

int x = 40;
for (int n = 12; n >= 0; n -= 1) {
line(x, 100, x, 300);
x += 20;
}
void setup() {
size(400,400);
drawLines(40, 12);
}

void drawLines(int x, int n) {
line(x,100, x, 300);
if (n > 0) {drawLines (x+20, n-1);}
}
void setup() {
size(400,400);
drawLines(40, 12);
}

void drawLines(int x, int n) {
line(x,100, x, 300);
if (n > 0) {drawLines (x+20, n-1);}
}

size(400,400);

int x = 40;
for (int n = 12; n >= 0; n -= 1) {
line(x, 100, x, 300);
x += 20;
}

void setup() {
size(400,400);
drawLines(40, 12);
}

void drawLines(int x, int n) {
line(x,100+x, x, 300-x);
if (n > 0) {drawLines (x+20, n-1);}
}
void setup () {
size(400,400);
background(255);
ellipseMode(CENTER);
strokeWeight(2);
smooth();
noFill();
noLoop();
}

void draw () {
translate(width/2,height/2);
two_circles(0,width,6);
}

void two_circles (float x, float d, int step) {
ellipse(x,0,d,d);
if (step > 1) {
two_circles(x-d/4,d/2,step-1);
two_circles(x+d/4,d/2,step-1);
}
}
void setup() {
size(400,400);
background(255);
ellipseMode(CENTER);
smooth();
noFill();
noLoop();
}
void draw() {
translate(width/2,height/2);
ellipse(0,0,width,height);
int d = width;
two_circles(0,d);
}

void two_circles(int x, int d) {
ellipse(x+d/4,0,d/2,d/2);
ellipse(x-d/4,0,d/2,d/2);
}

void draw () {
translate(width/2,height/2);
two_circles(0,width,6);
}

void two_circles (float x, float d, int step) {
ellipse(x,0,d,d);
if (step > 1) {
two_circles(x-d/4,d/2,step-1);
two_circles(x+d/4,d/2,step-1);
}
}
void setup () {
size(400,400);
background(255);
ellipseMode(CENTER);
strokeWeight(2);
smooth();
noFill();
noLoop();
}

void draw () {
translate(width/2,height/2);
two_circles(0,0,width,6,0);
}

void two_circles (float x, float y, float d, int step, int f) {
fill(f);
ellipse(x,y,d,d);
f += 44;
if (step > 1) {
two_circles(x-d/4,y,d/2,step-1,f);
two_circles(x+d/4,y,d/2,step-1,f);
}
}
void setup () {
size(400,400);
background(255);
ellipseMode(CENTER);
smooth();
noFill();
noLoop();
}

void draw () {
translate(width/2,height/2);
four_circles(0,0,width,5);
}

void four_circles (float x, float y, float d, int
step) {
ellipse(x,y,d,d);
if (step > 1) {
four_circles(x-d/4,y,d/2,step-1);
four_circles(x+d/4,y,d/2,step-1);
four_circles(x,y-d/4,d/2,step-1);
four_circles(x,y+d/4,d/2,step-1);
}
}
void draw () {
translate(width/2,height/2);
four_circles(0,0,width,5);
}

void four_circles (float x, float y, float d, int step) {
ellipse(x,y,d,d);
if (step > 1) {
four_circles(x-d/4,y,d/2,step-1);
four_circles(x+d/4,y,d/2,step-1);
four_circles(x,y-d/4,d/2,step-1);
four_circles(x,y+d/4,d/2,step-1);
}
}
void setup () {
size(400,400);
background(255);
ellipseMode(CENTER);
smooth();
noFill();
}

void draw () {
background(255);
translate(width/2,height/2);
int level = int(map(mouseX,0,width,0,8));
four_circles(0,0,width,level);
}

void four_circles (float x, float y, float d, int
step) {
ellipse(x,y,d,d);
if (step > 1) {
four_circles(x-d/4,y,d/2,step-1);
four_circles(x+d/4,y,d/2,step-1);
four_circles(x,y-d/4,d/2,step-1);
four_circles(x,y+d/4,d/2,step-1);
}
}
void setup() {
size(400,400);
rectMode(CENTER);
translate(width/2,height/2);
drawBoxes(0, 0, 120, 5);
}

void drawBoxes(float x, float y, float len, int n) {
rect(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*0.75,y+len*0.75,len/2,n-1);
}
}
void setup() {
size(400,400);
background(30);
rectMode(CENTER);
noStroke();
translate(width/2,height/2);
drawBoxes(0, 0, 120, 8);
}

void drawBoxes(float x, float y, int len, int n) {
rect(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*0.75,y+len*0.75,len/2, n-1);
drawBoxes(-(x+len*0.75),y+len*0.75,len/2, n-1);}
}
void setup() {
size(400,400);
background(30);
rectMode(CENTER);
noStroke();
translate(width/2,height/2);
drawBoxes(0, 0, 120, 8);
}

void drawBoxes(float x, float y, int len, int n) {
rect(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*0.75,y-len*0.75,len/2, n-1);
drawBoxes(-(x+len*0.75),y-len*0.75,len/2, n-1);}
}
void setup() {
size(400,400);
background(30);
rectMode(CENTER);
noStroke();
translate(width/2,height/2);
drawBoxes(0, 0, 120,5);
}

void drawBoxes(float x, float y, int len, int n) {
rect(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*0.75,y+len*0.75,len/2, n-1);
drawBoxes(-(x+len*0.75),y+len*0.75,len/2, n-1);
drawBoxes(x+len*0.75,y-len*0.75,len/2, n-1);
drawBoxes(-(x+len*0.75),y-len*0.75,len/2, n-1);}
}
3 7
void setup() {
size(400,400);
background(30);
rectMode(CENTER);
noStroke();
translate(width/2,height);
drawBoxes(0, 0, 120, 8);
}

void drawBoxes(float x, float y, int len, int n) {
rect(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*0.75,y-len*0.75,len/2, n-1);
drawBoxes(-(x+len*0.75),y-len*0.75,len/2, n-1);}
}
void setup() {
size(400,400);
background(30);
rectMode(CENTER);
noStroke();
translate(width/2,height-60);
drawBoxes(0, 0, 120, 8);
}

void drawBoxes(float x, float y, int len, int n) {
rect(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*0.75,y-len*0.75,len/2, n-1);
drawBoxes(-(x+len*0.75),y-len*0.75,len/2, n-1);}
}
float s = 0.85;

void setup() {
size(400,400);
background(30);
rectMode(CENTER);
noStroke();
translate(width/2,height/2);
drawBoxes(0, 0, 120,5);
}

void drawBoxes(float x, float y, int len, int n) {
rect(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*s,y+len*s,len/2, n-1);
drawBoxes(-(x+len*s),y+len*s,len/2, n-1);
drawBoxes(x+len*s,y-len*s,len/2, n-1);
drawBoxes(-(x+len*s),y-len*s,len/2, n-1);}
}
void setup() {
size(400,400);
background(30);
ellipseMode(CENTER);
noStroke();
smooth();
translate(width/2,height/2);
drawBoxes(0, 0, 200,5);
}

void drawBoxes(float x, float y, int len, int n) {
ellipse(x, y, len, len);
if (n > 0) {
drawBoxes(x+len*0.5,y+len*0.5,len/2, n-1);
drawBoxes(-(x+len*0.5),y+len*0.5,len/2, n-1);
drawBoxes(x+len*0.5,y-len*0.5,len/2, n-1);
drawBoxes(-(x+len*0.5),y-len*0.5,len/2, n-1);}
}
size(400,400);
smooth();
int len = 100;

translate(width/2,height/2);

rect(0,0,len,len);
line(0,0,len,len);
translate(len,len);
rotate(PI/2);

rect(0,0,len/2,len/2);
line(0,0,len/2,len/2);
translate(len/2,len/2);
rotate(PI/2);

rect(0,0,len/4,len/4);
line(0,0,len/4,len/4);
void setup() {
size(200,323);
rectMode(CORNER);
smooth();
noLoop();
}

void draw () {
golden(200,10);
}

void golden(float len,int n) {
rect(0,0,len,len);
line(0,0,len,len);
translate(len,len);
rotate(PI/2);
if (n > 0) {golden(len/1.6181, n-1);} //1.61803399...
}
void setup() {
size(200,323);
rectMode(CORNER);
smooth();
noLoop();
}

void draw () {
golden(200,10);
}

void golden(float len,int n) {
rect(0,0,len,len);
//line(0,0,len,len);
translate(len,len);
rotate(PI/2);
if (n > 0) {golden(len/1.6181, n-1);} //1.61803399...
}
void setup() {
size(200,323);
rectMode(CORNER);
noFill();
smooth();
noLoop();
}

void draw () {
golden(200,10);
}

void golden(float len,int n) {
rect(0,0,len,len);
pushMatrix();
translate(0,len);
arc(0,0,len*2,len*2,-PI/2,0);
popMatrix();
translate(len,len);
rotate(PI/2);
if (n > 0) {golden(len/1.6181, n-1);} //1.61803399...
}
void setup() {
size(600,600);
rectMode(CORNER);
noFill();
smooth();
noLoop();
}

void draw () {
translate(width/4,height/2);
golden(1,12);
}

void golden(float len,int n) {
rect(0,0,len,len);
pushMatrix();
translate(0,len);
arc(0,0,len*2,len*2,-PI/2,0);
popMatrix();
translate(len,len);
rotate(PI/2);
if (n > 0) {golden(len*1.6181, n-1);}
}
void setup() {
size(600,600);
rectMode(CORNER);
noFill();
smooth();
noLoop();
}

void draw () {
translate(width/4,height/2);
golden(1,12);
}

void golden(float len,int n) {
rect(0,0,len,len);
//pushMatrix();
//translate(0,len);
//arc(0,0,len*2,len*2,-PI/2,0);
//popMatrix();
translate(len,len);
rotate(PI/2);
if (n > 0) {golden(len*1.6181, n-1);}
}
void setup() {
size(600,600);
rectMode(CORNER);
noFill();
smooth();
noLoop();
}

void draw () {
translate(width/7,height/2);
golden(10,6);
}

void golden(float len,int n) {
rect(0,0,len,len);
translate(len,len);
rotate(PI/2);
if ((n % 2) == 1) {scale(1.618,1);}
else {scale(1,1.618);}
if (n > 0) {golden(len*1.6181, n-1);}}

int p1x = 50; int p1y = 0;
int p2x = 0; int p2y = 100;
int p3x = 100; int p3y = 100;
float x = 100.0; float y = 50.0;

void setup () {
size(100,100);
background(255);
}

void draw() {
int i = int(random(3));
if (i == 1) {x = (x + p1x) / 2.0; y = (y + p1y) / 2.0;}
if (i == 2) {x = (x + p2x) / 2.0; y = (y + p2y) / 2.0;}
if (i == 0) {x = (x + p3x) / 2.0; y = (y + p3y) / 2.0;}
point(x,y);
}
int p1x = 50; int p1y = 0;
int p2x = 0; int p2y = 100;
int p3x = 100; int p3y = 100;
float p0x = 100.0; float p0y = 50.0;

void setup () {
size(100,100);
background(255);
Serpinsky(p0x, p0y, 0);
}

void Serpinsky(float x, float y, int n) {
int i = int(random(3));
if (i == 1) {x = (x + p1x) / 2.0; y = (y + p1y) / 2.0;}
if (i == 2) {x = (x + p2x) / 2.0; y = (y + p2y) / 2.0;}
if (i == 0) {x = (x + p3x) / 2.0; y = (y + p3y) / 2.0;}
point(x,y);
if (n != 4000) {Serpinsky(x,y,n + 1);}
}
float angle = 8;

void setup() {
size(400,400);
translate(width/2,height);
drawLines(0,0,40,90,10);
}

void drawLines(float x1,float y1,float len,float an,int n) {
float x2 = x1 + cos(radians(an)) * len;
float y2 = y1 - sin(radians(an)) * len;
line(x1, y1, x2, y2);
if (n > 0) {drawLines(x2,y2,len*0.95,an-angle,n-1);
drawLines(x2,y2,len*0.95,an+angle,n-1);}
}
float angle = 45;
float first_line = 200;

void setup() {
size(400,400);
translate(width/2,height);
line(0,0,0,-first_line);
drawLines(0,-first_line,40,90,5);
}

void drawLines(float x1,float y1,float len,float an,int n) {
float x2 = x1 + cos(radians(an)) * len;
float y2 = y1 - sin(radians(an)) * len;
line(x1, y1, x2, y2);
if (n > 0) {drawLines(x2,y2,len*0.9,angle,n-1);
drawLines(x2,y2,len*0.9,90+angle,n-1);}
}
float angle = 30;
float first_line = 180;

void setup() {
size(400,400);
translate(width/2,height);
line(0,0,0,-first_line);
drawLines(0,-first_line,40,90,8);
}

void drawLines(float x1,float y1,float len,float an,int n) {
float x2 = x1 + cos(radians(an)) * len;
float y2 = y1 - sin(radians(an)) * len;
line(x1, y1, x2, y2);
if (n > 0) {drawLines(x2,y2,len*0.9,angle,n-1);
drawLines(x2,y2,len*0.9,90+angle,n-1);}
}
Ch. 22
P201-204

You might also like