im so lost on this one please help its for python 3.5 The Koch snowflake is a fr
ID: 3775796 • Letter: I
Question
im so lost on this one please help its for python 3.5
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.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 1 3 of the given distance, angles that produce the given effect, and the given level minus 1. Write a script that draws the Koch snowflake.
This is what i have but its way off please help:
from turtle import Turtle
def cCurve(t, x1 , y1, x2,y2,level):
def drawLine(x1, y1, x2, y2):
t.up()
t.goto(x1, y1)
t.down()
t.goto(x2, y2)
if level == 0:
drawLine(x1, y1, x2, y2)
else:
xm = (x1 + x2 + y1 - y2)//2
ym = (x2 + y1 + y2 - x1)//2
cCurve(t, x1, y1, xm, ym, level - 1)
cCurve(t, xm, ym, x2, y2,level - 1)
def main():
level = int(input("Enter the level: "))
t = Turtle()
t.hideturtle()
cCurve(t, 50, -50, 50, 50, level)
main()
Explanation / Answer
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()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.