DOC PREVIEW
UE CS 215 - Lecture 5

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 5OutlineSlide 3Generating FractalsLine Segment Fractal 1Line Segment Fractal 2Why Study Fractals?Function SpecifcationExampleFunction DesignImplementation 1Implementation 2Implementation 3Implementation 4In-class Exercise 3Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 1Lecture 5Homework 3 is posted. It is due next Wednesday.Reminder: Homework 2 is due today by 4:30pm otherwise it will be late. Last day to submit for late credit is Wednesday.Log into Linux.Questions?Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 2OutlineRecursive PatternsExample: Generating FractalsFriday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 3Recursive PatternsReview: General strategy for thinking about recursive solutions:Identify one or more base (anchor) cases, also called stopping conditions: Cases that are solved directly.Identify one or more recursive (inductive) steps: Solve the problem by dividing it into smaller version of itself.Sometimes, the problem itself is described recursively as a pattern.Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 4Example: Generating FractalsFractals are one of the tools in graphics used to generate natural looking scenes of mountains, clouds, trees, etc.Generally, a fractal has the following characteristics:It is self-similar, i.e., arbitrarily small pieces under magnification look like large pieces.It is too irregular to be described by Euclidean geometry.It has a simple and recursive definition.Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 5Line Segment FractalA simple fractal example is as follows:Start with a straight line segmentCompute the midpoint of the lineMove the midpoint up or down a random amountRepeat with each new line segmentThe base case happens when the new line segment is smaller than some set length.Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 6Line Segment Fractala) Initial Line Segmentb) Midpoint moved up a random distancec) Two midpoints moved up and down a random distanced) Four midpoints moved up and down a random distanceFriday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 7Why Study Fractals?After many steps, the line looks jagged.But if we magnify the line, we would see that the magnified portion looks remarkably similar to the overall shape of the line.This appears a lot in nature, which is why fractals are used to generate scenery.Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 8Function SpecificationWrite a function random_fractal( ) that computes and draws a random line fractal.Receives (textbook uses "input"): left_height and right_height, the height of the left and right endpoints, respectively, measured from a fixed base line.width, the horizontal width from the left to right endpointsepsilon, the width at which the process stops and the line is drawn; usually very smallFriday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 9ExampleOriginal function call might be (distance in inches):RandomFractal(0.0, 0.7, 2.0, 0.6)After 2 steps, will stop and might have fractal aboveFunction should generate and display height of right endpoints0.00.5"2.01.01.00.650.7-0.3baselineFriday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 10Function DesignBase case is easy1. If width is less than or equal to epsilon 1.1 Display right_height.Recursive step follows the generation algorithm1. Compute random height for the midpoint a. Compute current mid_height = (right_height+left_height)/2 b. Add random number to mid_height2. Generate random fractals for the new line segments a. random_fractal (left_height, mid_height, width/2, epsilon) b. random_fractal (mid_height, right_height, width/2, epsilon)Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 11ImplementationCopy files /home/hwang/cs215/lecture05/*.* from csserverFiles useful.h and useful.cpp are from the textbook Appendix I. File fractal.cpp is a slightly modified version of textbook code provided for Figure 9.4.Function prototype:void random_fractal(double left_height, double right_height, double width, double epsilon);Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 12ImplementationOnce again, base case is easy:if (width <= epsilon) display (right_height);The display( ) function prints out a line of output with a vertical bar in the middle. If the argument n is positive, then approximately n stars will be displayed to the right of the bar. If n is negative, then approximately n starts will be displayed to the left of the bar. See useful.cpp for more information.Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 13ImplementationSince the recursion generates the left segments before the right segments, the left end of the fractal will be at the top of the screen and the fractal is generated sideways.The recursive step also is straightforward. To compute the height of the midpoint:mid_height = (left_height + right_height) / 2;mid_height += random_real(-width, width);random_real( ) returns a real number in the given range; it also is in useful.cpp.Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 14ImplementationGenerating random fractals for the new line segments is the same as shown in the design:random_fractal (left_height, mid_height, width/2, epsilon);random_fractal (mid_height, right_height, width/2, epsilon);Friday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 15In-class ExerciseModify random_fractal( ) so that the movements of the midpoints are no longer random. Instead, at the first level midpoint should be moved up by the width amount, the midpoints at the next level should be moved down by the width amount, the next level up, then down and so on. Hint: you will need to add another parameter to random_fractal( ) to keep track whether to move the midpoint up or down.Compile, run, and test your


View Full Document

UE CS 215 - Lecture 5

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 5
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?