In: Computer Science
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.
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:-