Download presentation

Presentation is loading. Please wait.

Published byMaximilian Buoy Modified about 1 year ago

1
Recursion October 5, 2009

2
Reading Read pp in the text.

3
Fractals Self replicating functions

4
Recursive Definition Recursive definition of Recursion: If you don’t understand it, See "Recursion". Recursion is different from a circular definition, because recursion always has a base case which when reached, terminates the recursion. Circular definition of Recursion: See "Recursion".

5
Functions and Recursion Functions can call another function –We saw in last week’s class that you can call a function from the draw() function. Today we learn that a function can call itself -- (Exceptions: not draw() or setup() ) A function calling itself is an example of recursion. In order to end the recursion, there must be a base case.

6
static int radius; void setup() { size(300,300); smooth(); radius = width; drawCircle(width/2, height/2); stroke(0); } void drawCircle (int x, int y) { println("r = " + radius); if (radius<2) { return; //BASE CASE } else { //println("r = " + radius); ellipse(x, y, radius, radius); radius/=2; drawCircle(x,y); }

7
Can You Make the Sketch More Interesting? What about making the the circles not quite concentric? void drawCircle (int x, int y) { println("r = " + radius); if (radius>2) { //println("r = " + radius); fill(125,200,255, 75); ellipse(x, y, radius, radius); radius/=2; drawCircle(x+(int)random(radius/4),y+(int)random(radius/4)); }//implied else is the base case }

8
static int radius; void setup() { size(300,300); smooth(); drawCircle(width/2, height/2, width/2, height/2); stroke(0); } void drawCircle (float x, float y, float w, float h) { println("w = " + w); if (w<2.0) { //base case return; } else { println("w = " + w); ellipse(x, y, w, h); drawCircle(x-w/2,y, w/2, h/2); drawCircle(x+w/2,y, w/2, h/2); }

9
Edit it! Modify the preceding program so there is no fill. Change the size to 500 x 500 pixels. –Change the end case to be w<5. Do you like that better? Try a larger number. Can you replicate the existing design vertically, rather than horizontally? –Add those two lines to the program

10
Recursion + Experimentation + Random = fun! The following program illustrates how a simple example program can evolve over a basketball halftime into an interesting design. The program divides the screen into 4 parts in a very regular fashion. It became more interesting when randomness in terms of color, quadrant divided and rectangle placement was added.

11
int d1; int d2; void setup() { size(600, 600); stroke(255); background(0); } void draw() { frameRate(10); fill(random(100, 255), random(100, 255), random(100, 255), random(20- 40)); fillScreen(width, height, 0, 0); } void fillScreen(int d1, int d2, int x1, int y1) { if (d1 > 5) { if (((int)(random(0,2))%2) == 0) { /*fillScreen(d1 / 2, d2 / 2, (int)random(x1-d1/2, x1+d1/2), (int)random(y1-d2/2, y1+d2/2)); uncomment this and comment the next line for more abstract version*/

12
fillScreen(d1 / 2, d2 / 2, x1, y1); } else { fillScreen(d1 / 2, d2 / 2, x1+d1/2, y1); } else { if (((int)(random(0,2))%2) == 0) { fillScreen(d1 / 2, d2 / 2, x1, y1+d2/2); } else { fillScreen(d1 / 2, d2 / 2, x1+d1/2, y1+d2/2); }

13
stroke(100,random(100,200), random(100,150),random(100, 150)); int increment = (int)(random(1,2)+.5)%2; if (increment == 0) { rect(x1, y1, d1, d2); rect(x1, y1+d2, d1, d2); } else { rect(x1, y1, d2, d1); rect(x1+d2, y1, d2, d1); }

14
Exercise Begin with one of the sample programs from this powerpoint or from your text and experiment with it to produce an aesthetically pleasing design that doesn’t look derivative.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google