Computer Science 111 Fundamentals of Programming I

Slides:



Advertisements
Similar presentations
Recursive Strategies Eric Roberts CS 106B January 23, 2013.
Advertisements

Computer Graphics … how renderings are done on a computer. Art 321 Dr. J Parker Winter.
Linear Interpolation Applying “weighted averages” to some graphics problems: animations and curve-drawing.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Experiments and Variables
More About Recursion - 2 Java. Looking at more recursion in Java A simple math example Fractals.
SECTION 2-3 Mathematical Modeling – Triangular Numbers HW: Investigate Pascal’s Triangle o Write out the first 8 rows o Identify the linear, rectangular.
COMPSCI 105 S Principles of Computer Science
COMPSCI 105 S Principles of Computer Science Recursion 1.
Section 8.8 Heapsort.  Merge sort time is O(n log n) but still requires, temporarily, n extra storage locations  Heapsort does not require any additional.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
CS39N The Beauty and Joy of Computing Lecture #11 Recursion III It has been a challenge to power electronic components implanted within a body.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Day and Night by M.C. Escher
Fibonacci Spiral Richard Kwong MAT 385
Piet Mondrian Broadway Boogie-Woogie. Piet Mondrian Dutch Painter Known for white backgrounds with grids of vertical and horizontal black.
Students will learn about the art style of
Sullivan – Fundamentals of Statistics – 2 nd Edition – Chapter 5 Section 1 – Slide 1 of 33 Chapter 5 Section 1 Probability Rules.
Clipping: Clipping is a process of dividing an object into visible and invisible positions and displaying the visible portion and discarding the invisible.
Objectives Differentiate between raster scan display and random scan display.
Studying Earth Science Chapter Two: The Science Toolbox 2.1 Measurement 2.2 Measuring Time and Temperature 2.3 Systems and Variables 2.4 Graphs.
FUNCTIONS AND MODELS 1. In this section, we assume that you have access to a graphing calculator or a computer with graphing software. FUNCTIONS AND MODELS.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters,
Sullivan – Fundamentals of Statistics – 2 nd Edition – Chapter 2 Section 1 – Slide 1 of 27 Chapter 2 Section 1 Organizing Qualitative Data.
Ch 9 Infinity page 1CSC 367 Fractals (9.2) Self similar curves appear identical at every level of detail often created by recursively drawing lines.
A pioneer in the art of abstraction “always further”
10.3 Hyperbolas. Circle Ellipse Parabola Hyperbola Conic Sections See video!
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
SECTION 2-3 Mathematical Modeling. When you draw graphs or pictures of situation or when you write equations that describe a problem, you are creating.
AUGUST 2. MATH 104 Calculus I Review of previous material…. …methods and applications of integration, differential equations ………..
Course 8 Contours. Def: edge list ---- ordered set of edge point or fragments. Def: contour ---- an edge list or expression that is used to represent.
1.5 Segment and Angle Bisector Bisect a segment and an angle.
Piet Mondrian.
&feature/endscreen.
Piet Mondrian Adapted From Presentations Created by Rock Ledge Elementary Fine Art Program, Seymour, WI Original Source:
When the artist does not intend for it to look like anything in particular.
Visual C++ Programming: Concepts and Projects Chapter 10A: Recursion (Concepts)
Piet Mondrian – Pier Mondrian in a dutch painter, one of the first abstract painter of the twentyth century. Our work with his.
Computer Science We use computers, but the focus is on applied problem solving. One of my favorite videos talks about computer sciencecomputer science.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 10A Recursion (Concepts)
Intro to Graphics from Chapter 2 of Java Software Solutions
Composition No. II, with Red and Blue Piet MondrianPiet Mondrian (Dutch, 1872–1944) CLAIM Make a claim about the math you see in Mondiran’s artwork. Claim.
A Plane-Based Approach to Mondrian Stereo Matching
Computer Science 111 Fundamentals of Programming I
Piet Mondrian.
Computer Graphics CC416 Week 13 Clipping.
Fundamentals of Programming I Design with Functions
Abstract Art Looking at Different Artists.
Programming Fundamentals
Chapter 8: Recursion Java Software Solutions
Piet Mondrian Piet Mondrian was a Dutch artist who was famous for making paintings with lots of geometric shapes. He used a ruler to make straight lines.
patching Bezier curves and surfaces fractals
Piet Mondrian Created by J. Walker & L. McHugh 9/06.
Chapter 8: Recursion Java Software Solutions
Using Algebra Tiles for Student Understanding
Here are four triangles. What do all of these triangles have in common
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
One-Point Perspective Drawing and Space
Last Class We Covered Recursion Stacks Parts of a recursive function:
What do you see here???.
Java Software Solutions Foundations of Program Design Sixth Edition
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Lecture 6 - Recursion.
Presentation transcript:

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

Level 1: A Single Filled Rectangle

Level 2: Split at the Aesthetically Appropriate Spot

Level 3: Continue the Same Process with Each Part

Level 4

Level 5

Level 6

Level 7

Level 8

Level 9

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

Program Structure from turtle import Turtle import 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)

The mondrian Function 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, else: # Horizontal split mondrian(t, x1, y1, x2, (y2 - y1) // 3 + y1, mondrian(t, x1, (y2 - y1) // 3 + y1, x2, y2,

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

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

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)

Bisecting and Joining (50,50) (50,50) (0,0) (50,-50) (50,-50) drawLine(50, -50, 50, 50) 0 = (50 + 50 + -50 - 50) // 2 0 = (50 + -50 + 50 - 50) // 2 drawLine(50, -50, 0, 0) drawLine(0, 0, 50, 50)

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

Recursing Base case Recursive step (50,50) (50,50) (0,0) (50,-50) drawLine(x1, y1, x2, y2) xm = (x1 + x2 + y1 - y2) // 2 ym = (x2 + y1 + y2 - x1) // 2 cCurve(x1, y1, xm, ym) CCurve(xm, ym, x2, y2) Base case Recursive step

The cCurve Function 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

Program Structure 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)

Call Tree for ccurve(0) A call tree diagram shows the number of calls of a function for a given argument value ccurve ccurve(0) uses one call, the top-level one

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

Call Tree for ccurve(2) ccurve(2) uses 7 calls, a top-level one and 6 recursive calls ccurve ccurve ccurve ccurve ccurve ccurve 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 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 ccurve

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

For Next Week Finish Chapter 7