PYTHON CODE: Fractal Design using Turtle Graphics: A four-step algorithm creates
ID: 3852612 • Letter: P
Question
PYTHON CODE:
Fractal Design using Turtle Graphics:
A four-step algorithm creates this fractal:
Specify an intricacy level, a positive integer for the fractal.
Start with a straight line. This line is called level 0 fractal (Fig. (a))
To obtain a fractal for the next level, replace each line in the drawing with sides of an isosceles right triangle having the line as hypotenuse. Fig. (b), (c), and (d) show the level I, 2, and 3 fractals.
Repeat last step until the desired level of recursion is reached.
The fractal designs below have intricacy level 4, 8, and 12.
Hints:
Write a function drawline (…) draw line from (x1, y1) to (x2, y2)
Write a recursive function fractal (t, x1, y1, x2, y2, level) which starts drawing at (x1, y1) and ends at (x2, y2) for given level. To call the function recursively, use the following steps:
If level =0 call drawline()
Else
Find newX as (x1 +x2)/2 + (y2 – y1)/2
Find newY as (y1 = y2)/2 – (x2-x1)/2
Call fractal with x1,y1, newX, newY, and level-1
Call fractal with newX, newY, x2, y2, and level-1
Explanation / Answer
PYTHON CODE:
def koch(t, order, size):
"""
Make turtle t draw a Koch fractal of 'order' and 'size'.
Leave the turtle facing the same direction.
"""
if order == 0: # The base case is just a straight line
t.forward(size)
else:
koch(t, order-1, size/3) # Go 1/3 of the way
t.left(60)
koch(t, order-1, size/3)
t.right(120)
koch(t, order-1, size/3)
t.left(60)
koch(t, order-1, size/3)
--------
def koch(t, order, size):
if order == 0:
t.forward(size)
else:
for angle in [60, -120, 60, 0]:
koch(t, order-1, size/3)
t.left(angle)
-------
def koch_0(t, size):
t.forward(size)
def koch_1(t, size):
for angle in [60, -120, 60, 0]:
koch_0(t, size/3)
t.left(angle)
def koch_2(t, size):
for angle in [60, -120, 60, 0]:
koch_1(t, size/3)
t.left(angle)
def koch_3(t, size):
for angle in [60, -120, 60, 0]:
koch_2(t, size/3)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.