Truchet tiles quarter circles

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

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;

   

1 Like

And

And

https://mathworld.wolfram.com/TruchetTiling.html

Hello,

You need to indent your code properly for Python.

I commented a couple of unnecessary lines.

It seems to work:

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

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

tmp = int(random(2));

:)

2 Likes

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

:)

you need to indent everything below the for loop.

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

1 Like

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:

5 Likes

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

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

5 Likes