Skip to content

Instantly share code, notes, and snippets.

@villares
Last active May 28, 2021 02:34
Show Gist options
  • Save villares/de4656b52522d68fdcb6ca19d20beeea to your computer and use it in GitHub Desktop.
Save villares/de4656b52522d68fdcb6ca19d20beeea to your computer and use it in GitHub Desktop.
Two colour truchet tiles
# Translated to Processing Python mode from the Java example at
# "Processing: Creative Coding and Generative Art in Processing 2" by Ira Greenberg, Dianna Xu, Deepak Kumar
tileSize = 50 # tamanho_grid
rows = 50 # n_linhas
cols = 50 # n_colunas,
tiles = [[None] * rows for _ in range(cols)]
ic = color(100, 125, 0) # orange # ic = color(100, 125, 0)
oc = color(20, 150, 255) # blue # oc = color(20, 150, 255)
def setup():
size(1000, 1000)
smooth()
for i in range(rows):
for j in range(cols):
tiles[i][j] = Tile(j*tileSize, i*tileSize, tileSize, ic, oc)
colorSwap(i, j)
tiles[i][j].display()
def colorSwap(i,j):
if i > 0 and j == 0: # first tile of a row, starting from the 2nd row
# same orientation as tile directly above
if (tiles[i-1][0].intorient == tiles[i][0].intorient):
# set to opposite coloring of my neighbor above
tiles[i][0].swapColors = not tiles[i-1][0].swapColors
else:
# set to same coloring of my neighbor above
tiles[i][0].swapColors = tiles[i-1][0].swapColors
if j > 0: # subsequent tiles in a row, including the first
# same orientation as tile to the left
if (tiles[i][j-1].intorient == tiles[i][j].intorient):
# set to opposite coloring of my neighbor to the left
tiles[i][j].swapColors = not tiles[i][j-1].swapColors
else:
# set to same coloring of my neighbor to the left
tiles[i][j].swapColors = tiles[i][j-1].swapColors
class Tile:
def __init__(self, x, y, w, ic, oc):
self.x, self.y = x, y # x, y coords of top left corner of tile
self.sz = w # size of tile
self.ic = ic # inside – fill of arc if swapColor is False
# outside – fill of background square if swapColor is False
self.oc = oc
self.orient = random(1, 3) # orientation of tile
self.intorient = int(self.orient) # orientation of tile
# whether we should swap inside and outside colors
self.swapColors = False
def display(self):
pushMatrix()
translate(self.x, self.y) # move to tile's x-y location (upper left corner)
noStroke()
if self.swapColors:
fill(self.ic)
else:
fill(self.oc)
rect(0, 0, self.sz, self.sz) # draw background square
translate(self.sz / 2, self.sz / 2) # move to the center of the tile
rotate(self.intorient * PI / 2) # rotate by the appropriate angle
# move back to the upper left corner
translate(-self.sz / 2, -self.sz / 2)
stroke(255)
strokeWeight(3)
if self.swapColors:
fill(self.oc)
else:
fill(self.ic)
arc(0, 0, self.sz, self.sz, 0, PI / 2)
arc(self.sz, self.sz, self.sz, self.sz, PI, 3 * PI / 2)
popMatrix()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment