Reputation: 43
I am using this grid.py coloring program here https://scipython.com/blog/a-simple-colouring-grid/ The relevant code that makes the grid is
self.cells = []
for iy in range(n):
for ix in range(n):
xpad, ypad = pad * (ix+1), pad * (iy+1)
x, y = xpad + ix*xsize, ypad + iy*ysize
rect = self.w.create_rectangle(x, y, x+xsize,
y+ysize, fill=UNFILLED)
self.cells.append(rect)
I was wondering if I could get it so the squares stagger as shown below:
Upvotes: 1
Views: 190
Reputation: 36662
You could add/subtract a "stagger offset" for each x coordinates of the cells on a given row. The sign of the value determined by the rank of the 'row' by multiplying the offset by (-1)**row
.
Maybe like this?:
self.cells = []
for row in range(n):
stagger_offset = (xsize // 4) * (-1)**row # will be positive for even rows, negative otherwise
y0 = pad * (row + 1) + row * ysize
y1 = y0 + ysize
for col in range(n):
x0 = pad * (col + 1) + col * xsize + stagger_offset
x1 = x0 + xsize
self.cells.append(self.w.create_rectangle(x0, y0, x1, y1, fill=UNFILLED))
You may have to insert half cells at the beginning/end of even/odd rows, depending on what visual aspect you prefer.
Upvotes: 2