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 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 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 draw_count(countlist): # stddraw.setCanvasSize(100,100) stddraw.setXscale(-1,10) stddraw.setYscale(-0.1,0.5) stddraw.setPenColor(stddraw.GRAY) stddraw.line(0,0,100,0) stddraw.line(0,0,0,5000) for List in countlist: for i in range(1,len(List)): stddraw.line(i-1,List[i-1],i,List[i])
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 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)
#命令行输入字符串,由左向右漂移,飘到触碰到最右边边界,则重新从左边开始。 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
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 sys from stdpackage import stddraw sys.path.append('/Users/Jane/Desktop/PythonLearning/miaozaiye/percolation') from miaozaiye.percolation import percolationio, percolationv n = 20 p = 0.9 trials = 3 for i in range(trials): isOpen = percolationio.random(n, p) stddraw.clear() stddraw.setPenColor(stddraw.BLACK) percolationio.draw(isOpen, False) stddraw.setPenColor(stddraw.BLUE) isFull = percolationv.flow(isOpen) percolationio.draw(isFull, True) stddraw.show(1000.0) stddraw.show()
def draw(self): print('ball.draw') stddraw.setPenColor(stddraw.color.BLACK) print('this is location,', self.location.getatt()) stddraw.filledCircle(self.location.getatt()[0][0], self.location.getatt()[0][1], self.radius)