# Truchet tiles quarter circles

**URL:** <https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786>\
**Category:** Processing.py\
**Created:** [November 27, 2021, 2:45pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786 "2021-11-27T14:45:44Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [January 15, 2022, 10:23pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/8 "2022-01-15T22:23:54Z")

</div>

This **is not a solution to the question**. But I thought you all might enjoy seeing another nice Truchet example.

```python
# 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 

from random import choice

tile_size = 50 
rows = 50
cols = 50

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 * tile_size, i * tile_size, tile_size, ic, oc)
            color_swap(i, j)
            tiles[i][j].display()
        
def color_swap(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].orientation == tiles[i][0].orientation:                    
            # set to opposite coloring of my neighbor above
            tiles[i][0].swapped_colors = not tiles[i-1][0].swapped_colors
        else:
            # set to same coloring of my neighbor above
            tiles[i][0].swapped_colors = tiles[i-1][0].swapped_colors
    if j > 0: # subsequent tiles in a row, including the first
        # same orientation as tile to the left
        if tiles[i][j-1].orientation == tiles[i][j].orientation:
            # set to opposite coloring of my neighbor to the left
            tiles[i][j].swapped_colors = not tiles[i][j-1].swapped_colors
        else:
            # set to same coloring of my neighbor to the left 
            tiles[i][j].swapped_colors = tiles[i][j-1].swapped_colors
            
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.orientation = choice((0, 1)) # orientation of tile
        # whether we should swap inside and outside colors
        self.swapped_colors = False

    def display(self):
        pushMatrix()
        # move to tile's x-y location (upper left corner)
        translate(self.x, self.y)
        noStroke()
        if (self.swapped_colors):
            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.orientation * PI / 2) # rotate by the appropriate angle
        translate(-self.sz / 2, -self.sz / 2) # back to the upper left corner
        stroke(255)
        strokeWeight(3)
        if self.swapped_colors:
            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()

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d1736eaa0d1c9945e79182dab26216d55d45687c.png)

---

_[View the full topic](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786)._
