tiles 001

Working towards Truchet tiles

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);

// Global code will be evaluated once.
const turtle = new Turtle();
const gap = 12; //min=1 max=51 step=1
const gridCount = 15; //min=1 max=40 step=1
const size = 11; //min=5 max=50 step=1
const xOffset = -90; //min=-100 max=80 step=1
const yOffset = -90; //min=-100 max=80 step=1
const outline = 0;//min=0 max=1 step=1 (No, Yes)
const random = 0;//min=0 max=1 step=1 (No, Yes)
let r = 0;// //min=0 max=3 step=1
let t = 0;

if(random === 0){
    r = randomInRange(0, 3); // Pick a random number 
}

for(let x=0; x < gridCount; x++){
    for(let y=0; y < gridCount; y++){

    square((x * gap) + xOffset, (y * gap)+ yOffset);
    }
}


function square(x,y) {
    turtle.penup();
    turtle.goto(x,y);
    turtle.pendown();
    
    // only draw the outline square if required
    if(outline === 1){
        for(let i=0; i< 4; i++){
            turtle.forward(size);
            turtle.right(90);
        }
    }
    
    
    if(random === 1){
       r = randomInRange(0, 3); // Pick a random number 
    }
    
    switch(r) {
    case 0:
        turtle.pendown();
        turtle.circle(size, extent = 90);
        turtle.circle(size, extent = -90);
        turtle.penup();
    break;
    
    case 1:
        turtle.left(90);
        turtle.pendown();
        turtle.circle(size, extent = -90);
        
        turtle.penup();
        turtle.circle(size, extent = 90);
        turtle.right(90);
    break;
    
    case 2:
        turtle.penup();
        turtle.forward(size);
        turtle.right(90);
        turtle.pendown();
        turtle.circle(size, extent = 90);
        
        turtle.penup();
        turtle.circle(size, extent = -90);
        turtle.left(90);
        turtle.backward(size);
    break;
    
    case 3:
     turtle.penup();
     turtle.forward(size);
     turtle.pendown();
     turtle.circle(size, extent = -90);
     
     turtle.penup();
     turtle.circle(size, extent = 90);
     turtle.backward(size);
     break;
     
     default:
    }  // end of case
}

function randomInRange(from, to) {
  var r = Math.random();
  return Math.floor(r * (to - from) + from);
}