Question

In: Computer Science

FOR SAGE PYTHON Koch snowflake: The Koch snowflake can be constructed by starting with an equilateral...

FOR SAGE PYTHON

Koch snowflake: The Koch snowflake can be constructed by starting with an equilateral triangle, then recursively altering each line segment as follows: Step 1: divide the line segment into three segments of equal length. Step 2: draw an equilateral triangle that has the middle segment from step 1 as its base and points outward. Step 3: remove the line segment that is the base of the triangle from step 2. After one iteration of this process, the resulting shape is the outline of a hexagram. The Koch snowflake is formed by repeating this process over and over again (in principle, infinitely many times, but in practice we will stop after a certain number of iterations). Your goal is to write a program which draws the Koch snowflake, following these instructions:

(a) Don’t attempt to draw any lines yet, you should do that only at the very end. Instead, you should create a list of all vertices (points) which you want to connect in the end.

(b) Write a function, called koch_bump(), which takes as input two tuples (the coordinates of two points P = (x1, y1) and Q = (x2, y2)), and which returns a list of 4 tuples, starting with P and followed by the three points between P and Q which form the ”bump” pointing outwards from the line segment. Hint: Linear Algebra (vector geometry) can make this a lot easier, but you can also figure out the coordinates of the needed points using basic high school geometry

(c) Write a function, called koch_iteration(), which takes as input a list of tuples (think of these as the vertices of the equilateral triangle you start with), and which returns a new list of tuples which contains all points after applying the subdivision process exactly once. To do this, go through the given list of points, and apply the function koch_bump() to each two adjacent ones (don’t forget to also apply it to the last and first points in the list): For example, if the list contains the points A, B, C, you should call koch_bump() on the pairs (A, B), (B, C), and (C, A) — but you should write the function so that it can deal with an arbitrary number of points in the list.

(d) Write a function, called koch_snowflake(), which takes as input a list of tuples (the vertices of the starting polygon), an integer maxiter, and the optional argument filled (which defaults to False) and which produces the picture of the Koch snowflake obtained by applying koch_iteration() maxiter times, starting with the vertices given in the list. Hint: To connect a list of points (tuples) by line segments, you can use the polygon2d() command. Read its documentation, you probably want to use the options fill=filled,axes=False.

(e) Write a function, called regpolygon(), which takes as input an integer k, and which returns a list of k tuples which are the vertices of the regular polygon with radius 1. In other words, the k points should lie on the unit circle and should be evenly spaced apart. Hint: Use cos α and sin α, where α is a multiple of 2π/k.

(f) Draw the Koch snowflake after 5 iterations when starting with an equilateral triangle. Hint: Use koch_snowflake(regpolygon(3),5).

(g) Draw the Koch snowflake after 5 iterations when starting with a square.

(h) Draw the Koch snowflake after 5 iterations when starting with a pentagon.

(i) Repeat the last three parts, but with the option filled=True.

Solutions

Expert Solution

from turtle import *

# function to create koch snowflake
def snowflake(length_side, levels):
    if levels == 0:
        forward(length_side)
        return
    length_side /= 3.0
    snowflake(length_side, levels - 1)
    left(60)
    snowflake(length_side, levels - 1)
    right(120)
    snowflake(length_side, levels - 1)
    left(60)
    snowflake(length_side, levels - 1)


# main function 
if __name__ == "__main__":
    # defining the speed of the turtle 
    speed(0)
    length = 300.0

    # Pull the pen up – no drawing when moving. 
    penup()

    # Move the turtle backward by distance,  
    # opposite to the direction the turtle  
    # is headed. 
    # Do not change the turtle’s heading. 
    backward(length / 2.0)

    # Pull the pen down – drawing when moving. 
    pendown()
    for i in range(3):
        snowflake(length, 4)
        right(120)

        #To control the closing windows of the turtle
    mainloop()  

output:-


Related Solutions

The Koch snowflake is formed by making an equilateral triangle of 3 congruent Koch curves at...
The Koch snowflake is formed by making an equilateral triangle of 3 congruent Koch curves at each stage of the iteration. The perimeter of this snowflake is infinite based on the Koch curve results, while its area is finite. Convince the reader that the following is true. Justify with algebra that (1) the Koch snowflake has an infinite perimeter as n approaches infinity and that (2) the Koch snowflake has a finite area as n approaches infinity.
How to approximate smooth functions over koch snowflake? Please provide in details
How to approximate smooth functions over koch snowflake? Please provide in details
Draw a Koch Snowflake fractal image using MATLAB programming. I'm pretty new to MATLAB and im...
Draw a Koch Snowflake fractal image using MATLAB programming. I'm pretty new to MATLAB and im not too familar with how all the plotting and drawing functions work.
If equilateral triangles are constructed on the sides of any triangle, prove that the distances between...
If equilateral triangles are constructed on the sides of any triangle, prove that the distances between the vertices of the original triangle and the opposite vertices of the equilateral triangles are equal.
python programming • Appropriate and well constructed while and/or for loops (as necessary). • Appropriate if,...
python programming • Appropriate and well constructed while and/or for loops (as necessary). • Appropriate if, if-else, if-elif-else statements (as necessary). • The use of the ord() and chr() functions (as necessary). • The following three functions (refer to stage 6 for description): o display_details() o get_menu_choice() o get_offset() • Output that strictly adheres to the assignment specifications. If you are not sure about these details, you should check with the ‘Sample Output – Part II’ provided at the end...
1. A water slide is constructed so that swimmers, starting from rest at the top of...
1. A water slide is constructed so that swimmers, starting from rest at the top of the slide, leave the end of the slide traveling horizontally. As the drawing shows, one person hits the water 5.00 m from the end of the slide in a time of 1.500 s after leaving the slide. Ignoring friction and air resistance, find the height H in the drawing. The answer is 11.6 m (please explain) 2. An extreme skier, starting from rest, coasts...
Use Python Return a password string constructed with the following rules: (1) If there are only...
Use Python Return a password string constructed with the following rules: (1) If there are only 0 or 1 numeric among 3 parameters, concatenate the string form of all parameters directly. (2) If there are 2 numeric parameters, calculate the sum of 2 numeric values if they are both int, or calculate the product of them and round to 2 decimal places if any of them is float. Finally, append this sum/product to the string parameter. You may assume that...
Starting out with python Lists and Tuples - US Population Data In this assignment, you will...
Starting out with python Lists and Tuples - US Population Data In this assignment, you will be reading the contents of a file into a list. This file contains the midyear population of the United States, in thousands, during the years 1950 through 1990. The first line in the file contains the population for 1950, the second line contains the populations for 1951, and so forth. You will ask the user to input a specific year to check the population...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT