CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation

Slides:



Advertisements
Similar presentations
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
Advertisements

New Mexico Computer Science For All
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
ICS103 Programming in C Lecture 11: Recursive Functions
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
Log on and Download from website:
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Visual C++ Programming: Concepts and Projects Chapter 10A: Recursion (Concepts)
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion Topic 5.
Topic: Recursion – Part 2
Topic: Functions – Part 1
Introduction to Recursion
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Topic: Recursion – Part 1
CMPT 120 Topic: Functions – Part 4
Topic: Functions – Part 2
Recursion
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
University of Washington Computer Programming I
Lesson #6 Modular Programming and Functions.
Doing things more than once
Recursion Chapter 11.
JavaScript: Functions Part II
Fundamentals of Programming
Chapter 11 Recursion.
Lesson #6 Modular Programming and Functions.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dr. Sampath Jayarathna Cal Poly Pomona
ICS103 Programming in C Lecture 11: Recursive Functions
Java Software Solutions Foundations of Program Design Sixth Edition
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
Lecture 6 - Recursion.
Presentation transcript:

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

Drawing Trees Rendering Wilderness Nature reserve 3D computer graphics - nature scene Source: https://www.kisspng.com/png-rendering-wilderness-nature-reserve-3d-computer-gr-4461372/

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

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

Functions that call themselves Recursion Functions that call themselves

Recursion in the real world Russian dolls

Recursion in the real world Searching for the word “guffaw” in a dictionary Source: http://www.eslstation.net/ESL310L/310L_dict.htm

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

Recursion in the mathematical world Multiply two numbers Compute factorials Fractals The Sierpinski triangle A confined recursion of triangles that form a fractal https://en.wikipedia.org/wiki/Recursion

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 http://en.wikipedia.org/wiki/Recursion

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

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(…)

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

How to design/implement a recursive function? Hum… Easy for you to say! We need to think recursively! http://www.notonthehighstreet.com/britishandbespoke/product/paint-your-own-russian-dolls-for-children

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”

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.

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?

Let’s implement our Factorial function!

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

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