Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
canvas=500
divider=10
# made in drawbot - drawbot.com #
### this is a 'grid builder' that does the math separates the grid into random integers that add up to fill the entire thing. The variance in randomness can be controlled with the multiplier on the variance variable. we will use two arrays from this function, one to build rows, and the other to build columns. I based it off code found here: http://sunny.today/generate-random-integers-with-fixed-sum/ ###
def generate_random_integers(_sum, n):
mean = _sum / n
variance = int(7 * mean)
min_v = int(mean - variance)
max_v = int(mean + variance)
array = [min_v] * n
diff = _sum - min_v * n
while diff > 0:
a = randint(0, n - 1)
if array[a] >= max_v:
continue
array[a] += 1
diff -= 1
# print(array)
return(array)
#### makes sense to make a function for building a single column, and repeating w/translate ####
def drawColumn(boxWidths, i):
with savedState():
for j in range(rows):
if j==0:
fill(random())
rect(divider,divider,boxWidths[i], boxHeights[j]-divider*2)
else:
fill(random())
translate(0,boxHeights[j-1])
rect(divider,0,boxWidths[i], boxHeights[j]-divider)
#### time to build the grid ####
for f in range(100):
rows=randint(3,5)
cols=randint(3,5)
boxHeights=generate_random_integers(canvas,rows)
boxWidths=generate_random_integers(canvas-(divider*cols+divider),cols)
newPage(canvas,canvas)
fill(1)
rect(0,0,canvas,canvas)
frameDuration(1/2)
for i in range(cols):
if i==0:
drawColumn(boxWidths, i)
else:
translate(boxWidths[i-1]+divider, 0)
drawColumn(boxWidths, i)
saveImage("mondrianmonochrome.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.