def main(): hurstExponent = float(sys.argv[1]) stddraw.setPenRadius(0.0) stddraw.clear(stddraw.LIGHT_GRAY) scaleFactor = 2 ** (2.0 * hurstExponent) curve(0, .5, 1.0, .5, .01, scaleFactor) stddraw.show()
def main(argv): M = int(argv[1]) N = int(argv[2]) S = int(argv[3]) # Create a RandomQueue object containing Queue objects. servers = randomqueue.RandomQueue() for i in range(M): servers.enqueue(linkedlistqueue.Queue()) for j in range(N): # Assign an item to a server. min = servers.sample() for k in range(1, S): # Pick a random server, update if new min. q = servers.sample() if len(q) < len(min): min = q # min is the shortest server queue. min.enqueue(j) lengths = [] for q in servers: lengths += [len(q)] stddraw.createWindow() stddraw.setYscale(0, 2.0*N/M) stdstats.plotBars(lengths) stddraw.show() stddraw.wait()
def main(argv): r1 = int(argv[1]) g1 = int(argv[2]) b1 = int(argv[3]) c1 = color.Color(r1, g1, b1) r2 = int(argv[4]) g2 = int(argv[5]) b2 = int(argv[6]) c2 = color.Color(r2, g2, b2) stddraw.createWindow() stddraw.setPenColor(c1) stddraw.filledSquare(.25, .5, .2) stddraw.setPenColor(c2) stddraw.filledSquare(.25, .5, .1) stddraw.setPenColor(c2) stddraw.filledSquare(.75, .5, .2) stddraw.setPenColor(c1) stddraw.filledSquare(.75, .5, .1) stddraw.show() stddraw.wait()
def main(argv): H = float(argv[1]) stddraw.createWindow() s = 2 ** (2*H) # or this: s = math.pow(2, 2*H) curve(0, .5, 1.0, .5, .01, s) stddraw.show() stddraw.wait()
def main(argv): n = int(argv[1]) stddraw.createWindow() step = 1.0 / (3.0 ** n) tur = turtle.Turtle(0.0, 0.0, 0.0) koch(n, step, tur) stddraw.show() stddraw.wait()
def main(): n = int(sys.argv[1]) turtle = Turtle(.5, .0, 180.0/n) stepSize = math.sin(math.radians(180.0/n)) stddraw.clear(stddraw.LIGHT_GRAY) for i in range(n): turtle.goForward(stepSize) turtle.turnLeft(360.0/n) stddraw.show()
def show(a, which): n = len(a) stddraw.setXscale(-1, n) stddraw.setYscale(-1, n) for i in range(n): for j in range(n): if a[i][j] == which: stddraw.filledSquare(j, n-i-1, .5) stddraw.show()
def main(): filename = sys.argv[1] dt = float(sys.argv[2]) universe = Universe(filename) while True: universe.increaseTime(dt) stddraw.clear() universe.draw() stddraw.show(10)
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(args): t = int(args[1]) step = float(args[2]) stddraw.createWindow() myTurtle = turtle.Turtle(0.5, 0.5, 0.0) for t1 in range(t): myTurtle.turnLeft(360.0 * random.random()) myTurtle.goForward(step) stddraw.show() stddraw.wait()
def midpoint(x0, y0, x1, y1, var, n): if n == 0: stddraw.line(x0, y0, x1, y1) stddraw.show() return xmid = 0.5 * (x0 + x1) + stdrandom.gaussian(0, math.sqrt(var)) ymid = 0.5 * (y0 + y1) + stdrandom.gaussian(0, math.sqrt(var)) midpoint(x0, y0, xmid, ymid, var / 2.7, n-1) midpoint(xmid, ymid, x1, y1, var / 2.7, n-1)
def main(argv): n = int(argv[1]) stddraw.createWindow() x = 0.5 # center of square y = 0.5 # center of square size = 0.5 # side length of square draw(n, x, y, size) stddraw.show() stddraw.wait()
def main(): n = int(sys.argv[1]) # number of biased coin flips per trial p = float(sys.argv[2]) # heads with probability p trialCount = int(sys.argv[3]) # number of trials histogram = Histogram(n + 1) for trial in range(trialCount): heads = stdrandom.binomial(n, p) histogram.addDataPoint(heads) stddraw.setCanvasSize(500, 200) stddraw.clear(stddraw.LIGHT_GRAY) histogram.draw() stddraw.show()
def main(): n = int(sys.argv[1]) p = float(sys.argv[2]) trials = int(sys.argv[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) full = percolation.flow(isOpen) percolationio.draw(full, True) stddraw.show(1000.0) stddraw.show()
def main(argv): import bernoulli coinCount = int(argv[1]) trialCount = int(argv[2]) histogram = Histogram(coinCount + 1) for trial in range(trialCount): histogram.addDataPoint(bernoulli.binomial(coinCount)) stddraw.createWindow(500, 100) histogram.draw() stddraw.show() stddraw.wait()
def tree(n, x, y, a, branchRadius): cx = x + math.cos(a) * branchRadius cy = y + math.sin(a) * branchRadius stddraw.setPenRadius(.001 * (n ** 1.2)) stddraw.line(x, y, cx, cy) stddraw.show() if (n == 0): return tree(n-1, cx, cy, a + BEND_ANGLE - BRANCH_ANGLE, \ branchRadius * BRANCH_RATIO) tree(n-1, cx, cy, a + BEND_ANGLE + BRANCH_ANGLE, \ branchRadius * BRANCH_RATIO) tree(n-1, cx, cy, a + BEND_ANGLE, \ branchRadius * (1 - BRANCH_RATIO))
def main(args): n = int(args[1]) t = int(args[2]) step = float(args[3]) stddraw.createWindow() turtles = [] for i in range(n): turtles += \ [turtle.Turtle(random.random(), random.random(), 0.0)] for t1 in range(t): for i in range(n): turtles[i].turnLeft(360.0 * random.random()) turtles[i].goForward(step) stddraw.show() stddraw.wait()
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 main(): n = int(sys.argv[1]) cx = [0.000, 1.000, 0.500] cy = [0.000, 0.000, 0.866] x = 0.0 y = 0.0 stddraw.setPenRadius(0.0) for i in range(n): r = stdrandom.uniformInt(0, 3) x = (x + cx[r]) / 2.0 y = (y + cy[r]) / 2.0 stddraw.point(x, y) stddraw.show()
def curve(n, x0, y0, x1, y1): # print 'curve', N, x0, y0, x1, y1 gap = 0.01 err = 0.0025 T = 10000 xm = (x0 + x1) / 2.0 ym = (y0 + y1) / 2.0 fxm = estimate.eval(n, xm, T) if (x1 - x0 < gap) or (abs(ym - fxm) < err): stddraw.line(x0, y0, x1, y1) stddraw.show() return curve(n, x0, y0, xm, fxm) stddraw.filledCircle(xm, fxm, 0.005) stddraw.show() curve(n, xm, fxm, x1, y1)
def main(): stddraw.createWindow() stddraw.setXscale(0, 15) stddraw.setYscale(0, 15) for i in range(16): for j in range (16): #val = i + 16*j val = 8*i + 8*j #c1 = color.Color(0, 0, 255-val) c1 = color.Color(255-val, 255-val, 255) c2 = color.Color(val, val, val) stddraw.setPenColor(c1) stddraw.filledSquare(i, j, 0.5) stddraw.setPenColor(c2) stddraw.filledSquare(i, j, 0.25) stddraw.show() stddraw.wait()
def main(argv): n = float(argv[1]) decay = float(argv[2]) stddraw.createWindow() stddraw.setPenRadius(0) angle = 360.0 / n step = math.sin(math.radians(angle/2.0)) t = turtle.Turtle(0.5, 0, angle/2.0) i = 0 while i < 10.0 * 360.0 / angle: step /= decay t.goForward(step) t.turnLeft(angle) i += 1 stddraw.show() stddraw.wait()
def draw(n, sz, x, y): if n == 0: return x0 = x - sz / 2 x1 = x + sz / 2 y0 = y - sz / 2 y1 = y + sz / 2 stddraw.line(x0, y, x1, y) stddraw.line(x0, y0, x0, y1) stddraw.line(x1, y0, x1, y1) stddraw.show() draw(n - 1, sz / 2, x0, y0) draw(n - 1, sz / 2, x0, y1) draw(n - 1, sz / 2, x1, y0) draw(n - 1, sz / 2, x1, y1)
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): n = int(argv[1]) t = int(argv[2]) stddraw.createWindow() stddraw.setYscale(0, 0.2) freq = [0] * (n+1) for t in range(t): freq[binomial(n)] += 1 norm = [0.0] * (n+1) for i in range(n+1): norm[i] = float(freq[i]) / float(t) stdstats.plotBars(norm) stddev = math.sqrt(n) / 2.0 phi = [0.0] * (n+1) for i in range(n+1): phi[i] = gaussian.phi(i, n/2.0, stddev) stdstats.plotLines(phi) stddraw.show() stddraw.wait()
import stddraw import stdaudio from picture import Picture from juweip3 import GuitarString a_string = GuitarString(440.00) c_string = GuitarString(523.25) # show a nice background picture p = Picture("cpsc231-guitar.png") stddraw.picture(p) stddraw.show(0.0) escape = False while not escape: # check for and process events stddraw._checkForEvents() while stddraw.hasNextKeyTyped(): key = stddraw.nextKeyTyped() if key == chr(27): escape = True elif key == "a": a_string.pluck() elif key == "c": c_string.pluck() # simulate and play strings y = a_string.tick() y += c_string.tick() stdaudio.playSample(y)
def draw_MST(self, MST, mass): stddraw.clear() for i in MST: stddraw.line(mass[i[1]][0], mass[i[1]][1], mass[i[2]][0], mass[i[2]][1]) stddraw.show(2000)
def show(milliseconds=-1.0): if milliseconds < 0: stddraw.show() else: stddraw.show(milliseconds)
import richardnorman_p2_drawing import stddraw import pygame import richardnorman_p2_taffy game_over = False first_taffy_selected = False richardnorman_p2_drawing.draw_score() first_taffy = richardnorman_p2_taffy.Taffy second_taffy = richardnorman_p2_taffy.Taffy while not game_over: stddraw.show(350) if not first_taffy_selected: if stddraw.mousePressed(): first_taffy = richardnorman_p2_drawing.clicked_handler( stddraw.mouseX(), stddraw.mouseY(), True) first_taffy_selected = True else: if stddraw.mousePressed(): second_taffy = richardnorman_p2_drawing.clicked_handler( stddraw.mouseX(), stddraw.mouseY(), False) #check if valid swap valid_swap = richardnorman_p2_drawing.check_if_valid_swap( first_taffy, second_taffy) if not valid_swap: print( "The taffy swap is not valid, try selecting another taffy") else:
def draw(a=[]): stddraw.setXscale(-1, 21) stddraw.setYscale(0, 700) for k in range(len(a)): stddraw.filledRectangle(k, 0, 0.5, a[k]) stddraw.show()
l = [[False] * n for i in range(n)] for i in range(n): for j in range(n): if PofInt.relativelyPrime(i,j): l[i][j] = True percolationio.draw(l,True) def Pofhadamard(n): l = hadamard.had(n) percolationio.draw(l,True) def odd(n): if n % 2 == 0: return False return True def Pofx(n): l = [[False] * n for i in range(n)] for i in range(n): for j in range(n): if odd(comb(i,j)): l[i][j] = True percolationio.draw(l,True) n = eval(sys.argv[1]) #n = 4 #PofRelativelyPrime(n) #Pofhadamard(n) Pofx(n) stddraw.show()
def main(): stddraw.createWindow() # Create keyboardDict, a dictionary relating each keyboard key # to a guitar string. keyboardDict = {} i = 0 for key in _KEYBOARD: factor = 2**((i - 24) / 12.0) guitarString = guitarstring.GuitarString(_CONCERT_A * factor) keyboardDict[key] = guitarString i += 1 # pluckedGuitarStrings is the set of all guitar strings that have # been plucked. pluckedGuitarStrings = set() # loopCount is used to control the frequency of calls of # stddraw.show(). loopCount = 1023 # The main input loop. while True: # Call stddraw.show() occasionally to capture keyboard events. if loopCount == 1023: stddraw.show() loopCount = 0 loopCount += 1 if stddraw.hasNextKeyTyped(): # Fetch the key that the user just typed. key = stddraw.nextKeyTyped() # Figure out which guitar string to pluck, and pluck it. try: guitarString = keyboardDict[key] guitarString.pluck() pluckedGuitarStrings.add(guitarString) except KeyError: pass # Add up the samples from each plucked guitar string. Also # advance the simulation of each plucked guitar string by # one step. sample = 0.0 faintGuitarStrings = set() for guitarString in pluckedGuitarStrings: sample += guitarString.sample() guitarString.tic() if guitarString.isFaint(): faintGuitarStrings.add(guitarString) # Remove faint guitar strings from the set of plucked guitar # strings. for guitarString in faintGuitarStrings: pluckedGuitarStrings.remove(guitarString) # Play the total. stdaudio.playSample(sample)
def draw_convex_point(self, convex): for i in convex: stddraw.point(i[0], i[1]) stddraw.show(2000)
stddraw.setCanvasSize(420, 660) #700*0.6, 1100*0.6 stddraw.setFontSize(50) stddraw.setPenColor(stddraw.DARK_BLUE) stddraw.text(3.5, 9.5, "How do you") stddraw.text(3.5, 8.5, "want to play?") stddraw.filledRectangle(1, 5, 5, 2) stddraw.filledRectangle(1, 2, 5, 2) stddraw.setPenColor(stddraw.WHITE) stddraw.text(3.5, 6, "Timed") stddraw.text(3.5, 3, "Perfection") while True: stddraw.show(0.0) #pause for .1 second if stddraw.mousePressed(): if 5 < stddraw.mouseY() < 7 and 1 < stddraw.mouseX() < 6: #Timed time = 0 break if 2 < stddraw.mouseY() < 4 and 1 < stddraw.mouseX() < 6: #Perfection time = 9999 break score = 0 board = generateBoard(time) gameOver = False while not gameOver:
score = 0 turn = 0 board = stdarray.create2D(COLUMNS, ROWS, " ") sd.setCanvasSize(560, 800) sd.setXscale(0, 7) sd.setYscale(-1, 9) p2Mod.fill(board, COLUMNS, ROWS) while p2Mod.winCheck(board, COLUMNS, ROWS): p2Mod.fill(board, COLUMNS, ROWS) while not p2Mod.isDraw(board, COLUMNS, ROWS) and turn < FINALTURN: #Note a turn is used up if a turn is Valid but does not result in any pieces getting removed and thus the move is #reversed but a turn is still counted. This is intentional. If this is not wanted the turn+1 can be moved to the #combo loop turn += 1 p2Mod.drawBoard(board, COLUMNS, ROWS, score, turn) sd.show(1) validMove = False while not validMove: piece1 = p2Mod.getMove(board, COLUMNS, ROWS) sd.square(piece1[0]+.5, piece1[1]+.5, .5) sd.show(1) piece2 = p2Mod.getMove(board, COLUMNS, ROWS) sd.square(piece2[0] + .5, piece2[1] + .5, .5) sd.show(500) dif1 = (piece1[0] - piece2[0]) * (piece1[0] - piece2[0]) dif2 = (piece1[1] - piece2[1]) * (piece1[1] - piece2[1]) p2Mod.drawBoard(board, COLUMNS, ROWS, score, turn) if (dif1 == 0 and dif2 == 1) or (dif1 == 1 and dif2 == 0): validMove = True p2Mod.move(board, piece1, piece2)
def main(): n = int(sys.argv[1]) stddraw.setXscale(-0.1, 1.1) stddraw.setYscale(-0.1, 1.1) sier(n, 0, 0, 1, 0, 0.5, math.sin(math.pi / 3)) stddraw.show()
# Use insertion sort on each subset of the # the list with distance of h between each # element. j = i while j >= h and less(a[j], a[j - h]): exch(a, j, j - h) j -= h # Reduce h to one third of itself. h = h // 3 # generate random list or perhaps a list of evenly spaced integers (see numpy's arrange function) length = 20 indices = np.arange(0, length, 1) data = np.arange(0, length / 2, 0.5) random.shuffle(data) # draw the data on the canvas sf = 10 stddraw.setPenRadius(0) stddraw.setPenColor(stddraw.BLUE) stddraw.setXscale(-5, 25) stddraw.setYscale(-5, 15) for n in range(length): stddraw.filledRectangle(n, 0, 0.5, data[n]) stddraw.show(1000) # choose from insertion, selection, or shell sort selection(data) stddraw.show()
for k in range(1, NX - 1): psi[i, j, k] = psiNew[i, j, k] # (3) Hitung Energi setiap 10 iterasi if (n % 10) == 0: integral1 = 0.0 integral2 = 0.0 for i in range(1, NX - 1): for j in range(1, NX - 1): for k in range(1, NX - 1): integral1 += psi[i, j, k] * psi[i, j, k] d2psidx2 = -0.5 * (psi[i + 1, j, k] - 2 * psi[i, j, k] + psi[i - 1, j, k]) / dx2 d2psidx2 += -0.5 * (psi[i, j + 1, k] - 2 * psi[i, j, k] + psi[i, j - 1, k]) / dx2 d2psidx2 += -0.5 * (psi[i, j, k + 1] - 2 * psi[i, j, k] + psi[i, j, k - 1]) / dx2 integral2 += psi[i, j, k] * (d2psidx2 + v[i, j, k] * psi[i, j, k]) energi = integral2 / integral1 - 1.0 / dx print(energi) stddraw.clear() stddraw.setPenColor(stddraw.BLUE) for i in range(NX): x1 = (i - ic) * dx y1 = psi[i, jc, kc] / psi[ic, jc, kc] x2 = (i + 1 - ic) * dx y2 = psi[i + 1, jc, kc] / psi[ic, jc, kc] stddraw.line(x1, y1, x2, y2) stddraw.show(5)
def draw_clusters(self, clusters): stddraw.clear() for k in range(0, self.K): for i in clusters[k]: stddraw.point(i[0], i[1]) stddraw.show(2000)
stddraw.setYscale(0, n) else: stddraw.setXscale(0, m) stddraw.setYscale(0, m) No_ = m * n while True: no_run = 0 stddraw.clear() if No_ > 0: for i in range(n): for j in range(m): if no_run < No_: stddraw.setPenColor(stddraw.RED) stddraw.filledCircle(i + .5, j + .5, .5) no_run += 1 stddraw.show(20) if section: stdio.write("Luot choi cua ban[1-3]: ") k = stdio.readInt() No_ -= k section = False else: if No_ % 4 == 1: k = random.randrange(1, 4) elif No_ % 4 == 0: k = 3 else: k = No_ % 4 - 1 stdio.writeln("Toi chon " + str(k))
def draw_convex(self, convex): stddraw.line(convex[-1][0], convex[-1][1], convex[0][0], convex[0][1]) for i in range(len(convex) - 1): stddraw.line(convex[i][0], convex[i][1], convex[i + 1][0], convex[i + 1][1]) stddraw.show(2000)
def display(playing_field, x, y, lent, totalscore, finalscore, clickcoords, clicklocal, attempts, level, background, win): stddraw.clear() image(background) stddraw.setXscale(-lent, (2 * lent * x) - lent) stddraw.setYscale(-(2 * lent * y) - 4 * lent, lent) stddraw.setPenColor(stddraw.PINK) stddraw.filledRectangle(-lent, -(2 * lent * y) - 4 * lent, (2 * lent * x), 5 * lent) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.filledRectangle(-lent, -(2 * lent * y) - 3 * lent, (2 * lent * x), lent) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.filledRectangle(-lent, -(2 * lent * y) - 1.5 * lent, (2 * lent * x), lent) stddraw.setPenColor(stddraw.GRAY) stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent, (2 * lent * x), lent) if totalscore < finalscore: stddraw.setPenColor(stddraw.GREEN) stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent, ((2 * lent * x) - lent) / 1.7 * (totalscore / finalscore), lent) if totalscore >= finalscore: stddraw.setPenColor(stddraw.GREEN) stddraw.filledRectangle(-lent, -(2 * lent * y) - 2.2 * lent, ((2 * lent * x) - lent), lent) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.filledRectangle(x * lent + lent, -(2 * lent * y) - 3.5 * lent, (lent * x), 3.5 * lent) stddraw.setPenRadius(0.010) stddraw.setPenColor(stddraw.BOOK_LIGHT_BLUE) stddraw.rectangle(-lent, -(2 * lent * y) - 4 * lent, (2 * lent * x), 5 * lent) stddraw.setFontSize(25) stddraw.setPenColor(stddraw.DARK_GREEN) stddraw.text((((2 * lent * x) - lent) / 1.3), -((2 * lent * y) + lent), 'Points: ' + str(totalscore) + ' / ' + str(finalscore)) stddraw.setPenColor(stddraw.RED) stddraw.setFontSize(20) stddraw.text((((2 * lent * x) - lent) / 1.3), -((2 * lent * y) + 2.7 * lent), 'Attempts Left: ' + str(attempts)) stddraw.setPenColor(stddraw.BLUE) stddraw.setFontSize(25) stddraw.text((((2 * lent * x) - lent) / 20), -((2 * lent * y) - 0.08 * lent), 'Level: ' + str(level)) for i in range(len(clickcoords) - 1): found = False reps = -1 while found == False and not reps >= (x * y - 1): reps += 1 if clicklocal[reps][0] < clickcoords[i] < clicklocal[reps][ 1] and clicklocal[reps][2] < clickcoords[ i + 1] < clicklocal[reps][3]: spotx = playing_field[reps][0] spoty = playing_field[reps][1] stddraw.setPenColor(stddraw.DARK_RED) stddraw.setPenRadius(0.05) stddraw.filledSquare(spotx, spoty, lent) found = True for i in range(x * y): spotx = playing_field[i][0] spoty = playing_field[i][1] if playing_field[i][2] == 1: stddraw.setPenColor(stddraw.MAGENTA) stddraw.setPenRadius(0.05) stddraw.filledCircle(spotx, spoty, lent / 1.2) if playing_field[i][2] == 2: stddraw.setPenColor(stddraw.YELLOW) stddraw.setPenRadius(0.05) stddraw.filledSquare(spotx, spoty, lent / 1.2) if playing_field[i][2] == 3: stddraw.setPenColor(stddraw.BLUE) stddraw.setPenRadius(0.05) stddraw.filledPolygon([(spotx - lent) + 0.2, (spotx - lent) + 0.3, (spotx - lent) + 0.7, (spotx - lent) + 0.9, (spotx - lent) + 0.6], [(spoty - lent) + 0.6, (spoty - lent) + 0.9, (spoty - lent) + 0.9, (spoty - lent) + 0.6, (spoty - lent) + 0.1]) if playing_field[i][2] == 4: stddraw.setPenColor(stddraw.GREEN) stddraw.setPenRadius(0.05) stddraw.filledPolygon([ (spotx - lent) + 0.1, (spotx - lent) + 0.4, (spotx - lent) + 0.9, (spotx - lent) + 0.4, ], [(spoty - lent) + 0.4, (spoty - lent) + 0.1, (spoty - lent) + 0.4, (spoty - lent) + 0.9]) if playing_field[i][2] == 5: stddraw.setPenColor(stddraw.BLACK) stddraw.setPenRadius(0.05) stddraw.filledPolygon([(spotx - lent) + 0.5, (spotx - lent) + 0.1, (spotx - lent) + 0.9], [(spoty - lent) + 0.8, (spoty - lent) + 0.1, (spoty - lent) + 0.1]) if playing_field[i][2] == 6: stddraw.setPenColor(stddraw.CYAN) stddraw.setPenRadius(0.05) stddraw.filledPolygon([(spotx - lent) + 0.5, (spotx - lent) + 0.1, (spotx - lent) + 0.9, (spotx - lent) + 0.3, (spotx - lent) + 0.6], [(spoty - lent) + 0.9, (spoty - lent) + 0.7, (spoty - lent) + 0.7, (spoty - lent) + 0.1, (spoty - lent) + 0.1]) if attempts == 0 and totalscore < finalscore: while stddraw.mousePressed() == False: stddraw.setPenColor(stddraw.BLACK) stddraw.filledRectangle(-lent, -(lent * y) - 2 * lent, (2 * lent * x), 3.5 * lent) stddraw.setFontSize(40) stddraw.setPenColor(stddraw.RED) stddraw.text(lent * x - lent, -(lent * y), 'YOU LOSE, CLICK TO EXIT') stddraw.show(0) if stddraw.mousePressed() == True: win = True return win if totalscore >= finalscore: while stddraw.mousePressed() == False: stddraw.setPenColor(stddraw.BOOK_BLUE) stddraw.filledRectangle(-lent, -(lent * y) - 2 * lent, (2 * lent * x), 3.5 * lent) stddraw.setFontSize(40) stddraw.setPenColor(stddraw.GREEN) stddraw.text(lent * x - lent, -(lent * y), 'YOU WIN, CLICK TO PROCEED') stddraw.show(0) if stddraw.mousePressed() == True: win = False return win stddraw.show(250) return win
for ii in range(len(params)): if params[ii] < 0.0: f.write(str(params[ii]) + ' ') else: f.write(' ' + str(params[ii]) + ' ') f.write('\n') f.close() universe = u.Universe("temp_data.txt") for j in range(500): universe.increaseTime(dt) stddraw.clear() universe.draw() stddraw.show(10) x_arr = universe._xtracks y_arr = universe._ytracks #coords = zip(x_array_p[n],y_array_p) img_rows, img_cols = 110, 110 #imgs = np.zeros((n, img_rows, img_cols)) for ii in range(n): img = np.zeros((img_rows, img_cols)) xvals = scale_1000(x_arr[ii]) yvals = scale_1000(y_arr[ii]) for xval, yval in zip(xvals, yvals):
stddraw.clear() stddraw.setFontSize(30) stddraw.text(1, 3.5, "Is playerOne's turn") draw_the_picture(game_array) if mouse_clicked: game_array = player_two_draw(game_array) game_playertwo_click = [] game_playertwo_click = expend_the_player_array(game_playertwo_click, 2) game_win = determine_the_winner(game_playertwo_click, 2, game_win) there_is_no_winner(game_playertwo_click, game_playerOne_click) stddraw.show(1) """playerOne turns""" mouse_clicked = False while mouse_clicked == False: mouse_clicked = stddraw.mousePressed() # return boolean stddraw.clear() stddraw.setFontSize(30) stddraw.text(1, 3.5, "Is playerTwo's turn") draw_the_picture(game_array) if mouse_clicked: game_array = player_one_draw(game_array)
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 main(): # n = int(sys.argv[1]) n = 4 stddraw.setPenRadius(0.0) draw(n, .5, .5, .5) stddraw.show()
import stddraw as d N = 20 d.setXscale(0, N) d.setYscale(0, N) d.circle(10, 10, 10) for i in range(1, 10): d.circle(10, 10, 10 - i) d.show(1000)
import stddraw as d N = 50 d.setXscale(0, N) d.setYscale(0, N) for x in range(0, N): d.line(0, N - x, x, 0) d.show(0)
# Draw a curve formed by rolling a smaller circle of radius r inside # a larger circle or radius R. If the pen offset of the pen point in # the moving circle is a, then the equation of the resulting curve # at time t is # # x = (R+r)*cos(t) - (r+a)*cos(((R+r)*t)/r) # y = (R+r)*sin(t) - (r+a)*sin(((R+r)*t)/r) # 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.setXscale(-300, +300) stddraw.setYscale(-300, +300) stddraw.setPenRadius(0.0) t = 0.0 while True: x = (R + r) * math.cos(t) - (r + a) * math.cos(((R + r) * t) / r) y = (R + r) * math.sin(t) - (r + a) * math.sin(((R + r) * t) / r) 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.show(10.0) t += 0.01
def draw(self): #stddraw.rectangle(x,y,w,h),x,y为左下角坐标 x = self._x-self._width/2 y = self._y-self._height/2 stddraw.rectangle(x,y,self._width,self._height) stddraw.show()
tc = tc*ta psi[i] = tc n=0 while True: n = n + 1 # (2) Update psi for i in range(1,NX-1): psip1[i] = psim1[i] + cc*1j*(psi[i+1] - 2*psi[i] + psi[i-1]) - 2*1j*v[i]*psi[i]*dt # (4) Save for i in range(1,NX-1): psim1[i] = psi[i] psi[i] = psip1[i] if n%20 == 0: # (5) Draw psiR stddraw.clear() stddraw.setPenColor(stddraw.RED) for i in range(NX): x1 = i*dx y1 = abs(psi[i]) x2 = (i+1)*dx y2 = abs(psi[i+1]) stddraw.line(x1, y1, x2, y2) # Display and wait for 5 ms stddraw.show(5);
# (3) Generate an EM source # (a) a sinusoidal wave # bz[ic - 100] = bz[ic - 100] + math.sin(omega*n*dt) # (b) an EM pulse if n<60: bz[ic - 100] = bz[ic - 100] + 2.0*math.exp(-0.005*(n-30.0)*(n-30.0)) # (4) Update Ey for i in range(1,NX): ey[i] = ce1[med[i]]*ey[i] - ce2[med[i]]*(bz[i] - bz[i-1]) # (5) Draw Ey fields stddraw.clear() stddraw.setPenColor(stddraw.YELLOW) stddraw.filledRectangle(5,-2.5,2.5,5) stddraw.setPenColor(stddraw.BLUE) stddraw.text(2, 2, "Medium 0") stddraw.text(7, 2, "Medium 1") stddraw.setPenColor(stddraw.RED) for i in range(NX): x1 = i*dx y1 = ey[i] x2 = (i+1)*dx y2 = ey[i+1] stddraw.line(x1, y1, x2, y2) # Display and wait for 20 ms stddraw.show(10);
# Hitung Ex for i in range(1, 200): for j in range(1, 200): ex[i][j] = ce1[media[i][j]] * ex[i][j] + ce2[media[i][j]] * ( hz[i][j] - hz[i][j - 1]) # Hitung Ey for i in range(1, 200): for j in range(1, 200): ey[i][j] = ce1[media[i][j]] * ey[i][j] + ce2[media[i][j]] * ( hz[i - 1][j] - hz[i][j]) hz[100][100] += math.sin(omega * n * dt) if n % 20 == 0: stddraw.clear() for i in range(201): for j in range(201): v = (MAX_GRAY_SCALE / 2.0) + (500 * hz[i][j]) if v < 0: grayScale = 0 elif v > MAX_GRAY_SCALE: grayScale = MAX_GRAY_SCALE else: grayScale = int(v) color = Color(grayScale, grayScale, grayScale) pic.set(i, j, color) stddraw.picture(pic) stddraw.show(20)
def draw_data(self, mass): stddraw.clear() for i in mass: #print(i) stddraw.point(i[0], i[1]) stddraw.show(2000)
myTurtle.turnLeft(-120.0) koch(n-1, stepSize, myTurtle) myTurtle.turnLeft(60.0) koch(n-1, stepSize, myTurtle) # Accept integer n as a command-line argument. Plot a Koch curve of # order n to standard draw. n = int(sys.argv[1]) stddraw.setCanvasSize(512, 256) stddraw.setYscale(-.1, 0.4) stddraw.setPenRadius(0.0) stddraw.clear(stddraw.LIGHT_GRAY) stepSize = 1.0 / (3.0 ** n) myTurtle = Turtle(0.0, 0.0, 0.0) koch(n, stepSize, myTurtle) stddraw.show() #----------------------------------------------------------------------- # python koch.py 0 # python koch.py 1 # python koch.py 2 # python koch.py 3 # python koch.py 4
def main(): n = int(sys.argv[1]) p = float(sys.argv[2]) test = random(n, p) draw(test, False) stddraw.show()
def main(): stddraw.createWindow(1024, 256) stddraw.setPenRadius(0) stddraw.setXscale(0, _SAMPLES_PER_REDRAW) stddraw.setYscale(-0.75, +0.75) stddraw.show() # Create keyboardDict, a dictionary relating each keyboard key # to a guitar string. keyboardDict = {} i = 0 for key in _KEYBOARD: factor = 2 ** ((i - 24) / 12.0) guitarString = guitarstring.GuitarString(_CONCERT_A * factor) keyboardDict[key] = guitarString i += 1 # pluckedGuitarStrings is the set of all guitar strings that have # been plucked. pluckedGuitarStrings = set() t = 0 # The main input loop. while True: if stddraw.hasNextKeyTyped(): # Fetch the key that the user just typed. key = stddraw.nextKeyTyped() # Figure out which guitar string to pluck, and pluck it. try: guitarString = keyboardDict[key] guitarString.pluck() pluckedGuitarStrings.add(guitarString) except KeyError: pass # Add up the samples from each plucked guitar string. Also # advance the simulation of each plucked guitar string by # one step. sample = 0.0 faintGuitarStrings = set() for guitarString in pluckedGuitarStrings: sample += guitarString.sample() guitarString.tic() if guitarString.isFaint(): faintGuitarStrings.add(guitarString) # Remove faint guitar strings from the set of plucked guitar # strings. for guitarString in faintGuitarStrings: pluckedGuitarStrings.remove(guitarString) # Play the total. stdaudio.playSample(sample) # Plot stddraw.point(t % _SAMPLES_PER_REDRAW, sample) if t == (_SAMPLES_PER_REDRAW - 1): stddraw.show() stddraw.clear() t = 0 t += 1
def main(argv): fileName = argv[1] w = int(argv[2]) h = int(argv[3]) source = picture.Picture() source.load(fileName) target = picture.Picture(w, h) for ti in range(w): for tj in range(h): si = ti * source.width() // w sj = tj * source.height() // h target.set(ti, tj, source.get(si, sj)) maxHeight = max(source.height(), target.height()) stddraw.createWindow(source.width() + target.width(), maxHeight) stddraw.setXscale(0, source.width() + target.width()) stddraw.setYscale(0, maxHeight) stddraw.picture(source, source.width() / 2, maxHeight / 2) stddraw.picture(target, source.width() + target.width() / 2, maxHeight / 2) stddraw.show() stddraw.wait()
def main(): n = int(sys.argv[1]) dist = 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(dist) 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()