def main(argv): stddraw.createWindow() newton = Universe() dt = float(argv[1]) while True: newton.increaseTime(dt) newton.draw() stddraw.sleep(10) stddraw.show() stddraw.clear()
def main(argv): n = int(argv[1]) p = float(argv[2]) t = int(argv[3]) stddraw.createWindow() for i in range(t): open = percolation.random(n, p) stddraw.clear() stddraw.setPenColor(stddraw.BLACK) percolation.show(open, False) stddraw.setPenColor(stddraw.BLUE) full = percolation.flow(open) percolation.show(full, True) stddraw.sleep(1000) stddraw.show() stddraw.wait()
def draw(pole): n = len(pole) - 1 # Draw 3 poles. stddraw.clear() stddraw.setPenColor(POLE_COLOR) stddraw.setPenRadius(POLE_WIDTH) for i in range(3): stddraw.line(i, 0, i, n) # Draw n discs. discs = stdarray.create1D(3, 0) # discs[p] = # discs on pole p for i in range(n, 0, -1): stddraw.setPenColor(DISC_COLOR) stddraw.setPenRadius(0.035) # magic constant size = 0.5 * i / n p = pole[i] stddraw.line(p - size / 2, discs[p], p + size / 2, discs[p]) discs[p] += 1 stddraw.sleep(500) stddraw.show()
def draw(pole): n = len(pole) - 1 # Draw 3 poles. stddraw.clear() stddraw.setPenColor(POLE_COLOR) stddraw.setPenRadius(POLE_WIDTH) for i in range(3): stddraw.line(i, 0, i, n) # Draw n discs. discs = stdarray.create1D(3, 0) # discs[p] = # discs on pole p for i in range(n, 0, -1): stddraw.setPenColor(DISC_COLOR) stddraw.setPenRadius(0.035) # magic constant size = 0.5 * i / n p = pole[i] stddraw.line(p-size/2, discs[p], p + size/2, discs[p]) discs[p] += 1 stddraw.sleep(500) stddraw.show()
def main(argv): lamb = float(argv[1]) # Arrival rate mu = float(argv[2]) # Service rate hist = histogram.Histogram(60 + 1) q = linkedlistqueue.Queue() stddraw.createWindow(700, 500) nextArrival = stdrandom.exp(lamb) # Time of next arrival nextService = nextArrival + 1.0/mu # Time of next completed service # Simulate the M/D/1 queue while True: # Next event is an arrival. while nextArrival < nextService: # Simulate an arrival q.enqueue(nextArrival) nextArrival += stdrandom.exp(lamb) # Next event is a service completion. arrival = q.dequeue() wait = nextService - arrival # Update the histogram. stddraw.clear() hist.addDataPoint(min([60, int(wait+0.5)])) hist.draw() #stddraw.sleep(20) stddraw.sleep(20) stddraw.show() # Update the queue. if q.isEmpty(): nextService = nextArrival + 1.0/mu else: nextService = nextService + 1.0/mu
def main(argv): lamb = float(argv[1]) # Arrival rate mu = float(argv[2]) # Service rate hist = histogram.Histogram(60 + 1) q = linkedlistqueue.Queue() stddraw.createWindow(700, 500) nextArrival = stdrandom.exp(lamb) # Time of next arrival nextService = nextArrival + 1.0 / mu # Time of next completed service # Simulate the M/D/1 queue while True: # Next event is an arrival. while nextArrival < nextService: # Simulate an arrival q.enqueue(nextArrival) nextArrival += stdrandom.exp(lamb) # Next event is a service completion. arrival = q.dequeue() wait = nextService - arrival # Update the histogram. stddraw.clear() hist.addDataPoint(min([60, int(wait + 0.5)])) hist.draw() #stddraw.sleep(20) stddraw.sleep(20) stddraw.show() # Update the queue. if q.isEmpty(): nextService = nextArrival + 1.0 / mu else: nextService = nextService + 1.0 / mu
# Credits: idea suggested by Diego Nehab # Reference: http://www.math.dartmouth.edu/~dlittle/java/SpiroGraph # Reference: http://www.wordsmith.org/~anu/java/spirograph.html R = float(sys.argv[1]) r = float(sys.argv[2]) a = float(sys.argv[3]) stddraw.createWindow() stddraw.setXscale(-300, +300) stddraw.setYscale(-300, +300) stddraw.setPenRadius(0) t = 0.0 while True: x = (R + r) * math.cos(t) - (r + a) * math.cos(((R + r) / r) * t) y = (R + r) * math.sin(t) - (r + a) * math.sin(((R + r) / r) * t) degrees = -math.degrees((R + r) / r) * t stddraw.point(x, y) #stddraw.picture(x, y, "earth.gif", degrees) #stddraw.rotate(+Math.toDegrees((R+r)/r)*t) stddraw.sleep(10) stddraw.show() t += 0.01 # Example executions: # # python spirograph.py 180 40 15 # python spirograph.py 100 55 20
# Draw minute hand. stddraw.setPenRadius(.02) stddraw.setPenColor(stddraw.GRAY) angle2 = math.radians(6 * minutes) r2 = 0.3 stddraw.line(0.5, 0.5, \ 0.5 + r2 * math.sin(angle2), \ 0.5 + r2 * math.cos(angle2)) # Draw hour hand. stddraw.setPenRadius(.025) stddraw.setPenColor(stddraw.WHITE) angle3 = math.radians(30 * hours) r3 = 0.2 stddraw.line(0.5, 0.5, \ 0.5 + r3 * math.sin(angle3), \ 0.5 + r3 * math.cos(angle3)) # Draw digital time. # second = t % 60 # minute = (t / 60) % 60 # hour = (t / 3600) % 12 # time = '%2d:%02d:%02d' % (hour, minute, second) # stddraw.setPenColor(stddraw.RED) # stddraw.text(0.5, 0.0, time) # 1000 miliseconds = 1 second stddraw.sleep(1000) stddraw.show() t += 1
# banner.py #----------------------------------------------------------------------- import stddraw import sys # Accept string command-line argument s. Draw s, and move it across # the screen, left-to-right, wrapping around when it reaches the border. s = sys.argv[1] # Remove the 5% border. stddraw.createWindow() stddraw.setXscale(1.0 / 22.0, 21.0 / 22.0) stddraw.setYscale(1.0 / 22.0, 21.0 / 22.0) # Set the font. stddraw.setFontFamily('Arial') stddraw.setFontSize(60) stddraw.setPenColor(stddraw.BLACK) i = 0.0 while True: stddraw.clear() stddraw.text((i % 1.0), 0.5, s) stddraw.text((i % 1.0) - 1.0, 0.5, s) stddraw.text((i % 1.0) + 1.0, 0.5, s) stddraw.sleep(60) stddraw.show() i += 0.01
# banner.py #----------------------------------------------------------------------- import stddraw import sys # Accept string command-line argument s. Draw s, and move it across # the screen, left-to-right, wrapping around when it reaches the border. s = sys.argv[1] # Remove the 5% border. stddraw.createWindow() stddraw.setXscale(1.0/22.0, 21.0/22.0) stddraw.setYscale(1.0/22.0, 21.0/22.0) # Set the font. stddraw.setFontFamily('Arial') stddraw.setFontSize(60) stddraw.setPenColor(stddraw.BLACK) i = 0.0 while True: stddraw.clear() stddraw.text((i % 1.0), 0.5, s) stddraw.text((i % 1.0) - 1.0, 0.5, s) stddraw.text((i % 1.0) + 1.0, 0.5, s) stddraw.sleep(60) stddraw.show() i += 0.01