def main(): n = int(sys.argv[1]) for i in range(n): x = stdrandom.gaussian(0.5, 0.2) y = stdrandom.gaussian(0.5, 0.2) stddraw.point(x, y) stddraw.show()
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 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 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(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 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 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 fill_brownian(a, i0, i1, variance, scale): if i1 - i0 == 1: return Xm = int((i0 + i1) / 2) ym = (a[i0] + a[i1]) / 2 delta = stdrandom.gaussian(0, math.sqrt(variance)) a[Xm] = ym + delta fill_brownian(a, i0, Xm, variance / scale, scale) fill_brownian(a, Xm, i1, variance / scale, scale)
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 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 g20(n): l = [] a = [0] * 20 for i in range(n): l.append(stdrandom.gaussian()) #print('l:',l) i = -5 while i < 5: for t in l: if i < t < (i + 0.5): a[int(2 * i) + 10] += 1 i += 0.5 draw(a) return a
# Read values from standard input to create an array of charged # particles. Set each pixel color in an image to a grayscale value # proportional to the total of the potentials due to the particles at # corresponding points. Draw the resulting image to standard draw. MAX_GRAY_SCALE = 255 # Read charges from standard input into an array. #n = stdio.readInt() n = eval(sys.argv[1]) charges = stdarray.create1D(n) for i in range(n): x0 = round(stdrandom.uniformFloat(0, 1), 2) y0 = round(stdrandom.uniformFloat(0, 1), 2) q0 = stdrandom.gaussian(50, 150) charges[i] = Charge(x0, y0, q0) print(str(charges[i])) # Create a Picture depicting potential values. pic = Picture() for col in range(pic.width()): for row in range(pic.height()): # Compute pixel color. x = 1.0 * col / pic.width() y = 1.0 * row / pic.height() v = 0.0 for i in range(n): v += charges[i].potentialAt(x, y) v = (MAX_GRAY_SCALE / 2.0) + (v / 2.0e10)
def maxwellBo(sigma=1.0): maxwellbo = math.sqrt( stdrandom.gaussian(0, sigma)**2 + stdrandom.gaussian(0, sigma)**2 + stdrandom.gaussian(0, sigma)**2) return maxwellbo