Hi!
I am working on a sketch to generate symmetrical patterns consisting of 4 parts. Right now I am just using random values for testing. The way it works is that I generate an initial list of lists of random values. Each sub-list corresponds to one row in a segment. Then I use the built-in reverse() function and the custom deep_reverse function to generate mirrored lists of the initial list. Here is the code. It’s long but I think it’s necessary to see the whole thing to be clear here:
def settings():
global total,side,cell
cell = 100 # size of 1 cell in a segment
side = 2 # number of cells for one side
total = side**2 # total number of cells
sizer = side * cell * 2 # calculate sketch size
size(sizer,sizer)
def is_list(p):
return isinstance(p, list)
def deep_reverse(mylist):
result = []
for e in mylist:
if isinstance(e, list):
result.append(deep_reverse(e))
else:
result.append(e)
result.reverse()
return result
def draw():
noStroke()
tl,tr,bl,br = [],[],[],[]
# generate list of values for the first segment, top-left
val = 0
for y in range(side):
inter = []
for x in range(side):
rand = int(random(256))
val = rand
inter.append(val)
tl.append(inter)
# draw top-left segment
for y in range(side):
for x in range(side):
fill(tl[y][x])
rect(x*cell,y*cell,cell,cell)
# draw bottom-right segment
br = deep_reverse(tl)
for y in range(side):
for x in range(side):
fill(br[y][x])
rect(x*cell + width/2, y*cell + height/2, cell,cell)
# draw bottom-left segment
bl = tl
bl.reverse()
for y in range(side):
for x in range(side):
fill(bl[y][x])
rect(x*cell, y*cell + height/2, cell,cell)
# draw top-right segment
tr = deep_reverse(bl)
for y in range(side):
for x in range(side):
fill(tr[y][x])
rect(x*cell + width/2, y*cell, cell,cell)
noLoop()
Here’s an example image with the settings just as in the code posted:
To clarify, I generate the top-left square first, which here consists of 4 cells with random fill() values. Then I derive the remaining squares from the initial one.
This all works fine but I would like to make this more flexible. It’s also a lot of code. Are there more concise ways for doing the same thing? I have a hunch it might be a good idea to separate the actual values from the list initiation process. Meaning that instead of generating 4 lists of fill values, I could just make 1 list of fill values and then iterate over them using a list of indices. For this I would need to generate a list of indices reversed and mirrored like the other ones.
Code for what I mean:
fills =[50,100,150,200] # fill values for the initial pattern
index = [0, 1, 1, 0, 2, 3, 3, 2, 2, 3, 3, 2, 0, 1, 1, 0] # index list for image generation
for i in index:
fill(fills[index[i]])
But I have trouble generating such a list of indices in order to be able to scale it up.
Any insight or different angle on this problem is greatly appreciated. thank you!