COMPSCI 105 S Principles of Computer Science

Slides:



Advertisements
Similar presentations
Computer Science 111 Fundamentals of Programming I
Advertisements

40S Applied Math Mr. Knight – Killarney School Slide 1 Unit: Sequences Lesson: SEQ-L3 Drawing Fractal Patterns Drawing Fractal Patterns Learning Outcome.
ATEC Procedural Animation Introduction to Procedural Methods in 3D Computer Animation Dr. Midori Kitagawa.
Goal: to rotate a figure around a central point
Cosc 1P02 Week 2 Lecture slides
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
L systems (Aristid Lindenmayer)
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 1) An introduction to Logo: drawing, moving,
Approaches To Infinity. Fractals Self Similarity – They appear the same at every scale, no matter how much enlarged.
RECURSION Recursion Towers of Hanoi To Recurse or to Loop? Fractals Do something!
Log on and Download from website:
1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.
Today we will derive and use the formula for the area of a triangle by comparing it with the formula for the area of a rectangle. derive = obtain or receive.
Triangle Town Hi, I’m Elke. Pleased to be your guide through Triangle Town. Don’t ever worry about getting lost. I’ll stay with you the whole time.
Mini Project II – Drawing Machine
LOGO SOFTWARE BY: SAVE 9S. INTRODUCTION Logo is a software that can be found at : Shared area> High School > ICT > take home software > LOGO32. This is.
Graphics and Procedures Programming Right from the Start with Visual Basic.NET 1/e 5.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters,
Microsoft® Small Basic
Python Programming in Context Chapter 9. Objectives To introduce functional programming style To practice writing recursive functions To introduce grammars.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
1 CSC 221: Computer Programming I Fall 2011 Fun with turtle graphics  turtle module  relative motion (setup, reset, left, right, forward, backward) 
Main Points: - Python Turtle - Fractals
Turtle see, turtle do Lesson 1 – Welcome to LOGO.
A new human-computer interface?
Agent P, I have been hearing some rumours about a Python Turtle.
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
MSW Logo By Awin 9s.
Logo For beginners By Dali Matthews 9S What is logo?
How to link the robot and the computer (Bluetooth) How to turn on and off How to connect the adaptor Fluke card connection Sec Getting Started How.
CONTROL SYSTEMS Control Systems A command is a directive that performs a specific task An argument is a variable that can be used by the function reveiving.
E9 Students are expected to make generalizations about the properties of translations and reflections and apply these properties. E10 Students are expected.
By Liam Lane How To Use MSW LOGO.
1 Project designed and created by M. Shajith Kumar.
ATEC Procedural Animation Introduction to Procedural Methods in 3D Computer Animation Dr. Midori Kitagawa.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Let’s Learn 3. Modules Saenthong School, January – February 2016
Fractal Art. What is Fractal Art? A fractal is a geometric shape that has self similarity, that is it can be split into pieces that are approximate reduced.
CAG – (Auto)CAD Commands
Transformations for GCSE Maths Enlargement Translation Reflection Rotation.
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
What is it? How to use it and how useful can it be?
Using Logo to explore spiral patterns. Paul Broadbent Spiral patterns This is a (1,2,3) spiral path. It repeats lines of three.
Using the Python Turtle
Python Turtle Graphics
Fundamentals of Programming I Introduction to Graphics
Computer Science 111 Fundamentals of Programming I
Introduction to Python
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
LOGO BY Kaotip 9S.
Main Points: - Python Turtle - Fractals
General Form for a Conditional
ATCM 3310 Procedural Animation
Fundamentals of Programming I Introduction to Graphics
We are removing the 4th part of hw2pr2 for now.
Agent P, I have been hearing some rumours about a Python Turtle.
Transformations for GCSE Maths
Exercise (1) What does function chai draw? def chai(size):
Module 2 Lesson 3 Over and Over Again
Chap. 3 Functions Start Python IDLE (Shell) and open a new file.
Main Points: - Python Turtle - Fractals
Transformations for GCSE Maths
Introduction to Turtle Graphics
Fractals The Hilbert Curve.
Transformations for GCSE Maths
Module 2 Lesson 3 Over and Over Again
Module 2 Lesson 3 Over and Over Again
Presentation transcript:

COMPSCI 105 S2 2014 Principles of Computer Science Recursion 2

Agenda Agenda Introduction Using the Python Turtle Recursive Drawing Drawing a Spiral Drawing a KochUp Drawing a C-curve Call Tree COMPSCI105 21

21.1 Introduction Example A fractal is a rough or fragmented geometric shape that can be split into parts, each of which is (at least approximately) a reduced-size copy of the whole. This a property is called self- similarity COMPSCI105 21

21.2 Using the Python Turtle Turtle class Can be drawn using a “Turtle” Named after Logo programming language Pen location used to draw on the screen Commands Pen up Pen down Rotate … 1. Draw a line 2. Rotate 90 (pi/2) 5. Draw a line 6. Rotate 90 (pi/2) 3. Draw a line 4. Rotate 90 (pi/2) COMPSCI105 21

21.2 Using the Python Turtle The Python Turtle Steps: Import the turtle module which defines the Turtle and the Screen types. Create and open a window. The window contains a canvas, which is the area inside the window on which the turtle draws. Create a turtle object which can move forward, backwards, turn left, turn right, the turtle can have its tail up/down. If the tail is down the turtle draws as it moves. The width and colour of the turtle tail can be changed. When the user clicks somewhere in the window, the turtle window closes and execution of the Python program stops. import turtle my_win = turtle.Screen() tess = turtle.Turtle() my_win.exitonclick() COMPSCI105 21

21.2 Using the Python Turtle Instantiating a Turtle Instantiate a Turtle object: The turtle appears as an icon Initial position: (0, 0) Initial direction: East (0°) Colour: black Line width: 1 pixel Pen: down (ready to draw) tess = turtle.Turtle() y-axis (0,0) 90° 180° 0° 270° x-axis COMPSCI105 21

21.2 Using the Python Turtle Methods forward(distance) – move the turtle forward backward(distance) – move the turtle backwards right(angle) – turn the turtle clockwise left(angle) – turn the turtle anti-clockwise up() – puts the turtle tail/pen up, i.e., no drawing down() – puts the turtle tail/pen down, i.e., drawing pencolor(colour_name) – changes the colour of the turtle's tail heading() – returns the direction in which the turtle is pointing setheading(angle) – set the direction in which the turtle is pointing position() – returns the position of the turtle goto(x, y) – moves the turtle to position x, y speed(number) – set the speed of the turtle movement COMPSCI105 21

21.2 Using the Python Turtle Example my_win = turtle.Screen() tess = turtle.Turtle() tess.pencolor("hotpink") tess.pensize(5) tess.forward(80) tess.pencolor("blue") tess.left(120) tess.pencolor("green") tess.pensize(10) tess.pencolor("magenta") tess.left(120) tess.right(180) tess.up() tess.forward(80) tess.down() my_win.exitonclick() COMPSCI105 21

21.3 Recursive Drawing Recursive Drawing In the previous section we looked at some problems that were easy to solve using recursion In this section we will look at a couple of examples of using recursion to draw some interesting pictures Drawing a spiral recursively Drawing a Koch Up shape Drawing a C Curve COMPSCI105 21

21.4 Drawing a Spiral Drawing a Spiral recursively Define the draw_spiral function The base case is when the length of the line is zero or less. The recursive step: (length of the line > 0) Instruct the turtle to go forward by len units, and Turn right 90 degrees. Call draw_spiral again with a reduced length. Forward 100 draw_spiral(my_turtle,100) draw_spiral(my_turtle,95) Forward 95 draw_spiral(my_turtle,90) ... Forward 90 draw_spiral(my_turtle,0) COMPSCI105 21

21.4 Drawing a Spiral The draw_spiral function Steps: Define the draw_spiral function Create a turtle Call the recursive function def draw_spiral(my_turtle, line_len): if line_len > 0: my_turtle.forward(line_len) my_turtle.right(90) draw_spiral(my_turtle,line_len-5) import turtle ... my_win = turtle.Screen() my_turtle = turtle.Turtle() draw_spiral(my_turtle,100) my_win.exitonclick() COMPSCI105 21

21.5 Drawing a KochUp Drawing a Koch Up shape recursively Idea: recursively applying a simple rule to each of the triangles sides. Examples: The pattern: cut the side (line_len) into 3 equal parts (line_len/3) replace the center part with 2 sides of length line_len/3, such that it forms a spike repeat the process for each of the 4 sides, until the length of each side is smaller than a given value. Level 1 Level 0 Level 2 Level 3 COMPSCI105 21

21.5 Drawing a KochUp Drawing a Koch Up shape Define the draw_kochup function The base case is when the level is zero or less: Instruct the turtle to go forward by line_len units The recursive step: (level> 0) Call draw_kochup again with a reduced length and a reduced level Turn left 60 degrees. (anti-clockwise) Turn right 120 degrees. COMPSCI105 21

21.5 Drawing a KochUp Examples draw_kockup(my_turtle, 0, 300) Case 1: Simply draw a line of length 300 Case 2: Recursively call the same function again draw_kockup(my_turtle, 1, 300) draw_kockup(my_turtle, 0, 100) Draw a line Forward 100 my_turtle.left(60) draw_kockup(my_turtle, 0, 100) Forward 100 my_turtle.right(120) draw_kockup(my_turtle, 0, 100) Forward 100 my_turtle.left(60) draw_kockup(my_turtle, 0, 100) Forward 100 COMPSCI105 21

21.5 Drawing a C-curve Drawing a C curve shape recursively A C-curve is a fractal pattern A level 0 C-curve is a vertical line segment A level 1 C-curve is obtained by bisecting a level 0 C-curve and joining the sections at right angles … A level N C-curve is obtained by joining two level N - 1 C-curves at right angles COMPSCI105 21

21.5 Drawing a C-curve Examples Level 0 Level 1 Level 2 COMPSCI105 21

21.5 Drawing a C-curve Drawing a C Curve Define the draw_c_curve function The base case is when the level is zero or less: Instruct the turtle to go forward by line_len units The recursive step: (level> 0) Turn right 45 degrees. Call draw_kochup again with a reduced length and a reduced level Turn left 90 degrees. COMPSCI105 21

21.6 Call Tree Call Tree for C–Curve (0), C_curve(1) A call tree diagram shows the number of calls of a function for a given argument value ccurve(0) uses one call, the top-level one ccurve(1) uses three calls, a top-level one and two recursive calls ccurve ccurve ccurve ccurve COMPSCI105 21

21.6 Call Tree Call Tree for c-curve(2) ccurve(2) uses 7 calls, a top-level one and 6 recursive calls ccurve(n) uses 2n+1 - 1 calls, a top-level one and 2n+1 - 2 recursive calls ccurve ccurve ccurve ccurve ccurve ccurve ccurve COMPSCI105 21

Exercise Draw a kochDown shape recursively. Level 0 Level 1 Level 3 COMPSCI105 21