Processing.py: How to create grid square patterns and digital camo

Continuing the Processing.py series, this article demonstrates how to use a slight variant of the 10 PRINT example to build a grid pattern design made up of squares of differing sizes and colors. This technique can also produce digital camo (camouflage) patterns!

For the previous entry in the Processing.py series, see this page:

Processing.py: How to implement 10 PRINT

The variability of the patterns can be determined by using color, square size, backgrounds, square borders (usingĀ  stroke()) , and using random() with if statements. In the code shown below, random(1) returns a floating point number between 0 and 1 and outputs are determined by where the random number falls between 0 and 100. More ranges can be added for more variability by filtering outputs into smaller and smaller buckets.

x = 0
y = 0
spacing = 20
rand_point1 = 0.25
rand_point2 = 0.50
rand_point3 = 0.75

def setup():
    size(400, 400)
    background(255)
    #stroke(255)
    #stroke(0)
    #strokeWeight(3)

def draw():
    global x, y, spacing, rand_point
    noStroke()

    # Between 0 and 25
    if (random(1) <= rand_point1):
        #fill('#5D4C2E')
        fill(0)
        rect(x, y, spacing, spacing)
    # Between 25 and 50
    elif (random(1) > rand_point1 and random(1) <= rand_point2):
        #fill('#A29980')
        fill(64)
        rect(x, y, spacing, spacing)
    # Between 50 and 75
    elif (random(1) > rand_point2 and random(1) <= rand_point3):
        #fill('#C1B594')
        fill(128)
        rect(x, y, spacing, spacing)
    else:
        #fill('#CECECE')
        fill(255)
        rect(x, y, spacing, spacing)
    x += spacing

    if x > width:
        x = 0
        y += spacing

    saveFrame("print10_rect21.gif")

Here are some of the patterns I was able to create by playing with these variables:

Leave a Reply