def computation(number): ''' This function compute the steps which takes that for all distance from vertices of a polygon to its centroid are smaller than 0.001 This function will also plot a new polygon ''' # x and y are arrays to hold x coordinates and y coordinates of a vertex x,y=generate_polygon(number) x_average=average(x) # calculate x bar y_average=average(y) # calculate y bar diff=difference(x,y,x_average,y_average) # if diff is true, means distance of (x_bar,y_bar) and all vertices of a polygon are smaller than 0.001 k=0 while not diff: k+=1 stddraw.clear() stddraw.polygon(x,y) stddraw.show(20) x=average_coordinates(x) # calculate the new x coordinates y=average_coordinates(y) # calculate the new y coordinates x_average=average(x) # x_average is the x bar of this new polygon y_average=average(y) # y_average is the y bar of this new polygon diff=difference(x,y,x_average,y_average) return k
def set_the_gallows(): stddraw.line(6, 7, 6, 8) stddraw.line(6, 8, 9, 8) stddraw.line(9, 8, 9, 1) x = [4, 4, 9, 9] y = [1, 0, 0, 1] stddraw.polygon(x, y)
def drawPDiamond(x, y, colour=stddraw.MAGENTA): x1 = [x - 40, x, x + 40, x] y1 = [y, y + 40, y, y - 40] erase(x, y) stddraw.setPenColor(colour) stddraw.filledPolygon(x1, y1) stddraw.setPenColor(stddraw.BLACK) stddraw.polygon(x1, y1)
def TukeyPlot(b=[]): stddraw.setXscale(0, 6) stddraw.setYscale(b[2] - 1, b[3] + 1) stddraw.line(3, b[2], 3, b[3]) #绘制中线 x = [2, 4, 4, 2] ly = b[0] - b[1] hy = b[0] + b[1] y = [hy, hy, ly, ly] stddraw.polygon(x, y)
def draw_a_parallelogram(game_array): for j in range(9): for k in range(7): if game_array[j][k] == 0: x_axie = [k + 0.4, k + 0.2, k + 0.6, k + 0.8] y_axie = [j + 0.8, j + 0.4, j + 0.4, j + 0.8] stddraw.polygon(x_axie, y_axie) stddraw.setPenColor(stddraw.BLUE) stddraw.filledPolygon(x_axie,y_axie)
def draw_a_regular_triangle(game_array): for j in range(9): for k in range(7): if game_array[j][k] == 5: x_axie = [k + 0.2, k + 0.8, k + 0.5] y_axie = [j + 0.3, j + 0.3, j + 0.7] stddraw.polygon(x_axie, y_axie) stddraw.setPenColor(stddraw.ORANGE) stddraw.filledPolygon(x_axie,y_axie)
def draw_a_inclined(game_array): for j in range(9): for k in range(7): if game_array[j][k] == 3: x_axie = [k + 0.2 , k + 0.5, k + 1 - 0.2, k + 0.5] y_axie = [j + 0.5, j + 0.2, j + 0.5, j + 1 - 0.2] stddraw.polygon(x_axie, y_axie) stddraw.setPenColor(stddraw.GREEN) stddraw.filledPolygon(x_axie,y_axie)
def draw_a_square(game_array): for j in range(9): for k in range(7): if game_array[j][k] == 2: x_axie = [k + 0.2, k + 0.2, k + 0.8, k + 0.8] y_axie = [j + 0.8, j + 0.2, j + 0.2, j + 0.8] stddraw.polygon(x_axie, y_axie) stddraw.setPenColor(stddraw.YELLOW) stddraw.filledPolygon(x_axie,y_axie)
def drawBHexagon(x, y): a = 20 * math.sqrt(3) x1 = [x - 40, x - 20, x + 20, x + 40, x + 20, x - 20] y1 = [y, y + a, y + a, y, y - a, y - a] erase(x, y) stddraw.setPenColor(stddraw.BLUE) stddraw.filledPolygon(x1, y1) stddraw.setPenColor(stddraw.BLACK) stddraw.polygon(x1, y1)
def sierpinski(n, x, y, length): if n == 1: stddraw.polygon([0, 1, 1 / 2], [0, 0, math.sqrt(3) / 2]) return filledTriangle(x, y, length) else: filledTriangle(x, y, length) return sierpinski(n - 1, x - (length / 2), y, length / 2), sierpinski( n - 1, x + (length / 2), y, length / 2), sierpinski(n - 1, x, y + height(length), length / 2)
def drawGParallelogram(x, y): a = 14 * math.sqrt(3) x1 = [x - 42, x - 14, x + 42, x + 14] y1 = [y - a, y + a, y + a, y - a] erase(x, y) stddraw.setPenColor(stddraw.GREEN) stddraw.filledPolygon(x1, y1) stddraw.setPenColor(stddraw.BLACK) stddraw.polygon(x1, y1)
def drawOTriangle(x, y): a = 20 * math.sqrt(3) x1 = [x - 40, x, x + 40] y1 = [y - a, y + a, y - a] erase(x, y) stddraw.setPenColor(stddraw.ORANGE) stddraw.filledPolygon(x1, y1) stddraw.setPenColor(stddraw.BLACK) stddraw.polygon(x1, y1)
def draw_a_pentagon(game_array): for j in range(9): for k in range(7): if game_array[j][k] == 1: x_axie = [k + 0.2, k + 0.5, k + 0.8, k + 0.7, k + 0.3] y_axie = [j + 0.5, j + 0.2, j + 0.5, j + 0.8, j + 0.8] stddraw.setPenColor(stddraw.PINK) stddraw.filledPolygon(x_axie, y_axie) stddraw.setPenRadius(0.003) stddraw.polygon(x_axie, y_axie)
def paper(n): if n == 1: global width, height x = [0, width, width, 0] y = [height, height, 0, 0] stddraw.polygon(x, y) return x, y t = paper(n - 1) if n % 2 == 0: x = t[0] y = [0] * 4 for i in range(4): y[i] = t[1][i] / 2 else: x = [0] * 4 for i in range(4): x[i] = t[0][i] / 2 y = t[1] stddraw.polygon(x, y) return x, y
def uncross_step(number): ''' This function will calcuate and return the values k that after k steps, the polygon does not have corssing edges This function has a cross_edge() function inside, which will determine whether or not the current polygon has crossing edge ''' stddraw.clear() stddraw.setXscale(-1,1) stddraw.setYscale(-1,1) stddraw.setPenRadius(0.001) stddraw.setPenColor(stddraw.BLACK) x,y=generate_polygon(number) stddraw.polygon(x,y) stddraw.show(20) # move the polygon to the middle x,y=remove_centroid(x,y) stddraw.clear() # draw the new polygon stddraw.polygon(x,y) stddraw.show(20) # cross_edge() will determine whether or not this polygon has crossing edge cross=crossing_edge(x,y) k=0 while cross==False: stddraw.clear() stddraw.polygon(x,y) stddraw.show(20) x=average_coordinates(x) # calculate the new x coordinates y=average_coordinates(y) # calculate the new y coordinates x,y=normalize(x,y) k+=1 cross=crossing_edge(x,y) stddraw.clear() stddraw.polygon(x,y) return k
def drawShapes(number,x,y): stddraw.setPenColor(stddraw.BLACK) stddraw.setPenRadius(0.01) if number == 0: stddraw.square(x,y,0.4) stddraw.setPenColor(stddraw.YELLOW) stddraw.filledSquare(x,y,0.4) if number == 1: stddraw.setPenColor(stddraw.RED) stddraw.filledCircle(x,y,0.4) stddraw.setPenColor(stddraw.BLACK) stddraw.setPenRadius(0.004) stddraw.circle(x,y,0.4) if number == 2: xs = [x-0.4,x+0.4,x] ys = [y-0.4,y-0.4,y+0.4] stddraw.polygon(xs,ys) stddraw.setPenColor(stddraw.ORANGE) stddraw.filledPolygon(xs,ys) if number == 3: xs = [x-0.4,x,x+0.4,x] ys = [y,y+0.4,y,y-0.4] stddraw.polygon(xs,ys) stddraw.setPenColor(stddraw.GREEN) stddraw.filledPolygon(xs,ys) if number == 4: xs = [x-0.4,x-0.2,x+0.2,x+0.4,x] ys = [y,y+0.4,y+0.4,y,y-0.4] stddraw.polygon(xs,ys) stddraw.setPenColor(stddraw.MAGENTA) stddraw.filledPolygon(xs,ys) if number == 5: xs = [x-0.4,x-0.2,x+0.4,x+0.2] ys = [y-0.4,y+0.4,y+0.4,y-0.4] stddraw.polygon(xs,ys) stddraw.setPenColor(stddraw.BLUE) stddraw.filledPolygon(xs,ys)
stddraw.setXscale(-1.0, 1.0) stddraw.setYscale(-1.0, 1.0) points_x = [-0.3, 0 , 0.3] points_y = [-0.3, 0.4, -0.3] # post-condicion: número de puntos en x es la misma que en y assert len(points_x) == len(points_y) n = len(points_x) # velocidad angular speed_rot = 0.1 # en radianes while True: stddraw.clear(stddraw.BLACK) stddraw.setPenColor(stddraw.WHITE) # calculate rotations for i in range(n): newx = points_x[i]*cos(speed_rot) - points_y[i]*sin(speed_rot) newy = points_x[i]*sin(speed_rot) + points_y[i]*cos(speed_rot) points_x[i] = newx points_y[i] = newy # display triangle stddraw.polygon(points_x, points_y) # copy buffer to screen stddraw.show(0) stddraw.pause(20)
def drawPiece(x, y, ay): stddraw.setPenRadius(0.008) if board[y][x] == 0: #diamond FILL = Color(250,252,255) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.2, x+0.35, x+0.65, x+0.8] ys = [y+ay+0.2, y+ay+0.5, y+ay+0.7, y+ay+0.7, y+ay+0.5] stddraw.filledPolygon(xs, ys) OUTLINE = Color(220,225,255) stddraw.setPenColor(OUTLINE) xs = [x+0.5, x+0.2, x+0.35, x+0.65, x+0.8] ys = [y+ay+0.2, y+ay+0.5, y+ay+0.7, y+ay+0.7, y+ay+0.5] stddraw.polygon(xs, ys) if board[y][x] == 1: #emerald FILL = Color(180,255,180) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.7, x+0.7, x+0.5, x+0.3, x+0.3] ys = [y+ay+0.8, y+ay+0.6, y+ay+0.4, y+ay+0.2, y+ay+0.4, y+ay+0.6] stddraw.filledPolygon(xs, ys) FILL = Color(210,255,210) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.7, x+0.5, x+0.3] ys = [y+ay+0.8, y+ay+0.6, y+ay+0.4, y+ay+0.6] stddraw.filledPolygon(xs, ys) OUTLINE = Color(100,225,100) stddraw.setPenColor(OUTLINE) xs = [x+0.5, x+0.7, x+0.7, x+0.5, x+0.3, x+0.3] ys = [y+ay+0.8, y+ay+0.6, y+ay+0.4, y+ay+0.2, y+ay+0.4, y+ay+0.6] stddraw.polygon(xs, ys) if board[y][x] == 2: #ruby FILL = Color(245,140,140) stddraw.setPenColor(FILL) xs = [x+0.65, x+0.75, x+0.75, x+0.65, x+0.35, x+0.25, x+0.25, x+0.35] ys = [y+ay+0.8, y+ay+0.7, y+ay+0.3, y+ay+0.2, y+ay+0.2, y+ay+0.3, y+ay+0.7, y+ay+0.8] stddraw.filledPolygon(xs, ys) FILL2 = Color(255,180,180) stddraw.setPenColor(FILL2) xs = [x+0.65, x+0.75, x+0.5, x+0.25, x+0.35] ys = [y+ay+0.8, y+ay+0.7, y+ay+0.5, y+ay+0.7, y+ay+0.8] stddraw.filledPolygon(xs, ys) OUTLINE = Color(205,80,80) stddraw.setPenColor(OUTLINE) xs = [x+0.65, x+0.75, x+0.75, x+0.65, x+0.35, x+0.25, x+0.25, x+0.35] ys = [y+ay+0.8, y+ay+0.7, y+ay+0.3, y+ay+0.2, y+ay+0.2, y+ay+0.3, y+ay+0.7, y+ay+0.8] stddraw.polygon(xs, ys) if board[y][x] == 3: #saphhire FILL = Color(60,120,255) stddraw.setPenColor(FILL) stddraw.filledCircle(x+0.5, y+ay+0.5, 0.3) FILL = Color(80,190,255) stddraw.setPenColor(FILL) stddraw.filledCircle(x+0.4, y+ay+0.6, 0.1) OUTLINE = Color(20,50,255) stddraw.setPenColor(OUTLINE) stddraw.circle(x+0.5, y+ay+0.5, 0.3) if board[y][x] == 4: #topaz FILL = Color(255,255,200) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.2, x+0.3, x+0.7] ys = [y+ay+0.2, y+ay+0.5, y+ay+0.7, y+ay+0.7] stddraw.filledPolygon(xs, ys) OUTLINE = Color(225,225,100) stddraw.setPenColor(OUTLINE) stddraw.polygon(xs, ys) if board[y][x] == 5: #amethyst FILL = Color(225,170,225) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.8, x+0.5, x+0.2] ys = [y+ay+0.8, y+ay+0.5, y+ay+0.2, y+ay+0.5] stddraw.filledPolygon(xs, ys) FILL = Color(245,180,245) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.5, x+0.2] ys = [y+ay+0.8, y+ay+0.4, y+ay+0.5] stddraw.filledPolygon(xs, ys) OUTLINE = Color(155,100,155) stddraw.setPenColor(OUTLINE) xs = [x+0.5, x+0.8, x+0.5, x+0.2] ys = [y+ay+0.8, y+ay+0.5, y+ay+0.2, y+ay+0.5] stddraw.polygon(xs, ys) if board[y][x] == 6: #aquamarine FILL = Color(100,250,220) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.8, x+0.7, x+0.3, x+0.2] ys = [y+ay+0.8, y+ay+0.4, y+ay+0.2, y+ay+0.2, y+ay+0.4] stddraw.filledPolygon(xs, ys) FILL = Color(190,255,250) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.7, x+0.6, x+0.4, x+0.3] ys = [y+ay+0.7, y+ay+0.45, y+ay+0.3, y+ay+0.3, y+ay+0.45] stddraw.filledPolygon(xs, ys) OUTLINE = Color(50,200,170) stddraw.setPenColor(OUTLINE) xs = [x+0.5, x+0.8, x+0.7, x+0.3, x+0.2] ys = [y+ay+0.8, y+ay+0.4, y+ay+0.2, y+ay+0.2, y+ay+0.4] stddraw.polygon(xs, ys) if board[y][x] == 7: #citrine FILL = Color(255,160,70) stddraw.setPenColor(FILL) xs = [x+0.3, x+0.2, x+0.7, x+0.7] ys = [y+ay+0.2, y+ay+0.6, y+ay+0.8, y+ay+0.3] stddraw.filledPolygon(xs, ys) FILL = Color(255,190,90) stddraw.setPenColor(FILL) xs = [x+0.5, x+0.2, x+0.7, x+0.7] ys = [y+ay+0.5, y+ay+0.6, y+ay+0.8, y+ay+0.6] stddraw.filledPolygon(xs, ys) OUTLINE = Color(205,100,30) stddraw.setPenColor(OUTLINE) xs = [x+0.3, x+0.2, x+0.7, x+0.7] ys = [y+ay+0.2, y+ay+0.6, y+ay+0.8, y+ay+0.3] stddraw.polygon(xs, ys)
import stddraw import numpy as np stddraw.setCanvasSize(500, 500) stddraw.setXscale(-1.0, 1.0) stddraw.setYscale(-1.0, 1.0) points = np.array([[-0.3, -0.3], [0, 0.3], [0.3, -0.3]]) n = points.shape[0] #numero de filas en matriz point # velocidad angular speed_rot = 0.1 # en radianes matrix_rot = np.array([[np.cos(speed_rot), -np.sin(speed_rot)], [np.sin(speed_rot), np.cos(speed_rot)]]) while True: stddraw.clear(stddraw.BLACK) stddraw.setPenColor(stddraw.WHITE) # calculate rotations for i in range(n): points[i] = matrix_rot.dot(points[i]) # display triangle stddraw.polygon(points[:, 0], points[:, 1]) # copy buffer to screen stddraw.show(0) stddraw.pause(20)
y[i] = t[1][i] / 2 else: x = [0] * 4 for i in range(4): x[i] = t[0][i] / 2 y = t[1] stddraw.polygon(x, y) return x, y width = 10 height = width * 2**0.5 stddraw.setXscale(-1, height + 1) stddraw.setYscale(-1, height + 1) '''测试用 x = [0,width,width,0] y = [height,height,0,0] stddraw.polygon(x,y) #stddraw.square(5,5,2.5) for i in range(len(y)): y[i] = y[i]/2 stddraw.polygon(x,y) ''' #x = [0,width,width,0] #y = [height,height,0,0] n = eval(sys.argv[1]) #n = 2 paper(n) stddraw.show()