![]() ![]() % draw the next circle if more iterations remainĭrawCircles(,iterations-1,initAngle+20,r) % use trigonometry to determine the x and y components % initiAngle = number the current angle of the spiral % pt = the bottom left hand corner of the circle to be drawn % to draw the next circle in the spiral, until all iterations have been % This function draws the current circle in the spiral and calls itself % This function generates a fractal spiral with r iterationsįunction drawCircles(pt,iterations,initAngle,r) With each iteration, we will draw a circle, and we'll simply change the angle, position, and size of the circle with each iteration. We'll continue with a simple example: drawing a spiral. P1=plot(,'Color',c) ĭrawTriangles(len-len*lengthDecrement,angle+angInc.ĪngInc,iterations-1,r,lengthDecrement,transparency) % transparency = number between 0 and 1 transparency of triangle % lengthDecrement = number amount len changes each iteration % iterations = int remaining number of iterations % angle = number amount the triangle rotates relative to previous % len = number length of a side of the triangle % Draws the triangle for each iteration and calls itself. ![]() % transparency = number between 0 and 1 transparency of each triangleĭrawTriangles(1,0,angleIncrement,r,r,lengthDecrement,transparency)įunction drawTriangles(len,angle,angInc,iterations,r. % lengthDecrement = number amount triangle shrinks each iteration % angleIncrement = number amount the triangle rotates each iteration ![]() % Draw a fractal triangle, with the following inputs: Take a look, and let me know if you have any questions!įunction fractalTriangle(r,angleIncrement,lengthDecrement,transparency) The inputs for fractalTriangle are described in the code below. We should have something that looks like this: So our draw subfunction will calculate the three points of the current triangle, based on the current length and angle parameters, and draw the triangle on top of the existing triangles. We want to rotate the triangle with each iteration, and also shrink it slightly. Then we simply draw lines between them using the plot command. Since we know that the three points of an equilateral triangle are separated by 120 degrees, we can simply rotate our first point to get our second and third points. It shows us the structure of the program we want to write, but will not run because the details haven't been filled in.įunction mainFunction (totalNumberOfIterations)įunction drawingFunction(remainingIterations)įor this example, we again harness the power of rotation! We want to draw a triangle centered around the origin. Note that the 'code' you see here is not really code at all. Thus, with a simple call to mainFunction you can draw an entire fractal structure! The only difference is that it calls itself with one less iteration. Then, importantly, drawingFunction calls itself. drawingFunction does the job of drawing your recursive structure, and takes as an input the number of recursions you want. Let's call it drawingFunction and color it blue in the example program below. In mainFunction, you call a second function- a sub-function. You begin with a function that will compute any global variables and compute and any non-iterative components of the drawing. The basic structure for each of these programs is very similar, so let's go over that first. Be warned: the greater the recursive depth, the longer the code will take to execute! You most likely won't have time in this class to code up your own recursive drawing, but you're encouraged to choose one of the patterns below and try changing a few parameters. Let's examine some of these deceptively simple-looking images. Perhaps the simplest fractals are created by the process of recursively drawing a particular pattern. You can only get so far into mathematical art without mentioning fractals! As a review, a fractal is an image for which the same structre is evident at any level of resolution. ![]()
0 Comments
Leave a Reply. |