""" April 26, 2020 CSE 30 Spring 2020 Program 1 @author: Fahim, Decker Krogh The code is currently set up for level 6 which I think looks really nice. It takes a lot of processing power, however, for the transform fn to spin it. """ import math import turtle import random VERTICES = [] TRIANGLES = [] iC = 0 def pointGen(p1,p2,level,roughness=1): x = ((p1[0])+(p2[0]))/2 y = ((p1[1])+(p2[1]))/2 z = (((p1[2])+(p2[2]))/2) + ((((level+.1)*(level+.1))*roughness)/(40) * random.random()) return [x,y,z] def midpoint_displacement(p1, p2, p3, level,roughness): if len(VERTICES) == 0: #this whole if/else is just appending points to list. VERTICES.append(p1) ip1 = len(VERTICES) - 1 VERTICES.append(p2) ip2 = len(VERTICES) - 1 VERTICES.append(p3) ip3 = len(VERTICES) - 1 else: tempList = VERTICES.copy() p1inList = False p2inList = False p3inList = False for i in tempList: # using iteration instead of in keyword because I don't want z to match (creates holes) if (i[0] == p1[0]) and (i[1] == p1[1]): p1inList = True ip1 = VERTICES.index(i) if (i[0] == p2[0]) and (i[1] == p2[1]): p2inList = True ip2 = VERTICES.index(i) if (i[0] == p3[0]) and (i[1] == p3[1]): p3inList = True ip3 = VERTICES.index(i) if p1inList == False: VERTICES.append(p1) ip1 = len(VERTICES) - 1 if p2inList == False: VERTICES.append(p2) ip2 = len(VERTICES) - 1 if p3inList == False: VERTICES.append(p3) ip3 = len(VERTICES) - 1 if level == 0: TRIANGLES.append([ip1, ip2, ip3]) #append triangle of verts else: np12, np23, np31 = pointGen(p1, p2, level,roughness), pointGen(p2, p3, level,roughness), pointGen(p3, p1, level,roughness) midpoint_displacement(np12,np23,np31,level-1,roughness) midpoint_displacement(p1,np12,np31, level-1,roughness) midpoint_displacement(p2,np12,np23, level-1,roughness) midpoint_displacement(p3,np23,np31, level-1,roughness) def transform(x, y, z, angle, tilt): # Animation control (around y-axis). If considered as a view of earth from space, it's moving over the equator. s, c = math.sin(angle), math.cos(angle) x, z = x * c - z * s, x * s + z * c # Camera tilt (around x-axis). If considered as a view of earth from space, the tilt angle is measured from the equator. s, c = math.sin(tilt), math.cos(tilt) y, z = y * c - z * s, y * s + z * c # Setting up View Parameters z += 5 # Fixed Distance from top FOV = 1000 # Fixed Field of view f = FOV / z sx, sy = x * f, y * f return sx, sy def main(): print ('AllTheColor?(y/n)',end='') multicoloredIn = str(input()) if multicoloredIn == 'y': multicolored = True else: multicolored = False colors = ["red", "orange", "green", "cyan", "blue", "purple", "magenta", "pink"] # Create terrain using turtle terrain = turtle.Turtle() terrain.pencolor("red") turtle.bgcolor('black') terrain.pensize(2) # Turn off move time for instant drawing turtle.tracer(0, 0) terrain.up() angle = 0 print("Calculating points...") midpoint_displacement([2,1,.8],[0,-2,-.8],[-2,1,-.8],6,.8) #last two are level and roughness. roughness has recommended range of 0 to 1 (put more roughness if dropping level) print("Points Calculated") global VERTICES global TRIANGLES VERTICES = [tuple(x) for x in VERTICES] TRIANGLES = [tuple(x) for x in TRIANGLES] print(VERTICES) print(TRIANGLES) while True: # Clear the screen terrain.clear() # Transform the terrain VERT2D = [] for vert3D in VERTICES: x, z, y = vert3D #switched y and z sx, sy = transform(x, y, z, angle, -.2) #-.2 tilt for rotating around object. 1.57 for top-down VERT2D.append((sx, sy)) global iC iC = iC+1 if iC<7 else 0 for triangle in TRIANGLES: if multicolored: iC = iC+1 if iC<7 else 0 terrain.pencolor(colors[iC]) else: terrain.pencolor('red') points = [] points.append(VERT2D[triangle[0]]) points.append(VERT2D[triangle[1]]) points.append(VERT2D[triangle[2]]) # Draw the trangle terrain.goto(points[0][0], points[0][1]) terrain.down() terrain.goto(points[1][0], points[1][1]) terrain.goto(points[2][0], points[2][1]) terrain.goto(points[0][0], points[0][1]) terrain.up() # Update screen turtle.update() # Control the speed of animation angle += 0.1 if __name__ == "__main__": main()