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
  • /* * 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); * ------------------------------------------- *...

  • 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...

  • 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...

  • I want the math lab code for theses problems in a unique way

    I want the math lab code for theses problems in a unique way Theory A projectile is launched from point A up an incline plane that makes an angle of 10 with the horizontal. The mountain is 6,000 m high. Vo 10° Figure 1: Flightpath of a projectile. If we neglect the air resistance, the flight path of a projectile launched at an initial speed vo and an angle θ of departure relative to the horizontal is a parabola (see...

  • Python PageRank-Nibble Parallel PageRank-Nibble is described in in Section 3.3 of [1]. At a high level,...

    Python PageRank-Nibble Parallel PageRank-Nibble is described in in Section 3.3 of [1]. At a high level, the algorithm works as follows: each node i in the graph gets two new quantities, p[i] and r[i].  p values will indicate the PageRank score, and r holds a "residual" add 1 unit of "mass" to r[seed] iteratively, for each node i with "enough residual" r[i] transfer some proportion of the mass in residual r[i] to PageRank value p[i] distribute some proportion of the remaining...

  • Given a starting sphere code, modify the Java and HTML code to create multiple spheres that...

    Given a starting sphere code, modify the Java and HTML code to create multiple spheres that will rotate in different positions at different speeds. basicSphere.js: "use strict"; var canvas; var gl; var numTimesToSubdivide = 3; var index = 0; var pointsArray = []; var va = vec4(0.0, 0.0, -1.0,1); var vb = vec4(0.0, 0.942809, 0.333333, 1); var vc = vec4(-0.816497, -0.471405, 0.333333, 1); var vd = vec4(0.816497, -0.471405, 0.333333,1); var program; var program1; function triangle(a, b, c) {    pointsArray.push(a);...

  • Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development p...

    Designing functions Reminder: When designing functions, follow the given steps to reinforce good software development practices and problem solving strategies: a) Start by writing the documentation for the function. Use docstrings shown in lecture and lab. b) Write test calls to the function in your main function for the different cases of input the function will encounter. This will help you understand the behavior of the function and later behave as tests for your function. c) Define the function d)...

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
ADVERTISEMENT