Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

In a file called fractals.py, write a function vonKochSegment() that takes five arguments: a level (the number of times that you do expansion) a starting

  1. In a file called fractals.py, write a function vonKochSegment() that takes five arguments:
    1. a level (the number of times that you do expansion)
    2. a starting point
    3. a heading (in degrees counterclockwise from due east on the screen)
    4. a length of the line
    5. the graphics window in which to draw the resulting image.
    The function should return the ending point of the segment. Here's how your function should work:
    • if level is zero, then calculate the ending point of the segment (this requires some trigonometry: you want to go length units in the direction of the heading, which means length * cos(heading) in the x direction and -length * sin(heading) in the y direction), and then draw a line from the starting point to the ending point in window.
    • otherwise, level is at least one. You'll draw four level-1 segments:
      • segment 1 starts at the starting point and goes length/3 units in direction heading
      • segment 2 starts where segment 1 ends and goes length/3 units in direction heading - 60
      • segment 3 starts where segment 2 ends and goes length/3 units in direction heading + 60
      • segment 4 starts where segment 3 ends and goes length/3 units in direction heading
  2. Now write a function vonKoch(level, length) that creates a graphics window and draws three von Koch segments of level level arranged in an equilateral triangle. The first one should head in direction 0, the second should head in direction 120 from the end of the first side, and the third should head in direction 240 from the end of the second side. Here is some skeleton code to get you started:

from graphics import *

import math

def vonKochSegment(level, start, heading, length, window):

if level == 0:

end = Point(start.getX() + length*math.cos(math.radians(heading)),

start.getY() + length*math.sine(math.radians(heading)))

line = Line(start,end)

line.draw(window)

return end

else:

#your code goes here!

def vonKoch(length, level):

window = Graph('von Koch Snowflake', length*1.5, length*1.5)

p1 = Point(length/4, length)

p2 = vonKochSegment(level, p1, 0.0. length, window)

#more of your code goes here!

 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions