Twirling Fans

An alternating pattern of fans

Log in to post a comment.

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

// Canvas is 200x200
const canvas_size = 200;

// Number of rows/columns in the grid
const size = 6; // min=1, max=40, step=1

// Number of segments in each fan
const segments = 10; // min=2, max=20, step=1

// Position of inner band
const inner = 0.5; // min=0, max=2, step=0.1

// Position of outer band
const outer = 1.0; // min=0, max=2, step=0.1

// Length of ribs
const ribs = 1.0; // min=0, max=2, step=0.1

// Size of each box in the grid
const box_size = canvas_size / size;

let shape_orientations = [
    { dx: 0, dy:0, heading:0 },
    { dx: box_size, dy: box_size, heading:180 },
];

// The walk function will be called until it returns false.
function walk(i) {
    let x = parseInt(i % size, 10);
    let y = parseInt(i / size, 10);
    let shape_orientation = shape_orientations[0];

    // Checkboard pattern
    if (x % 2 != y % 2) {
      shape_orientation = shape_orientations[1];
    }
    
    // Move to the appropriate corner and heading in the grid box
    turtle.jump(x * box_size - canvas_size / 2 + shape_orientation.dx,
                y * box_size - canvas_size / 2 + shape_orientation.dy);
    turtle.setheading(shape_orientation.heading)

    // Draw a fan
    draw_fan();
    
    // Check if this is the last grid box
    return i < (size * size) - 1;
}

function draw_fan() {
    let pos = turtle.pos();
    let heading = turtle.heading();

    // Draw inner fan curve    
    turtle.penup();
    turtle.forward(box_size * inner)
    turtle.right(90);
    turtle.pendown();
    turtle.circle(box_size * inner, 90)

    // Reset
    turtle.penup();
    turtle.goto(pos)
    turtle.setheading(heading)
    turtle.pendown();

    // Draw outer fan curve
    turtle.penup();
    turtle.forward(box_size * outer);
    turtle.right(90);
    turtle.pendown();
    turtle.circle(box_size * outer, 90)
    
    // Reset
    turtle.penup();
    turtle.goto(pos)
    turtle.setheading(heading)
    turtle.pendown();
    
    // Draw fan ribs
    for (i=0; i<=segments; i++) {
      turtle.forward(box_size * ribs);
      turtle.penup();
      turtle.goto(pos);
      turtle.right(90 / segments);
      turtle.pendown();
    }
}