Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Blog
Views: 52
Author: Vincent J. Matsko Date: 1 November 2015, Day011 Post: Evaporation I
# Again, remember shift+enter to evaluate! from sage.plot.colors import rgbcolor # Makes sure r is in the correct range (that is, between 0 and 1). # You might add or subtract to an RGB value and inadvertantly go outside the range [0,1]. def RGBValue (r): if r < 0: return 0 else: if r > 1: return 1 else: return r # Note that this routine adds the delta to the colors. You can subtract, too (just change the code). def RandomColor(base, delta): return rgbcolor([RGBValue(base[count] + delta * random()) for count in range(3)]) # As arguments, provide the width, height, color (RGB values), delta (change in the color parameters), and the random number seed. def ColorSquare(width, height, color, delta, seed): squares = [] # Saves the points in the final image. set_random_seed(seed) # Sets the random number generator. Using different values produces different results. for i in range(width): for j in range(height): # Uses the append method to add the next square. # This is a feature of object-oriented programming. squares.append(plot(polygon2d([(i,j),(i+1,j),(i+1,j+1),(i,j+1)], rgbcolor=RandomColor(color, delta)))) # Python uses the "+" to mean many differnet things. You can "add" graphics objects using "sum" and output them all together. P = sum(squares) show(P, aspect_ratio=1, svg=True, axes=False, xmin=-1, xmax=width+1, ymin=-1, ymax=height+1) # This shows you how you would save your image to a file. save(P, 'colorsquare.svg')
ColorSquare(20, 20, [0.0,0.2,0.9], 1, 47)
def RandomGray(gray, delta): change = delta * random (); return rgbcolor([gray + change, gray + change, gray + change]); # As arguments, provide the width, height, level of gray, base radius for circles, # change in radius, change in gray, and the random number seed. def TextureSquare(width, height, gray, r, rdelta, cdelta, seed): disks = [] # Saves the points in the final image. set_random_seed(seed) # Sets the random number generator. Using different values produces different results. for i in range(width): for j in range(height): disks.append(plot(disk((i,j), r + rdelta * random(), (0,2*pi), rgbcolor=RandomGray(gray, cdelta)))) P = sum(disks) show(P, aspect_ratio=1, svg=True, axes=False, xmin=-1, xmax=width+1, ymin=-1, ymax=height+1)
# Example just changing the radii of the circles. # Just make the delta 0 (sixth argument to the function) so there is no change in color. # Note that the gray is defined by a number between 0 (black) and 1 (white). TextureSquare(100, 100, 0.3 ,0.4, 0.3, 0.5, 44)
# Example just changing the color of the circles. # Just make the rdelta 0 (fifth argument to the function) so there is no change in radius. TextureSquare(20, 20, 0.3 ,0.45, 0, 0.3, 42)
# Example changing both the radius and the color of the circles. TextureSquare(20, 20, 0.3 ,0.4, 0.1, 0.3, 42)