# 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:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [November 27, 2021, 2:45pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/1 "2021-11-27T14:45:44Z")

</div>

Hi there! I am trying to adapt a truchet tiles Code but I think that I have made a mess 😅  
I don’t undestand very well Range() in spite i have seen reference. Could you tell me what I have doing wrong, please? Thanks!

```auto
size(1000,1000);
 background(#004477);
 noFill();
 stroke(#FFFFFF);
  strokeWeight(3);
   arc(0,0, 100,100, 0,PI/2);
   arc(100,100, 100,100, PI,PI+PI/2);
   col = 0;
   row = 0;
   
   for i in range(1,145): 
   print( int(random(2)) == 1 );
   
   if int(random(2)) == 1: 
   arc(col,row, 50,50, 0,PI/2);
   arc(col+50,row+50, 50,50, PI,PI+PI/2);
   
   else: 
   arc(col+50,row, 50,50, PI/2,PI);
   arc(col,row+50, 50,50, PI+PI/2,2*PI);
   
   col += 50;
   if i%12 == 0: 
   row += 50;
   col = 0;

   

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 27, 2021, 3:13pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/2 "2021-11-27T15:13:56Z")

</div>

> **[Sébastien Truchet](https://en.m.wikipedia.org/wiki/S%C3%A9bastien_Truchet)**
>
> Jean Truchet (1657 – 5 February 1729), known as Father Sébastian, was a French Dominican priest born in Lyon, who lived under the reign of Louis XIV. He was active in areas such as mathematics, hydraulics, graphics, typography, and for many inventions.
> Truchet was born in 1657, the son of a merchant father and a very pious mother. At age 16, he joined the Discalced Carmelites. He took the name Sébastien to honor his mother, who was named Sébastiane. In 1693, he was selected by Abbé Bignon to ...

And

> **[Square tiling](https://en.wikipedia.org/wiki/Square_tiling)**
>
> In geometry, the square tiling, square tessellation or square grid is a regular tiling of the Euclidean plane. It has Schläfli symbol of {4,4}, meaning it has 4 squares around every vertex.
> Conway called it a quadrille.
> The internal angle of the square is 90 degrees so four squares at a point make a full 360 degrees. It is one of three regular tilings of the plane. The other two are the triangular tiling and the hexagonal tiling.
> There are 9 distinct uniform colorings of a square tiling. Nami...

And

[https://mathworld.wolfram.com/TruchetTiling.html](https://mathworld.wolfram.com/TruchetTiling.html)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 27, 2021, 3:17pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/3 "2021-11-27T15:17:57Z")

</div>

Hello,

You need to indent your code properly for Python.

I commented a couple of unnecessary lines.

It seems to work:

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

You get different results for each of these lines for `int(random(2)` :

> [@humano](#):
>
> ```auto
> print( int(random(2)) == 1 );
>    
> if int(random(2)) == 1: 
> 
> ```

I suggest you calculate first and then `print()` and use `if()` conditional with `tmp`:

`tmp = int(random(2));`

`:)`

---

<div class="post-metadata">

**Author:** ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)\
**Post date:** [November 27, 2021, 3:26pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/4 "2021-11-27T15:26:27Z")

</div>

And how could I indent my Code properly for Python? Thanks!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 27, 2021, 3:46pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/5 "2021-11-27T15:46:22Z")

</div>

> [@humano](#):
>
> And how could I indent my Code properly for Python? Thanks!

Do some research and share what you find with us.

[https://discourse.processing.org/t/guidelines-asking-questions/2147#what-has-your-research-told-you-7](https://discourse.processing.org/t/guidelines-asking-questions/2147#what-has-your-research-told-you-7)

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 27, 2021, 5:56pm UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/6 "2021-11-27T17:56:07Z")

</div>

you need to indent everything below the for loop.

also you need to indent the 2 lines under if and under else additionally.

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [November 28, 2021, 2:03am UTC](https://discourse.processing.org/t/truchet-tiles-quarter-circles/33786/7 "2021-11-28T02:03:45Z")

</div>

> [@humano](#):
>
> how could I indent my Code properly for Python?

In Python, a code block within a program begins with a header. Block headers end with a colon. Code within a block must be indented relative to the header. In fact, indentation defines the block structure of a Python program to a similar degree with which curly braces define the structure of a Java or JavaScript program.

Blocks may be nested inside other blocks, and that must be specified via nested indentations.

In Python, some common types of code blocks include the following:

- conditional blocks
- loops
- function definitions
- class definitions

There are additional types of code blocks, some of which relate to exception handling and file processing.

The degree of indentation needs to be consistent within a block.

The code in your original post includes a `for` loop as well as `if` and `else` conditional blocks that require proper indentation within. None of the code that precedes the `for` loop should be indented.

In Python, semicolons are not needed at ends of lines.

_EDITED (November 28, 2021) to revise some of the above and add the following:_

For information on indentation, see this portion of PEP 8:

> **[PEP 8 – Style Guide for Python Code | peps.python.org](https://peps.python.org/pep-0008/)**
>
> Python Enhancement Proposals (PEPs)

---

<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)
