Review Transformations – Scale – Translate – Rotate Combining Transformations – Transformations are cumulative – Rotating about the center of an object.

Slides:



Advertisements
Similar presentations
Creative Computing. \\ aims By the end of the session you will be able to: 1.Move objects around 2.Write simple interactive programs 3.Use the mouse position.
Advertisements

1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: Parameter.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 8: Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
M180: Data Structures & Algorithms in Java
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm.
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Review Expressions and operators Iteration – while-loop – for-loop.
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.
Program Development and Design Using C++, Third Edition
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Recursion To understand recursion, you first have to understand recursion.
Recursion Function calling itself
Recursion CENG 707.
Chapter Topics Chapter 16 discusses the following main topics:
COMP 51 Week Fourteen Recursion.
Topic 6 Recursion.
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Chapter 15 Recursive Algorithms
Computers as an Expressive Medium
CS 211 Object Oriented Programming
Chapter 15 Recursion.
Introduction to Recursion
Java 4/4/2017 Recursion.
Introduction to Computer Science - Alice
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm Analysis (for Divide-and-Conquer problems)
Recursion Data Structures.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Module 1-10: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Main() { int fact; fact = Factorial(4); } main fact.
Handout-16 Recursion Overview
Recursion.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

Review Transformations – Scale – Translate – Rotate Combining Transformations – Transformations are cumulative – Rotating about the center of an object Animating with transformations

Factorial The factorial of a positive integer N is computed as the product of N with all positive integers less than or equal to N. 4! = 4  3  2  1 = 24 30! = 30  29  …  2  1 =

1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 6.int factorial(int N) { 7. int F = 1; for( int i=N; i>=1; i--) { 10. F = F * i; 11. } return F; 14.} Factorial - Iterative Implementation Trace it.

5! = 5  4  3  2  1 4! = 4  3  2  1 5! = 5  4! N! = N  (N-1)! Factorial can be defined in terms of itself

1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 6.int factorial(int N) { 7. if (N == 1) { 8. return 1; 9. } else { 10. int F = N * factorial(N-1); 11. return F; 12. } 13.} Factorial – Recursive Implementation Trace it.

Last In First Out (LIFO) Stack of Plates

1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code

1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code

1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code

1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3

1.int factorial(int N=5) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3

1.int factorial(int N=5) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3

1.int factorial(int N=5) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5

1.int factorial(int N=4) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5

1.int factorial(int N=4) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5

1.int factorial(int N=4) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5

1.int factorial(int N=3) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5

1.int factorial(int N=3) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5

1.int factorial(int N=3) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5 factorial() N=3, Line=5

1.int factorial(int N=2) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5 factorial() N=3, Line=5

1.int factorial(int N=2) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5 factorial() N=3, Line=5

1.int factorial(int N=2) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5 factorial() N=3, Line=5 factorial() N=2, Line=5

1.int factorial(int N=1) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5 factorial() N=3, Line=5 factorial() N=2, Line=5

1.int factorial(int N=1) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5 factorial() N=3, Line=5 factorial() N=2, Line=5

1.int factorial(int N=2) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * 1; 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5 factorial() N=3, Line=5

1.int factorial(int N=3) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * 2; 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5 factorial() N=4, Line=5

1.int factorial(int N=4) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * 6; 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3 factorial() N=5, Line=5

1.int factorial(int N=5) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * 24; 6. return F; 7. } 8.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code setup() A=10, Line=3

1.void setup() { 2. int A = 10; 3. int B = 120; 4. println( B ); 5.} 1.void setup() { 2. int A = 10; 3. int B = factorial(5); 4. println( B ); 5.} 1.int factorial(int N) { 2. if (N == 1) { 3. return 1; 4. } else { 5. int F = N * factorial(N-1); 6. return F; 7. } 8.} Call StackExecuting FunctionCompiled Code

The Call Stack keeps track of … 1.all functions that are suspended, in order 2.the point in the function where execution should resume after the invoked subordinate function returns 3.a snapshot of all variables and values within the scope of the suspended function so these can be restored upon continuing execution

What happens if there is no stopping condition, or "base case"?

// Fibonacci sequence // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... void setup() {} void draw() {} void mousePressed() { int f = fibonacci(12); println(f); } // Compute and return the nth Fibonacci number int fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { int f = fibonacci(n-1) + fibonacci(n-2); return f; }

Creating a maze, recursively 1.Start with a rectangular region defined by its upper left and lower right corners 2.Divide the region at a random location through its more narrow dimension 3.Add an opening at a random location 4.Repeat on two rectangular subregions Inspired by

// RecursiveMaze int N = 25; // Grid dimension int gsize = 20; // Grid size int V = 1; // Vertical constant int H = 2; // Horizontal constant void setup() { // Setup sketch size(N*gsize+1, N*gsize+1); noLoop(); background(255); stroke(0); // Kick off the recursive divide // on entire sketch window divide(0,0,N,N); } // Determine the direction for dividing // Stop when too small. int divDir(int r1, int c1, int r2, int c2) { int dr = r2 - r1; // Deltas int dc = c2 - c1; if (dr <= 1 || dc <= 1) // Too small return 0; // No division else if (dr < dc) // Flat and wide return V; // Vertical division else // Tall and narrow return H; // Horizontal div } // Return a random integer in the range int randomInt(int min, int max) { return round(random(min-0.5,max+0.5)); } // Draw a line on a grid segment void gridLine(int r1, int c1, int r2, int c2) { line(r1*gsize, c1*gsize, r2*gsize, c2*gsize); }

// Divide the region given upper left and // lower right grid corner points void divide(int r1, int c1, int r2, int c2) { int cr, rr; // Get divide direction (V, H or 0) int dir = divDir(r1, c1, r2, c2); // Divide in vertical direction if (dir == V) { // Wall and opening locations cr = randomInt(c1+1, c2-1); rr = randomInt(r1, r2-1); // Draw wall gridLine(cr,r1,cr,rr); gridLine(cr,rr+1,cr,r2); // Recursively divide two subregions divide(r1,c1,r2,cr); divide(r1,cr,r2,c2); // Divide in horizontal direction } else if (dir == H) { // Wall and opening locations cr = randomInt(c1, c2-1); rr = randomInt(r1+1, r2-1); // Draw wall gridLine(c1,rr,cr,rr); gridLine(cr+1,rr,c2,rr); // Recursively divide two subregions divide(r1,c1,rr,c2); divide(rr,c1,r2,c2); // No division. We're done. } else { return; }