Cellular automaton



  • here is some code to generate cellular automaton patterns from a random seed

    '''
    Cellular Automaton with drawbot - feb 2018 j. lang
    • cell_s sets the cell size. the smaller the value the longer the script will run. 
    • rule is the rule to be used to generate the pattern. values higher than 255 will be the same as half of that value. some rules are more interesting than others. 
    see https://en.wikipedia.org/wiki/Elementary_cellular_automaton
    '''
    
    # --------
    # settings 
    # --------
    
    cell_s = 4  
    rule   = 106 
    
    # amount of rows
    row_a = int(height() / cell_s) 
    # length of the visible row 
    row_l = int(width()/cell_s) + 1 
    # a random starting row 
    grid = [ [i for i in range(- row_a, row_l + row_a) if random() > .5] ] 
    # convert the rule to a binary string 
    pattern = bin(rule)[2:].zfill(8)
    # all possible combinations
    combis = [ [1,1,1], [1,1,0], [1,0,1], [1,0,0], [0,1,1], [0,1,0], [0,0,1], [0,0,0] ]
    # the valid combinations for the rule 
    valid = [ combis[c] for c in range(8) if pattern[c] == '1'  ]
    
    # -------–
    # drawing
    # --------
    
    # start from the top
    translate(0, height())
    
    for r in range(row_a):
    
        row = grid[r]
        new_row = []
        
        for cell in range( - row_a, row_l + row_a ): 
            check = [ (cell - 1) in row, cell in row, (cell + 1) in row ]
    
            if check in valid: 
                new_row.append( cell )
                if 0 <= cell <= row_l:
                    rect( cell_s * cell - cell_s/2, - cell_s * r - cell_s, cell_s, cell_s)
    
        grid.append( new_row )
    
    # saveImage('~/Desktop/cellular_automaton_rule{0}.png'.format(rule) )        
    

    example of rule 106

    0_1518779780759_cellular_automato_rule106.jpg

    example of rule 73

    0_1518779980575_cellular_automato_rule73.png



  • the script could be used to get a scarf knitted.
    good idea. thanks for the tipp @frederik

    rule 122 on one side which makes the other side rule 161

    0_1522065214890_scarf.jpg


  • admin

    wicked!!! 👍



  • @jo Beautiful!!!


Log in to reply