Turn grid into a checkerboard pattern in python?


I have successfully created a grid, but am now trying to turn my grid into a checkerboard pattern, preferably using a variant of the floodfill command.

how do I make sure the program recognizes which squares are even and which are odd?

currently the IDE is set so m[i][j]= 1 gives blue, while m[i][j]= 0 gives red, which I am happy to keep, and so I do not need to define the colors. Thank you.

Code I have so far :

from pylab import *
from numpy import *
from math import *

m=zeros((100,100))
for i in range(100):
    for j in range(100):
        if (math.floor(i) % 10) != 0:
            if (math.floor(j) % 10) != 0:
                m[i][j]= 1
            else:
                m[i][j]= 0

imshow(m)
show()

Code output :

enter image description here


Answers:


You can check the sum of the two indices (row and column) and color it with the first color if it's odd and second otherwise. Something like:

for i in range(nrows):
    for j in range(ncols):
        m[i][j] = 0 if (i+j)%2 else 1