Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation

Similar presentations


Presentation on theme: "CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation"— Presentation transcript:

1 CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Python – Drawing trees using Turtle and Recursion

2 Drawing Trees Rendering Wilderness Nature reserve 3D computer graphics - nature scene Source:

3 Iteratively -> using for loop ...
… and random module Let’s have a look at the code!

4 Recursively -> using recursion
Let’s have a look at the code!

5 Functions that call themselves
Recursion Functions that call themselves

6 Recursion in the real world
Russian dolls

7 Recursion in the real world
Searching for the word “guffaw” in a dictionary Source:

8 Recursion in the real world
Droste Effect The picture is defined by the picture itself.

9 Recursion in the mathematical world
Multiply two numbers Compute factorials Fractals The Sierpinski triangle A confined recursion of triangles that form a fractal

10 Recursion - Definition
Recursion occurs when an object or a process is defined in terms of itself (or a version of itself)  In mathematics and computer science, a kind of objects or processes exhibit recursive behavior when they can be defined by two properties: A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case Adapted from

11 In the software world … So far, when solving problems, we have achieved repetition (i.e., repeating statements in our code) by using iterative statements -> loops By putting statements we wanted to repeat in a loop

12 Recursive functions Another way of achieving repetition (i.e., repeating statements in our code) is by putting statements we want to repeat in a function and calling the function itself a certain number of times Directly: Indirectly: functionA # recursive call functionA(…) function1 # recursive call function2(…) function 2 # recursive call function1(…)

13 Let’s give it a try! Let’s give our turtle a break and practice recursion with the following simple … Problem Statement Write a Python function that returns the factorial of a number passed as a parameter

14 How to design/implement a recursive function?
Hum… Easy for you to say! We need to think recursively!

15 Example - recursive factorial (slide with animation – to be seen in PowerPoint)
5! 24 120 5 * 4! 6 24 4 * 3! 2 Tada!!! 6 3 * 2! 1 2 2 * 1! 1 Base case When the base case is reached, the function stops calling itself and the execution “recurses back”

16 Observation from Animation
1. Base Case A recursive algorithm must have a base case. 2. Recursive Case A recursive algorithm must change its state and move toward the base case. A recursive algorithm must call itself, recursively.

17 Let’s design the algorithm for our Factorial problem!
1. Base Case When does the factorial algorithm stop? 2. Recursive Case How does the factorial algorithm change and move toward its base case? How does the factorial algorithm call itself?

18 Let’s implement our Factorial function!

19 Review Recursion Demo: Recursively drawing trees in Turtle
Examples of recursion occurring in the real world Examples of recursion occurring in the mathematical world What is recursion Iterative code versus Recursive code How does recursion work Design and implement recursion

20 Next Lecture Lot’s more about recursion
Let’s modify our drawTree( ) recursive function such that it really draws trees!


Download ppt "CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation"

Similar presentations


Ads by Google