How to create grid square patterns and digital camo

Continuing the 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 series, see this page: 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)

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

    # Between 0 and 25
    if (random(1) <= rand_point1):
        rect(x, y, spacing, spacing)
    # Between 25 and 50
    elif (random(1) > rand_point1 and random(1) <= rand_point2):
        rect(x, y, spacing, spacing)
    # Between 50 and 75
    elif (random(1) > rand_point2 and random(1) <= rand_point3):
        rect(x, y, spacing, spacing)
        rect(x, y, spacing, spacing)
    x += spacing

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


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

Leave a Reply