0% found this document useful (0 votes)
33 views

14 Recursion 20120518

The document discusses recursion in processing and provides examples of using recursion to draw fractal patterns. Some key points: - Recursion is a technique where a function calls itself repeatedly. It is useful for generating fractals. - Examples are provided to recursively draw: lines extending out from a central point with decreasing length; Sierpinski triangles; boxes within boxes; the Koch snowflake curve; and the golden spiral using recursive rotations and scaling. - Parameters like the length and number of iterations are passed to the recursive functions to control the patterns generated. - Recursion allows complex fractal patterns to be generated with simple recursive functions.

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)
33 views

14 Recursion 20120518

The document discusses recursion in processing and provides examples of using recursion to draw fractal patterns. Some key points: - Recursion is a technique where a function calls itself repeatedly. It is useful for generating fractals. - Examples are provided to recursively draw: lines extending out from a central point with decreasing length; Sierpinski triangles; boxes within boxes; the Koch snowflake curve; and the golden spiral using recursive rotations and scaling. - Parameters like the length and number of iterations are passed to the recursive functions to control the patterns generated. - Recursion allows complex fractal patterns to be generated with simple recursive functions.

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/ 35

Processing

Recursion

14

Recursion

Recursion

1.
2.
3.
4.

void A () {
A;}

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

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

void

Debug

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, x, 300-x);
if (n > 0) {drawLines (x+20, n-1);}
}

int p1x = 50;


int p2x = 0;
int p3x = 100;
float x = 100.0;

int p1y = 0;
int p2y = 100;
int p3y = 100;
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);}
}

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);}
}

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);}}

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
05/18/2012

You might also like