+ All Categories
Home > Documents > Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in...

Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in...

Date post: 14-Dec-2015
Category:
Upload: mara-mowdy
View: 226 times
Download: 6 times
Share this document with a friend
30
Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature
Transcript
Page 1: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Computer Science 111

Fundamentals of Programming IAdvanced Turtle Graphics

Recursive Patterns in Art and Nature

Page 2: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Recursive Patterns in Art

• The 20th century Dutch artist Piet Mondrian painted a series of pictures that displayed abstract, rectangular patterns of color

• Start with a single colored rectangle

• Subdivide the rectangle into two unequal parts (say, 1/3 and 2/3) and paint these in different colors

• Repeat this process until an aesthetically appropriate “moment” is reached

Page 3: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 1: A Single Filled Rectangle

Page 4: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 2: Split at the Aesthetically

Appropriate Spot

Page 5: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 3: Continue the Same Process with Each Part

Page 6: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 4

Page 7: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 5

Page 8: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 6

Page 9: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 7

Page 10: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 8

Page 11: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 9

Page 12: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Design a Recursive Function

• The function expects a Turtle object, the corner points of a rectangle, and the current level as arguments

• If the level is greater than 0

– Draw a filled rectangle with the given corner points

– Calculate the corner points of two new rectangles within the current one and decrement the level by 1

– Call the function recursively to draw these two rectangles

Page 13: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

from turtle import Turtleimport random

def drawRectangle(t, x1, y1, x2, y2): red = random.randint(0, 255) green = random.randint(0, 255) blue = random.randint(0, 255) t.pencolor(red, green, blue) # Code for drawing goes here

# Definition of the recursive mondrian function goes here

def main(level = 1): t = Turtle() t.speed(0) t.hideturtle() x = 50 y = 50 mondrian(t, -x, y, x, -y, level)

Program Structure

Page 14: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

def mondrian(t, x1, y1, x2, y2, level): if level > 0: drawRectangle(t, x1, y1, x2, y2)

vertical = random.randint(1, 2) if vertical == 1: # Vertical split mondrian(t, x1, y1, (x2 - x1) // 3 + x1, y2, level - 1) mondrian(t, (x2 - x1) // 3 + x1, y1, x2, y2, level - 1)

else: # Horizontal split

mondrian(t, x1, y1, x2, (y2 - y1) // 3 + y1, level - 1) mondrian(t, x1, (y2 - y1) // 3 + y1, x2, y2, level - 1)

The mondrian Function

Page 15: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Recursive Patterns in Nature

• A fractal is a mathematical object that exhibits the same pattern when it is examined in greater detail

• Many natural phenomena, such as coastlines and mountain ranges, exhibit fractal patterns

Page 16: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

The C-curve

• 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

Page 17: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.
Page 18: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Level 0 and Level 1

(50,50)

(50,-50)

(0,0)

(50,-50)

(50,50)

drawLine(50, -50, 50, 50)

drawLine(50, -50, 0, 0)drawLine(0, 0, 50, 50)

Page 19: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Bisecting and Joining

(50,50)

(50,-50)

(0,0)

(50,-50)

(50,50)

0 = (50 + 50 + -50 - 50) // 20 = (50 + -50 + 50 - 50) // 2drawLine(50, -50, 0, 0)drawLine(0, 0, 50, 50)

drawLine(50, -50, 50, 50)

Page 20: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Generalizing

(50,50)

(50,-50)

(0,0)

(50,-50)

(50,50)

drawLine(x1, y1, x2, y2) xm = (x1 + x2 + y1 - y2) // 2ym = (x2 + y1 + y2 - x1) // 2drawLine(x1, y1, xm, ym)drawLine(xm, ym, x2, y2)

Page 21: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Recursing

(50,50)

(50,-50)

(0,0)

(50,-50)

(50,50)

drawLine(x1, y1, x2, y2) xm = (x1 + x2 + y1 - y2) // 2ym = (x2 + y1 + y2 - x1) // 2cCurve(x1, y1, xm, ym)CCurve(xm, ym, x2, y2)

Base case Recursive step

Page 22: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

def cCurve(t, x1, y1, x2, y2, level): if level == 0: drawLine(t, x1, y1, x2, y2) else: xm = (x1 + x2 + y1 - y2) // 2 ym = (x2 + y1 + y2 - x1) // 2 cCurve(t, x1, y1, xm, ym, level - 1) cCurve(t, xm, ym, x2, y2, level - 1)

Note that recursive calls occur before any C-curve is drawn when level > 0

The cCurve Function

Page 23: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

from turtle import Turtle

def drawLine(t, x1, y1, x2, y2): """Draws a line segment between the endpoints.""" t.up() t.goto(x1, y1) t.down() t.goto(x2, y2)

# Definition of the recursive cCurve function goes here

def main(level = 1): t = Turtle() t.speed(0) t.hideturtle() cCurve(t, 50, -50, 50, 50, level)

Program Structure

Page 24: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

ccurve

A call tree diagram shows the number of calls of a function for a given argument value

Call Tree for ccurve(0)

ccurve(0) uses one call, the top-level one

Page 25: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

ccurve

Call Tree for ccurve(1)

ccurve(1) uses three calls, a top-level one and two recursive calls

ccurve ccurve

Page 26: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

ccurve

Call Tree for ccurve(2)ccurve(2) uses 7 calls, a top-level one and 6 recursive calls

ccurve ccurve

ccurve

ccurve ccurve

ccurve

Page 27: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

ccurve

Call Tree for ccurve(n)ccurve(n) uses 2n+1 - 1 calls, a top-level one and 2n+1 - 2 recursive calls

ccurve ccurve

ccurve

ccurve ccurve

ccurve

Page 28: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

ccurve

Call Tree for ccurve(2)The number of line segments drawn equals the number of calls on the frontier of the tree (2n)

ccurve ccurve

ccurve

ccurve ccurve

ccurve

Page 29: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

Summary

• A recursive algorithm passes the buck repeatedly to the same function

• Recursive algorithms are well-suited for solving problems in domains that exhibit recursive patterns

• Recursive strategies can be used to simplify complex solutions to difficult problems

Page 30: Computer Science 111 Fundamentals of Programming I Advanced Turtle Graphics Recursive Patterns in Art and Nature.

For Next Week

Finish Chapter 7


Recommended