Presentation is loading. Please wait.

Presentation is loading. Please wait.

We are removing the 4th part of hw2pr2 for now.

Similar presentations


Presentation on theme: "We are removing the 4th part of hw2pr2 for now."— Presentation transcript:

1 We are removing the 4th part of hw2pr2 for now.
Extra credit of 5 points will be given to anyone or team who have completed or will complete this part.

2 Turtle Graphics Python way of drawing

3 Exercise (1) What does function chai draw? def chai(size):
""" mystery! """ forward(size) left(90) forward(size/2.0) right(90) backward(size) Why are there two identical commands in a row? How could you add more to each end?

4 Then you will be able to write functions using turtle commands:
Turtle Graphics We will be using Python’s built-in turtle graphics package. You will want to have this line in your hw2pr3.py file: from turtle import * Then you will be able to write functions using turtle commands: Turtle reference Also see link in the homework For basic help on Python's turtle graphics module

5 Recursive Graphics (1) Could we tri this with recursion?
do... or do not... there is no tri … (1) Could we tri this with recursion? def tri(): """ draws a polygon """ forward(100) left(120) def tri( ): """ draws a polygon """ def triRec(n=3): if n == 0: return else: forward(100) left(120) triRec(n-1) EVERYTHING IN CS IS PATTERNS. WHAT IS THE PATTERN THAT I CAN TEASE OUT HERE? GOING BACK TO YOUR LEGO DAYS… Illustrate by example. Draw square, and pentagon. (2) Could we create any regular n-gon?

6 Generic n-gon A default parameter value!
def ngon(nSides,curSide=0): """ A simple recursive function to create an arbitrary n-sided polygon Parameters: n - Number of sides of the polygon curSide - used by the recursion """ if curSide >= nSides: return else: forward(100) left(360/nSides) ngon(nSides,curSide+1) How many degrees should we turn?

7 ngon(nSides) pencolor('red') ngon(3) pencolor('orange') ngon(4) pencolor('yellow') ngon(5) pencolor('green') ngon(6) pencolor('blue') ngon(7)

8 Drawing Recursively Any self-similarity here? 190 200

9 Designing any recursive program boils down to the same two pieces
Drawing Recursively Designing any recursive program boils down to the same two pieces Think about the SIMPLEST POSSIBLE case! Base Case: Do ONLY ONE STEP, and let recursion do the rest… Recursive Step:

10 Designing any recursive program boils down to the same two pieces
Drawing Recursively Designing any recursive program boils down to the same two pieces def spiralSquare(x): if x < 1: return else: forward(x) left(90) spiralSquare(x-10) Base Case call spiralSquare(x), where x == number of moves Recursive Step What if we had forgotten the base case?

11 Drawing Recursively Try writing these functions: def spiralTri(x):
Draws a triangular (equilateral) spiral. Try it! spiralTri(200) draws def spiralOct(x): Draws an octagonal spiral. Try it! spiralOct(100) draws def spiralLim(x, n): Draws a square spiral with only n segments! actually, a bit larger than this … Try it! spiralLim(200, 7) draws

12 close-up of innermost part of the spiral…
hw2pr3 spiral 81 72.9 90 close-up of innermost part of the spiral… spiral( 100, 90, 0.9 ) 100 spiral(initLength, angle, multiplier)

13 svTree( trunkLength, levels )
hw2pr3 svTree Level 1 Level 2 Level 3 For the next program we are going to draw a fractal tree. Fractals come from a branch of mathematics, and have much in common with recursion. The definition of a fractal is that when you look at it the fractal has the same basic shape no matter how much you magnify it. Some examples from nature are the coastlines of continents, snowflakes, mountains, and even trees or shrubs. The fractal nature of many of these natural phenomenon makes it possible for programmers to generate very realistic looking scenery for computer generated movies. In our next example we will generate a fractal tree. To understand how this is going to work it is helpful to think of how we might describe a tree using a fractal vocabulary. Remember that we said above that a fractal is something that looks the same at all different levels of magnification. If we translate this to trees and shrubs we might say that even a small twig has the same shape and characteristics as a whole tree. Using this idea we could say that a tree is a trunk, with a smaller tree going off to the right and another smaller tree going off to the left. If you think of this definition recursively it means that we will apply the recursive definition of a tree to both of the smaller left and right trees. Level 4 svTree( 100, 4 ) I wonder what happened to the leaves on that tree… ? svTree( trunkLength, levels )


Download ppt "We are removing the 4th part of hw2pr2 for now."

Similar presentations


Ads by Google