# Mirroring lists for symmetrical images

**URL:** <https://discourse.processing.org/t/mirroring-lists-for-symmetrical-images/31844>\
**Category:** Processing.py\
**Created:** [August 22, 2021, 6:20pm UTC](https://discourse.processing.org/t/mirroring-lists-for-symmetrical-images/31844 "2021-08-22T18:20:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 22, 2021, 6:20pm UTC](https://discourse.processing.org/t/mirroring-lists-for-symmetrical-images/31844/1 "2021-08-22T18:20:22Z")

</div>

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:

```auto
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:

![Screenshot (654)](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2e535f3e6affd2770da49fc351871bd0e31200d3.png)  
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [August 23, 2021, 9:19am UTC](https://discourse.processing.org/t/mirroring-lists-for-symmetrical-images/31844/2 "2021-08-23T09:19:51Z")

</div>

This doesn’t really answer your question, but –

Have you considered using Processing’s transformation functions to reflect the top-left corner?

```python
size(200, 200)

def drawTriangle():
    triangle(0, 0, 100, 0, 0, 100)

# top-left triangle
drawTriangle()

# top-right triangle
translate(width, 0)
scale(-1, 1)
drawTriangle()

# bottom-right triangle
translate(0, height)
scale(1, -1)
drawTriangle()
...

```

---

<div class="post-metadata">

**Author:** ![LindowMan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lindowman/32/14031_2.png) [@LindowMan](https://discourse.processing.org/u/LindowMan)\
**Post date:** [August 25, 2021, 8:18am UTC](https://discourse.processing.org/t/mirroring-lists-for-symmetrical-images/31844/3 "2021-08-25T08:18:28Z")

</div>

AH thanks! Actually no, I hadn’t. i figured out a solution in the meantime but wasn’t aware of the functions you mentioned. Will be helpful in the future.
