def line(self): stddraw.setYscale(-1.5, 1.5) stddraw.setXscale(-1.5, 1.5) # try: done_point = [] for (x1, y1) in zip(self.x, self.y): if (x1, y1) not in done_point: done_point.append((x1, y1)) for (x2, y2) in zip(self.x, self.y): if (x1, y1) != (x2, y2) and self.possibility(): color_list = [ stddraw.LIGHT_GRAY, stddraw.RED, stddraw.BLUE ] color_choice = random.sample(color_list, 1) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.setPenRadius(r=0.01) stddraw.line(x1, y1, x2, y2) stddraw.show(20) else: pass else: pass for (x, y) in zip(self.x, self.y): stddraw.setPenRadius(r=0.02) stddraw.setPenColor(stddraw.BLACK) stddraw.point(x, y)
def draw_line(dragonline, turn): stddraw.setXscale(-5, 5) stddraw.setYscale(-5, 5) stddraw.setFontSize(20) theta = 0 x_list = [0, 1] y_list = [0, 0] stddraw.line(x_list[0], y_list[0], x_list[1], y_list[1]) x = 1 y = 0 List = dragonline.turn_flow(turn) Turn = [(1, 0), (0, 1), (-1, 0), (0, -1)] for index in range(2 * (turn - 1) + 1): det_theta = 1 if List[index] == 'L' else -1 theta = (theta + det_theta) % 4 det_x = Turn[theta][0] det_y = Turn[theta][1] x = x + det_x y = y + det_y x_list.append(round(x)) y_list.append(round(y)) stddraw.setPenColor(stddraw.BLACK) stddraw.line(x_list[index], y_list[index], x_list[index + 1], y_list[index + 1]) stddraw.setPenColor(stddraw.RED) # stddraw.text(x_list[index],y_list[index],'({0},{1}) to ({2},{3})'.format(x_list[index],y_list[index],x_list[index+1],y_list[index+1])) # stddraw.text(x_list[index],y_list[index],str(index)) stddraw.show(50)
def draw_move(dict, a, c, location): print('enter move') stddraw.line(0, -1, 10, -1) stddraw.setFontSize(50) stddraw.line(location['a'][0], location['a'][1], location['a'][2], location['a'][3]) stddraw.line(location['b'][0], location['b'][1], location['b'][2], location['b'][3]) stddraw.line(location['c'][0], location['c'][1], location['c'][2], location['c'][3]) for d in dict.keys(): stddraw.text(location[c][0], 9, a + '-->' + c) n = len(dict[d]) if n == 0: pass else: for i in range(1, n + 2): print(i) stddraw.line(location[d][0] - i / n, 8 * (n + 1 - i) / (n + 1), location[d][0] + i / n, 8 * (n - i + 1) / (n + 1)) stddraw.text(location[d][0] - i / n - 0.5, 8 * (n - i) / n, str(i)) pass stddraw.show(300)
def main(filename, theta): #通过命令行输入图片地址,以及最大角度 img = picture(filename) width = img.width() height = img.height() new_img = picture(width, height) #用极坐标来重新画图 for alpha in range(2 * math.pi): #遍历从 0 到360度 length = get_length(alpha) # 获取该图片在该角度下,极坐标对应的轴长 for r in range(length): col = r * math.cos(alpha) row = r * math.sin(alpha) pix = img.get(col, row) beta = r / length * theta new_col, new_row = swirl(col, row, beta) new_img.set(new_col, new_row, pix) # for col in range(width): # for row in range(height): # # alpha = getangle(col,row) #获取该位置所对应的角度 # ratio = getratio(col,row,alpha) #获取该位置所对应的角度变化比例 # beta = theta*ratio #应该旋转的角度 # pix = img.get(col,row) #原始图片该位置的像素值 # # new_col,new_row = swirl(col,row,beta) #给新图片该位置对应旋转后新位置 # new_img.set(new_col,new_row,pix) #给新图片新位置赋上像素值 stddraw.picture(new_img) #画出新图片 stddraw.show() #展示新图片
def draw_spirograph(R,r,a): stddraw.setXscale(-40,40) stddraw.setYscale(-40,40) l = 1500 x = stdarray.create1D(l,0) y = stdarray.create1D(l,0) for t in range(l): x[t] = (R+r)*math.cos(t) - (r+a) * math.cos((R+r)*t/r) y[t] = (R+r)*math.sin(t) - (r+a) * math.sin((R+r)*t/r) for t in range(l+1): if t >= l-1: pass else: if t ==0: stddraw.setPenColor(stddraw.RED) if t==l/3: stddraw.setPenColor(stddraw.DARK_BLUE) if t == 2*l/3: stddraw.setPenColor(stddraw.BOOK_BLUE) stddraw.line(x[t],y[t],x[t+1],y[t+1]) stddraw.show(5) stddraw.show()
def draw(list): for i in range(len(list) - 1): x0 = list[i][0] y0 = list[i][1] x1 = list[i + 1][0] y1 = list[i + 1][1] stddraw.line(x0, y0, x1, y1) stddraw.show(20)
def draw_possibility(): print('enter draw_possibility') stddraw.setYscale(-0.5,100) stddraw.setXscale(-50,400) for i in range(len(possibility)-1): stddraw.line(i,possibility[i],i+1,possibility[i+1]) stddraw.show()
def main(length,trials): count1 = doubletrial(insert,30,length,trials) count2 = doubletrial(merge.insert,30,length,trials) print(count1,'\n',count2) draw_count([count1,count2]) stddraw.show()
def main(): n = int(sys.argv[1]) p = float(sys.argv[2]) test = random(n, p) stddraw.setPenColor(stddraw.BLACK) draw(test, False) stddraw.setPenColor(stddraw.BLUE) draw(test, True) stddraw.show()
def main(): n = int(sys.argv[1]) time = float(sys.argv[2]) table = Table(n) while True: table.time_evolve(time) table.draw() stddraw.show(20) stddraw.clear()
def main(): universe = Universe(sys.argv[1]) dt = float(sys.argv[2]) count = 0 while True: universe.increaseTime(dt) stddraw.clear() universe.draw() stddraw.show(10) print(count) count += 1
def main(): n = int(sys.argv[1]) p = float(sys.argv[2]) trials = int(sys.argv[3]) histogram = Histogram(n + 1) for t in range(trials): heads = stdrandom.binomial(n, p) histogram.addDataPoint(heads) stddraw.setCanvasSize(500, 200) histogram.draw() stddraw.show()
def main(): duration = int(input('what is the duration?(min):')) status = input('start to work?(Y/N)') while status == 'Y': stddraw.clear() stddraw.setXscale(0, 10) stddraw.setYscale(0, 10) time.sleep(duration * 60) stddraw.text(5, 5, 'go to do sport') stddraw.show() status = input('continue to work?(Y/N') if status == 'N': break
def run(n): dice_1 = [1, 3, 4, 5, 6, 8] dice_2 = [1, 2, 2, 3, 3, 4] dice_s = [1, 2, 3, 4, 5, 6] result1 = dice(dice_1, dice_2, n) result2 = dice(dice_s, dice_s, n) print(result1, result2) stddraw.setYscale(0, 1.1 * max(max(result1), max(result2))) stdstats.plotLines(result1) stdstats.plotBars(result2) stddraw.show(50)
def audio_note(self): stddraw.setXscale(0, 50) stddraw.setYscale(-10, 10) print('enter audio_note') duration = self.f hz = notes[self.n - 1] n = int(SPS * duration) samples = stdarray.create1D(n + 1, 0.0) for i in range(n + 1): samples[i] = math.sin(2.0 * math.pi * i * hz / SPS) print(samples) for i in range(0, n - 100, 100): stddraw.line(i / 500, samples[i], (i + 100) / 500, samples[i + 100]) stddraw.show() stdaudio.playSample(samples) print('play') stdaudio.wait()
def blome(self): stddraw.setXscale(-1.2, 1.2) stddraw.setYscale(-1.2, 1.2) x_list = [] y_list = [] for i in range(1, self.n + 1): theta = self.theta for n in range(100): x = math.sin(theta) * math.cos(theta) y = math.sin(theta) * math.sin(theta) x_list.append(x) y_list.append(y) if n > 0: stddraw.line(x_list[n - 1], y_list[n - 1], x_list[n], y_list[n]) stddraw.show(50) theta += n * self.theta / 100
def lifegame(life): ''' recursive return the adjacent part block :param x: :param y: :return: ''' stddraw.clear() draw(life) stddraw.show(500) for line in life: print(line) life2 = stdarray.create2D(10, 10, 0) for x in range(10): for y in range(10): print('x is {0},y is {1}'.format(x, y)) a = 0 for i in range(x - 1, x + 2): # print('i is {0}'.format(i)) for j in range(y - 1, y + 2): # print('j is {0}'.format(j)) if i < 0 or i > 9 or j < 0 or j > 9: pass else: a += life[i][j] print( 'i is {0},j is {1},life[{0}][{1}] is {3},a is{2}'. format(i, j, a, life[i][j])) if a == 0: print(x, y, 'no alive in 9') pass else: life2[x][y] = isalive(life, x, y) lifegame(life2)
def fade(file1, file2): print('can support extension:', pygame.image.get_extended()) source = Picture(file1) target = Picture(file2) n = 5 width = source.width() height = source.height() stddraw.setCanvasSize(width, height) pic = Picture(width, height) for t in range(n + 1): for col in range(width): for row in range(height): c0 = source.get(col, row) cn = target.get(col, row) alpha = 1.0 * t / n pic.set(col, row, blend(c0, cn, alpha)) stddraw.picture(pic) stddraw.show(2) stddraw.show()
def main(): # parser = optparse.OptionParser("""\ # usage: %prog [options] infile outfile # # draw circles in random location ,with random radius and color.""") # # parser.add_option("-n", "--number", dest="n", # help=("input the number")) # # parser.add_option("-p", "--possibility", dest="p", # help=("input the possibility in black")) # # # opts, args = parser.parse_args() # # n = int(opts.n) # p = float(opts.p) stddraw.setXscale(0,2) stddraw.setYscale(0,2) i = 0 while i < n: x = 2*random.random() y = 2*random.random() r = 0.01*random.randrange(1,30) color = stddraw.BLUE if random.random()<p else stddraw.BOOK_RED stddraw.setPenColor(color) stddraw.filledCircle(x,y,r) i +=1 stddraw.show(10)
def recursive_tree(n, A0, r, x0, y0): if n == 0: return xa = x0 + r * math.cos((A0 + A) / 360 * 2 * math.pi) ya = y0 + r * math.sin((A0 + A) / 360 * 2 * math.pi) stddraw.line(x0, y0, xa, ya) stddraw.show(50) recursive_tree(n - 1, A0 + A, r * a, xa, ya) xb = x0 + r * math.cos((A0 + B) / 360 * 2 * math.pi) yb = y0 + r * math.sin((A0 + B) / 360 * 2 * math.pi) stddraw.line(x0, y0, xb, yb) stddraw.show(50) recursive_tree(n - 1, A0 + B, r * b, xb, yb) xc = x0 + r * math.cos((A0 + C) / 360 * 2 * math.pi) yc = y0 + r * math.sin((A0 + C) / 360 * 2 * math.pi) stddraw.line(x0, y0, xc, yc) stddraw.show(50) recursive_tree(n - 1, A0 + C, r * c, xc, yc)
v1x = v2x v2x = a changepoint.append((r1x, r1y, r2x, r2y)) # stddraw.line(r1x,r1y,r2x,r2y) # stddraw.show() a = v1y v1y = v2y v2y = a # break if abs(r1x + v1x) + RADIUS > 1.0: v1x = -v1x if abs(r1y + v1y) + RADIUS > 1.0: v1y = -v1y if abs(r2x + v2x) + RADIUS > 1.0: v2x = -v2x if abs(r2y + v2y) + RADIUS > 1.0: v2y = -v2y r1x = r1x + v1x r1y = r1y + v1y r2x = r2x + v2x r2y = r2y + v2y position1.append((r1x, r1y)) stddraw.clear(stddraw.LIGHT_GRAY) stddraw.setPenColor(stddraw.GRAY) for x, y in position1[-50:]: print(x, y) stddraw.circle(x, y, RADIUS) stddraw.setPenColor(stddraw.BLACK) stddraw.filledCircle(r1x, r1y, RADIUS) stddraw.filledCircle(r2x, r2y, RADIUS) stddraw.show(DT)
import math from stdpackage import stdarray from stdpackage import stdaudio from stdpackage import stdio from stdpackage import stddraw SPS = 44100 CONCERT_A = 440.0 stddraw.setXscale(0,50) stddraw.setYscale(-20,20) height = 1 while not stdio.isEmpty(): pitch = stdio.readInt() duration = stdio.readFloat() hz = CONCERT_A*(2**(pitch/12.0)) n = int(SPS*duration) samples = stdarray.create1D(n+1,0.0) for i in range(n+1): samples[i] = math.sin(2.0*math.pi*i*hz/SPS) stdaudio.playSamples(samples) for i in range(0,n-100,100): stddraw.line(i/500,samples[i]+20-height,(i+100)/500,samples[i+100]+20-height) stddraw.show(duration*1000) height +=2 stdaudio.wait()
def draw_clock(H=0, M=0, S=0): stddraw.setXscale(-1.5, 1.5) stddraw.setYscale(-1.5, 1.5) R = 1.1 Hour_L = 0.4 Min_L = 0.55 Sec_L = 0.7 Hour_count = 0 Hour_X = stdarray.create1D(12, 0) Hour_Y = stdarray.create1D(12, 0) for i in range(1, 13): Hour_X[i - 1] = 1 * math.cos(-(i / 12 * 2 * math.pi - 1 / 2 * math.pi)) Hour_Y[i - 1] = 1 * math.sin(-(i / 12 * 2 * math.pi - 1 / 2 * math.pi)) stddraw.setFontSize(25) if H > 11: print('H is {0}'.format(H)) H = H % 12 Hour_count = 1 while True: while H < 13: angle_H = H * 2 * math.pi / 12 - 1 / 2 * math.pi print('H is {0}'.format(H)) if H == 12: H = 0 Hour_count += 1 H += 1 if Hour_count % 2 == 0: text = 'morining' else: text = 'afternoon' while M < 61: angle_M = M * 2 * math.pi / 60 - 1 / 2 * math.pi print('m is {0}'.format(M)) if M == 60: print('m is 60') M = 0 break else: M += 1 while S < 61: angle_s = S * 2 * math.pi / 60 - 1 / 2 * math.pi print('s is {0}'.format(S)) if S == 60: S = 0 print('s is 60') break else: S += 1 stddraw.clear() stddraw.circle(0, 0, R) for i in range(1, 13): stddraw.text(Hour_X[i - 1], Hour_Y[i - 1], str(i)) stddraw.text(0, 0.85, text) stddraw.setPenRadius(0.02) stddraw.line(0, 0, Hour_L * math.cos(-angle_H), Hour_L * math.sin(-angle_H)) stddraw.setPenRadius(0.01) stddraw.line(0, 0, Min_L * math.cos(-angle_M), Min_L * math.sin(-angle_M)) stddraw.setPenRadius(0.005) stddraw.line(0, 0, Sec_L * math.cos(-angle_s), Sec_L * math.sin(-angle_s)) stddraw.show(1000)
#命令行输入字符串,由左向右漂移,飘到触碰到最右边边界,则重新从左边开始。 from stdpackage import stddraw s = 'this is a sentence' x = 0.0 y = 0.9 stddraw.setFontSize(35) stddraw.setPenColor(stddraw.BOOK_RED) while True: stddraw.clear() if x < 1: stddraw.text(x, y, s) stddraw.show(100.0) x += 0.1 else: x = 0.0
# trials = int(sys.argv[2]) n = 20 trials = 10000 p = 0.1 stddraw.setCanvasSize(1000, 400) for n in range(20, 1000): stddraw.clear() freq = stdarray.create1D(n + 1, 0) for t in range(trials): heads = stdrandom.binomial(n, 0.7) freq[heads] += 1 norm = stdarray.create1D(n + 1, 0) for i in range(n + 1): norm[i] = 1.0 * freq[i] / trials phi = stdarray.create1D(n + 1, 0.0) stddev = math.sqrt(n) / 2.0 for i in range(n + 1): phi[i] = gaussian.pdf(i, n / 2.0, stddev) stddraw.setYscale(0, 1.1 * max(max(norm), max(phi))) stdstats.plotBars(norm) stdstats.plotLines(phi) stddraw.show(20)
def show(self): stddraw.show()
import sys from stdpackage import stdarray,stddraw,stdio,stdrandom n = int(sys.argv[1]) probabilities = stdarray.readFloat1D() cx = stdarray.readFloat2D() cy = stdarray.readFloat2D() x = 0.0 y = 0.0 stddraw.setPenRadius(0.0) for i in range(n): r = stdrandom.discrete(probabilities) x0 = cx[r][0]*x + cx[r][1]*y+cx[r][2] y0 = cy[r][0]*x + cy[r][1]*y+cy[r][2] x = x0 y = y0 stddraw.point(x,y) stddraw.show()
#从命令行获取整数m, 从标准输入获取最近m个浮点数,并用动画展示出来 from stdpackage import stdarray, stddraw, stdio, stdstats import sys m = int(sys.argv[1]) list = [] stddraw.setCanvasSize(500, 500) stddraw.setYscale(-1, 1) for i in range(m): stddraw.clear() list.append([stdio.readFloat(), str(i)]) print(list) stdstats.plotBars(list, text=True) stddraw.show(200) stddraw.show()