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 5Homework 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 2OutlineRecursive PatternsExample: Generating FractalsFriday, January 21 CS 215 Fundamentals of Programming II - Lecture 5 3Recursive PatternsReview: 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 FractalsFractals 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 FractalA simple fractal example is as follows:Start with a straight line segmentCompute the midpoint of the lineMove the midpoint up or down a random amountRepeat with each new line segmentThe 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 SpecificationWrite 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 endpointsepsilon, 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 9ExampleOriginal 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 aboveFunction 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 DesignBase 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 11ImplementationCopy files /home/hwang/cs215/lecture05/*.* from csserverFiles 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 12ImplementationOnce 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 13ImplementationSince 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 14ImplementationGenerating 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 ExerciseModify 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