Question

The Koch snowflake is a fractal shape. At level 0, the shape is an equilateral triangle....

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()

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#where are you getting the error, i m not getting any
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()

t.setheading (theta) t.forward (distance) def main ): level- int (input (Enter the level: ) drawKochFractal (200, 200, 150, level) main () drawKochFractal0 kochFrac C:\Users Vaibhav AppData\Local\Programs\ Python\Python37\python.exe C:/Users/Vaibhav/Docume Enter the level:

Add a comment
Know the answer?
Add Answer to:
The Koch snowflake is a fractal shape. At level 0, the shape is an equilateral triangle....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • with turtle in python: Fractals are fun at every level. (Draw the Koch snowflake) The Koch...

    with turtle in python: Fractals are fun at every level. (Draw the Koch snowflake) The Koch snowflake is one of the earliest fractals to have been described. The snowflake has a finite area bounded by an infinitely long line. It can be constructed as follows, starting with an equilateral triangle and doing the following for each side: 1. divide the line segment into three segments of equal length. 2. draw an equilateral triangle pointing outward that has the middle segment...

  • /* * File: HFractal.cpp * ------------------ * This program draws an H-fractal on the graphics wi...

    /* * File: HFractal.cpp * ------------------ * This program draws an H-fractal on the graphics window.int main() { */ #include "gwindow.h" /* Function prototypes */ void drawHFractal(GWindow & gw, double x, double y, double size, int order); /* Main program */ int main() { GWindow gw; double xc = gw.getWidth() / 2; double yc = gw.getHeight() / 2; drawHFractal(gw, xc, yc, 100, 3); return 0; } /* * Function: drawHFractal * Usage: drawHFractal(gw, x, y, size, order); * ------------------------------------------- *...

  • I cannot run this code! Please explain and fix it for me import turtle import random...

    I cannot run this code! Please explain and fix it for me import turtle import random # Approximate size of turtle objects in pixels BODY_SIZE = 80 # Half if the body size for collision detection with the edge of the screen HALF_BODY_SIZE = BODY_SIZE / 2 # if enemies get this close to friend there is collision COLLISION_DISTANCE = 5 # The window size SIZE = 500 # inner boundary within window for reversing the direction of enemies LOWER_BOUND...

  • import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2...

    import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() / 2 turtleX = turt.xcor() turtleY = turt.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: stillIn = False if turtleY > topBound or turtleY < bottomBound: stillIn = False return stillIn def main(): wn = turtle.Screen() # Define your turtles here june = turtle.Turtle() june.shape('turtle') while isInScreen(wn,june): coin = random.randrange(0, 2) if...

  • Can figure out how get my program to keep track of a total score, here is...

    Can figure out how get my program to keep track of a total score, here is my code so far in python 3.6.6 import math import random import turtle def target(): """Turtle drawing the target"""    t = turtle.Turtle() wn = turtle.Screen() wn.bgcolor("black") t.hideturtle() t.speed(0)    #most outside circle worth 10 points t.setposition(0,-275) t.color("grey") t.begin_fill() t.circle(275) t.end_fill()    #2nd most outter circle worth 20 points t.penup() t.setposition(0,-200) t.pendown() t.color("red") t.begin_fill() t.circle(200) t.end_fill()    #3rd most outter circle worth 30 points...

  • Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a ...

    Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a triangle at depth k given the bottom vertex.    vertex: a tuple (x,y) that gives the coordinates of the bottom vertex. When k=0, vertex is the left bottom vertex for the outside triangle. length: the length of the original outside triangle (the biggest one). k: the depth of the input triangle. As k increases by 1, the triangles shrink to a smaller size. When k=0, the...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certai...

    A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certain statistical systems, such as Brownian motion of particles or molecules. Coin flipping is an example of a one-dimensional random walk--one dimensional because you only can go forward (when you flip heads) or backward (when you flip tails) along a straight line. Suppose you take a random walk of nsteps. How many steps away from your starting point would you expect to end up on average, if...

  • Step One The most general object in this hierarchy is the Shape. Begin by creating a...

    Step One The most general object in this hierarchy is the Shape. Begin by creating a class for a Shape. It will have data members xand y, which define the upper left-hand corner of the console where the Shape will be drawn.  For example is x is 5 and y is 7, the upper left-hand corner of the Shape is 5 spaces from the left margin and 7 spaces from the top margin. Due to the limited size of the standard...

  • JavaFX program - Add a second level to the game. //Main game object/class package main; import java.util.ArrayList; impo...

    JavaFX program - Add a second level to the game. //Main game object/class package main; import java.util.ArrayList; import javafx.animation.AnimationTimer; import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringProperty; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Game extends Pane implements Runnable {       // instance variables    private ArrayList<MOB> mobs;    private ArrayList<Rectangle> hitBoxes;    private ArrayList<Treasure> treasure;    private Player player;    private final ImageView background = new ImageView();    private Level level;    private SimpleIntegerProperty score;    private...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT