Recursion October 5, 2009. Reading Read pp 197-221 in the text.

Slides:



Advertisements
Similar presentations
Basics of motion. Fluid motion Fluid motion is best achieved with floats, try and work out why: floats have decimal places, which provide more resolution,
Advertisements

Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
For(int i = 1; i
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Selected Advanced Topics Chapter 7 and 8
Polymorphism Method overriding Method overloading Dynamic binding 1.
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Value is 1 Let x = FIBO(n-1)Let y = FIBO(n-2)x + y endbegin n > 2 n =1 or 2 RTN for Fibonacci numbers FIBO(n) begin Value is 1 Let y = FIBO(n-2)x + y.
Image Processing … computing with and about data, … where "data" includes the values and relative locations of the colors that make up an image.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/13/08.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
Code Elements and Processing Coordinate System. Code elements: pages Comments: are documentation notes that are ignored by the computer but are.
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
FUNDAMENTALS OF PROGRAMMING SM1204 Semester A 2010/2011.
FUNDAMENTALS OF PROGRAMMING SM1204 Semester A 2010/2011.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
5 CM 4 CM Calculation Area = Length times Width (lw or l x W) Note Length of a rectangle is always the longest side.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
Review Transformations – Scale – Translate – Rotate Combining Transformations – Transformations are cumulative – Rotating about the center of an object.
PROCESSING Methods. Objectives Be able to define methods that return values Be able to define methods that do not return values Be able to use random.
Review Recursion Call Stack. Two-dimensional Arrays Visualized as a grid int[][] grays = {{0, 20, 40}, {60, 80, 100}, {120, 140, 160}, {180, 200, 220}};
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
B. RAMAMURTHY Simulating Motion and Implementing Animation.
CIS 3.5 Lecture 2.2 More programming with "Processing"
2-D Shapes, Color, and simple animation. 7 Basic Shapes Ellipse :: ellipse() Arc :: arc() Line :: line() Point :: point() Rectangle :: rect() Triangle.
Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An object is a "black box" which receives and sends.
Processing TYWu. Where can I download? 2.0b9 Windows 32-bit.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
Welcome back!. Object Oriented Programming – Encapsulation Classes encapsulate state (fields) and behavior (methods) – Polymorphism Signature Polymorphism.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Processing TYWu. Where can I download? 2.0b9 Windows 32-bit.
B. RAMAMURTHY Processing and Programming cse
Review Expressions and operators Iteration – while-loop – for-loop.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
FUNDAMENTALS OF PROGRAMMING SM1204 SEMESTER A 2012.
Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.
Processing Variables. Variables Processing gives us several variables to play with These variables are always being updated and you can assume they have.
Review Random numbers mouseX, mouseY setup() & draw() frameRate(), loop(), noLoop() Mouse and Keyboard interaction Arcs, curves, bézier curves, custom.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
Processing Lecture.3 Loop and animation
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Access Functions and Friend Functions
Chapter 7 Functions.
Code Elements and Processing Coordinate System
For Net Art Lecture 2 J Parker
Exam1 Review CSE113 B.Ramamurthy 11/29/2018 B.Ramamurthy.
Chapter 5, Conditionals Brief Notes
Code Elements and Processing Coordinate System
What employers and colleges expect you to have.
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Recursive GCD Demo public class Euclid {
class PrintOnetoTen { public static void main(String args[]) {
2 types of scale factor problems
Exam1 Review CSE113 B.Ramamurthy 4/17/2019 B.Ramamurthy.
Chapter 4, Variables Brief Notes
Recursion Method calling itself (circular definition)
Trigonometry & Random March 2, 2010.
How About Some PI? Trigonometry Feb 18,2009.
Continuous & Random September 21, 2009.
Presentation transcript:

Recursion October 5, 2009

Reading Read pp in the text.

Fractals Self replicating functions

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".

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.

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

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 }

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

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

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.

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

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

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

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.