Question
The Koch snowflake is a fractal shape. At level 0, the shape is an equilateral triangle. At level 1, each line segment is split into
The Koch snowflake is a fractal shape. At level 0, the shape is an equilateral triangle. At level 1, each line segment is split into four equal parts, producing an equilateral bump in the middle of each segment. Figure 7-15 shows these shapes at levels 0, 1, and 2. Figure 7-15 First three levels of a Koch snowflake Figure 7-15 First three levels of a Koch snowflake At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level. The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. The function drawFractalLine is recursive. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with of the given distance, angles that produce the given effect, and the given level minus 1. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4
here is what I have and I keep getting an error saying width is not defined:
from turtle import Turtle import math
import sys
def drawKochFractal(width, height, size, level): """Draws a Koch fractal of the given level and size.""" t = Turtle() t.hideturtle() t.up() t.goto(-width // 3, height // 4) t.down()
drawFractalLine(t, size, 0, level);
drawFractalLine(t, size, -120, level)
drawFractalLine(t, size, 120, level)
def drawFractalLine(t, distance, theta, level):
"""Either draws a single line in a given direction
or four fractal lines in new directions."""
if (level == 0):
drawPolarLine(t, distance, theta)
else:
drawFractalLine(t, distance // 3, theta, level - 1)
drawFractalLine(t, distance // 3, theta + 60, level - 1)
drawFractalLine(t, distance // 3, theta - 60, level - 1)
drawFractalLine(t, distance // 3, theta, level - 1)
def drawPolarLine(t, distance, theta):
"""Moves the given distance in the given direction."""
t.setheading(theta)
t.forward(distance)
def main():
level = int(input("Enter the level: "))
drawKochFractal(200, 200, 150, level)
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started