def draw(pole): POLE_WIDTH = 0.01 POLE_COLOR = stddraw.RED DISC_COLOR = stddraw.BLUE 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.show(500.0)
def goForward(self, step): oldx = self._x oldy = self._y self._x += step * math.cos(math.radians(self._angle)) self._y += step * math.sin(math.radians(self._angle)) stddraw.line(oldx, oldy, self._x, self._y) stddraw.show(100)
def draw_graph(points, color): stddraw.setPenColor(color) prev = None for point in points: if prev is not None: stddraw.line(*prev, *point) prev = point
def go_forward(self, step): dx = step * math.cos(self._angle) dy = step * math.sin(self._angle) # leave a line first stddraw.line(self._x, self._y, self._x + dx, self._y + dy) # actually move my turtle self._x += dx self._y += dy
def drawRuler(n): s = ruler(n) l = len(s) stddraw.setXscale(-1, l + 1) stddraw.setYscale(0, n * 1.1) for i in range(l): stddraw.line(i, 0, i, eval(s[i])) stddraw.show()
def plotLines(a): """ Plot the values of array 'a' as line end-points. """ N = len(a) stddraw.setXscale(0, N-1) stddraw.setPenRadius() for i in range(1, N): stddraw.line(i-1, a[i-1], i, a[i])
def plotLines(a): """ Plot the elements of array a as line end-points. """ n = len(a) stddraw.setXscale(-1, n) stddraw.setPenRadius(0.0) for i in range(1, n): stddraw.line(i-1, a[i-1], i, a[i])
def plotLines(a): """ Plot the elements of array a as line end-points. """ n = len(a) stddraw.setXscale(-1, n) stddraw.setPenRadius(0.0) for i in range(1, n): stddraw.line(i - 1, a[i - 1], i, a[i])
def plotLines(a): """ Plot the values of array 'a' as line end-points. """ N = len(a) stddraw.setXscale(0, N - 1) stddraw.setPenRadius() for i in range(1, N): stddraw.line(i - 1, a[i - 1], i, a[i])
def curve(x0, y0, x1, y1, variance, scaleFactor): if (x1 - x0) < .01: stddraw.line(x0, y0, x1, y1) return xm = (x0 + x1) / 2.0 ym = (y0 + y1) / 2.0 delta = stdrandom.gaussian(0, math.sqrt(variance)) curve(x0, y0, xm, ym+delta, variance/scaleFactor, scaleFactor) curve(xm, ym+delta, x1, y1, variance/scaleFactor, scaleFactor)
def curve(x0, y0, x1, y1, var, s): if (x1 - x0) < .01: stddraw.line(x0, y0, x1, y1) return xm = (x0 + x1) / 2.0 ym = (y0 + y1) / 2.0 delta = stdrandom.gaussian(0, math.sqrt(var)) curve(x0, y0, xm, ym+delta, var/s, s) curve(xm, ym+delta, x1, y1, var/s, s)
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 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 fill_brownian(a, i0, i1, variance, scaleFactor): if i0 - i1 == -1: stddraw.line(i0, a[i0], i1, a[i1]) return ym = int((i0 + i1) * 1 / 2) delta = stdrandom.gaussian(0, math.sqrt(variance)) a[ym] = delta fill_brownian(a, i0, ym, variance / scaleFactor, scaleFactor) fill_brownian(a, ym, i1, variance / scaleFactor, scaleFactor)
def midpoint(x0, y0, x1, y1, var, n): if n == 0: stddraw.line(x0, y0, x1, y1) stddraw.show(0.0) 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 draw_marker(x, y, turn_number): if turn_number % 2 != 0: #player one clicked #draw an X stddraw.line(x + 0.1, y + (1 / 3) - 0.1, x + (1 / 3) - 0.1, y + 0.1) stddraw.line(x + 0.1, y + 0.1, x + (1 / 3) - 0.1, y + (1 / 3) - 0.1) else: #player two clicked #draw an O stddraw.circle(x + (1 / 3) / 2, y + (1 / 3) - (1 / 3) / 2, 0.1)
def player_two_draw(game_array_in_def): x = stddraw.mouseX() y = stddraw.mouseY() for j in range(3): for k in range(3): if x >= j and x <= j + 1 and y >= k and y <= k + 1: stddraw.line(j + 0.3, k + 0.7, j + 0.7, k + 0.3) stddraw.line(j + 0.3, k + 0.3, j + 0.7, k + 0.7) game_array_in_def[j][k] = 2 return game_array_in_def
def curve(x0, y0, x1, y1, variance, scaleFactor, n=11): if n == 0: stddraw.line(x0, y0, x1, y1) stddraw.show(0.0) return delta = stdrandom.gaussian(0, math.sqrt(variance)) delta1 = stdrandom.gaussian(0, math.sqrt(variance)) xm = (x0 + x1) / 2.0 + delta ym = (y0 + y1) / 2.0 + delta1 curve(x0, y0, xm, ym, variance / scaleFactor, scaleFactor, n - 1) curve(xm, ym, x1, y1, variance / scaleFactor, scaleFactor, n - 1)
def curve(a, x0, y0, x1, y1, var, beta, n=7): if n == 0: stddraw.line(x0, y0, x1, y1) return xm = (x0 + x1) / 2.0 ym = (y0 + y1) / 2.0 ix = int((((x0 + x1) / 2.0) * 128)) delta = stdrandom.gaussian(0.0, math.sqrt(var)) a[ix] = ym + delta curve(a, x0, y0, xm, ym + delta, var / beta, beta, n - 1) curve(a, xm, ym + delta, x1, y1, var / beta, beta, n - 1)
def draw_tree(n, x, y, size): if n <= 1: return stddraw.line(x, y, x - size / 1.3, y - size) stddraw.filledCircle(x - size / 1.3, y - size, 0.008) stddraw.line(x, y, x + size / 1.3, y - size) stddraw.filledCircle(x + size / 1.3, y - size, 0.008) draw_tree(n - 1, x - size / 1.3, y - size, size / 2) draw_tree(n - 1, x + size / 1.3, y - size, size / 2)
def curve(n, x0, y0, x1, y1, trials=10000, gap=.01, err=.0025): xm = (x0 + x1) / 2.0 ym = (y0 + y1) / 2.0 fxm = math.sin(xm) + math.cos(10 * xm) if (x1 - x0 < gap) or (abs(ym - fxm) < err): stddraw.line(x0, y0, x1, y1) stddraw.show(0.0) return curve(n, x0, y0, xm, fxm) stddraw.filledCircle(xm, fxm, .005) stddraw.show(0.0) curve(n, xm, fxm, x1, y1)
def curve(n, x0, y0, x1, y1, trials=100, gap=.01, err=.0025): xm = (x0 + x1) / 2.0 ym = (y0 + y1) / 2.0 fxm = estimate.evaluate(n, xm, trials) if (x1 - x0 < gap) or (abs(ym - fxm) < err): stddraw.line(x0, y0, x1, y1) stddraw.show(0.0) return curve(n, x0, y0, xm, fxm) stddraw.filledCircle(xm, fxm, .005) stddraw.show(0.0) curve(n, xm, fxm, x1, y1)
def curve(x0, y0, x1, y1, variance, scale_factor, n): if n == 0: stddraw.line(x0, y0, x1, y1) stddraw.show(0) return xmid = (x0 + x1) / 2 ymid = (y0 + y1) / 2 deltax = stdrandom.gaussian(0, math.sqrt(variance)) deltay = stdrandom.gaussian(0, math.sqrt(variance)) variance = variance / scale_factor curve(x0, y0, xmid + deltax, ymid + deltay, variance, scale_factor, n - 1) curve(xmid + deltax, ymid + deltay, x1, y1, variance, scale_factor, n - 1)
def _draw_lines(array, linecolor): """ @param array: Array with points @param linecolor: Color of points Draws points in given color, sorts them by their x value from smallest to largest and connects them with a line (TO BE USED FOR draw_data_random() FUNCTION) """ stddraw.setPenColor(linecolor) array = sorted(array, key=lambda point: point[0]) for i in range(len(array) - 1): stddraw.line(array[i][0], array[i][1], array[i+1][0], array[i+1][1])
def drawCanvas(): stddraw.setPenColor(stddraw.BLACK) stddraw.setPenRadius(0.5) stddraw.line(-3.33, -10.0, -3.33, 10.0) stddraw.line(3.33, -10.0, 3.33, 10.0) stddraw.line(-10.0, -3.33, 10.0, -3.33) stddraw.line(-10.0, 3.33, 10.0, 3.33)
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(): n = int(sys.argv[1]) stddraw.setYscale(-4, 4) stddraw.setYscale(0, 5) stddraw.setPenRadius(0.1) x = [0.0] * (n+1) y = [0.0] * (n+1) for i in range(n+1): x[i] = -0.4 + 0.8 * i/n y[i] = gaussian.pdf(x[i]) for i in range(n): stddraw.line(x[i], y[i], x[i+1], y[i+1]) stddraw._show()
def drawBoard(x, y): stddraw.setCanvasSize(600, 750) stddraw.setXscale(0, x) stddraw.setYscale(0, y + 3) stddraw.clear(stddraw.WHITE) #Draw the vertical lines for i in range(x + 1): stddraw.line(i / 1.5, 1, i / 1.5, y + 1) #Draw the horizontal lines for i in range(y + 1): stddraw.line(0, i, x / 1.5, i) drawScore(x, y) drawLogo(x, y) drawTotalScore(x, y, 0)
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 fouspike(n): m = 500 x = [0] * m y = [0] * m t = -10 for i in range(m): x[i] = t y[i] = 0 for j in range(1, n + 1): y[i] += math.cos(j * t) y[i] = y[i] / n t += 1 / 25 stddraw.setXscale(-11, 11) stddraw.setYscale(-3, 3) for i in range(m - 1): stddraw.line(x[i], y[i], x[i + 1], y[i + 1]) stddraw.show()
def curve(n, x0, y0, x1, y1): # print 'curve', N, x0, y0, x1, y1 gap = .01 err = .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, .005) stddraw.show() curve(n, xm, fxm, x1, y1)
def set_the_face(): stddraw.line(6 - 0.1, 6, 5.5, 6.5) stddraw.line(5.5, 6, 6 - 0.1, 6.5) stddraw.line(6 + 0.1, 6, 6.5, 6.5) stddraw.line(6.5, 6, 6 + 0.1, 6.5) for i in range(30): stddraw.point(6 + (i * 0.01), 5.6 - (i * 0.005)) stddraw.point(6 - (i * 0.01), 5.6 - (i * 0.005))
def draw_grid(self): # draw each cell of the game grid for row in range(self.grid_height): for col in range(self.grid_width): # draw the tile if the grid cell is occupied by a tile if self.tile_matrix[row][col] != None: self.tile_matrix[row][col].draw() # draw the inner lines of the grid stddraw.setPenColor(self.line_color) stddraw.setPenRadius(self.line_thickness) # x and y ranges for the game grid start_x, end_x = -0.5, self.grid_width - 0.5 start_y, end_y = -0.5, self.grid_height - 0.5 for x in np.arange(start_x + 1, end_x, 1): # vertical inner lines stddraw.line(x, start_y, x, end_y) for y in np.arange(start_y + 1, end_y, 1): # horizontal inner lines stddraw.line(start_x, y, end_x, y) stddraw.setPenRadius() # reset the pen radius to its default value
def _draw_coordinate_system(minX, maxX, minY, maxY): """ @param minX: smallest x value @param maxX: largest x value @param minY: smallest y value @param maxY: largest y value Draws axis and numbers them """ stddraw.setPenColor(color.WHITE) stddraw.setPenRadius(0.01) stddraw.line(minX - 1, 0, maxX + 1, 0) stddraw.line(0, minY - 1, 0, maxY + 1) for x in range(minX, maxX + 1): if not x == 0: stddraw.text(x, -0.5, str(x)) for y in range(minY, maxY + 1): if not y == 0: stddraw.text(-0.2, y, str(y))
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(n, lineLength, x, y): if n == 0: return x0 = x - lineLength/2 x1 = x + lineLength/2 y0 = y - lineLength/2 y1 = y + lineLength/2 stddraw.line(x0, y, x1, y) stddraw.line(x0, y0, x0, y1) stddraw.line(x1, y0, x1, y1) draw(n-1, lineLength/2, x0, y0) draw(n-1, lineLength/2, x0, y1) draw(n-1, lineLength/2, x1, y0) draw(n-1, lineLength/2, x1, y1)
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)
stddraw.filledCircle(0.5, 0.5, 0.45) # Draw hour markers. stddraw.setPenColor(stddraw.BLUE) for i in range(12): theta = math.radians(i * 30) stddraw.filledCircle(0.5 + 0.4 * math.cos(theta), \ 0.5 + 0.4 * math.sin(theta), .025) # Draw second hand. stddraw.setPenRadius(.01) stddraw.setPenColor(stddraw.YELLOW) angle1 = math.radians(6 * seconds) r1 = 0.4 stddraw.line(0.5, 0.5, \ 0.5 + r1 * math.sin(angle1), \ 0.5 + r1 * math.cos(angle1)) # 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)
import stddraw import sys import math # Accept integer command-line argument n. Plot the function # y = sin(4x) + sin(20x) # between x = 0 and x = pi by drawing n line segments. # Accept the number of line segments to plot. n = int(sys.argv[1]) stddraw.createWindow() # The function y = sin(4x) + sin(20x), sampled at n points # between x = 0 and x = pi. x = stdarray.create1D(n+1, 0.0) y = stdarray.create1D(n+1, 0.0) for i in range(n+1): x[i] = math.pi * i / n y[i] = math.sin(4.0*x[i]) + math.sin(20.0*x[i]) # Rescale the coordinate system. stddraw.setXscale(0, math.pi) stddraw.setYscale(-2.0, +2.0) # Plot the approximation to the function. for i in range(n): stddraw.line(x[i], y[i], x[i+1], y[i+1]) stddraw.show() stddraw.wait()
# Make one random move. r = random.random() sum = 0.0; for j in range(n): # Find interval containing r. sum += p[page][j] if r < sum: page = j break freq[page] += 1 if i % 1000 == 0: # Plot histogram of frequencies stddraw.clear(); for k in range(n): stddraw.line(k, 0, k, freq[k]) stddraw.show() # Print page ranks. for i in range(n): stdio.writef("%8.5f", float(freq[i]) / float(t)) stdio.writeln() stddraw.wait() #----------------------------------------------------------------------- # Example executions: # # python transition.py < tiny.txt | python randomsurferhistogram.py 1000000 # python transition.py < medium.txt | python randomsurferhistogram.py 1000000
def goForward(self, step): oldx = self._x oldy = self._y self._x += step * math.cos(math.radians(self._angle)) self._y += step * math.sin(math.radians(self._angle)) stddraw.line(oldx, oldy, self._x, self._y)
#----------------------------------------------------------------------- # triangle.py #----------------------------------------------------------------------- import stddraw import math # Draw a triangle. t = math.sqrt(3.0) / 2.0 stddraw.createWindow() stddraw.line(0.0, 0.0, 1.0, 0.0) stddraw.line(1.0, 0.0, 0.5, t) stddraw.line(0.5, t, 0.0, 0.0) stddraw.point(0.5, t/3.0) stddraw.show() stddraw.wait()
import stddraw import sys import math # Accept integer command-line argument n. Draw an n petal rose (if n # is odd) and a 2n petal rose (if n is even). n = int(sys.argv[1]) stddraw.createWindow() stddraw.setXscale(-1, +1); stddraw.setYscale(-1, +1); stddraw.setPenColor(stddraw.PINK); x0 = 0.0 y0 = 0.0 t = 0.0 while t <= 360.0: theta = math.radians(t) r = math.sin(n * theta) x1 = r * math.cos(theta) y1 = r * math.sin(theta) stddraw.line(x0, y0, x1, y1) x0 = x1 y0 = y1 t += 0.1 stddraw.show() stddraw.wait()