Overlapping circles

Simple pattern of overlapping circles

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 square_size = 1; // min = .5 max = 10 step = .1
const lines_per_square = 1; // min = 1 max = 4 step = 1
const num_circles = 10; // min = 0 max = 50 step = 1

class Circle {
    constructor(cx, cy, r) {
        this.cx = cx;
        this.cy = cy;
        this.r = r;
    }
}

circles = [];
for(let i = 0; i < num_circles; i++) {
    let circle = new Circle(Math.random() * 200 - 100, Math.random() * 200 - 100, Math.random() * 25 + 25)
    circles.push(circle);
}

for(let i = -100; i < 100; i+=square_size) {
    for(let j = -100; j < 100; j+=square_size) {
        circle_count = 0;
        for(circle in circles) {
            if(Math.sqrt((circles[circle].cx-i)*(circles[circle].cx-i) + (circles[circle].cy-j)*(circles[circle].cy-j)) < circles[circle].r) {
                circle_count++;
            }
        }
        if(circle_count % 2 == 0) {
            vertical_line_square(i, j, lines_per_square, square_size)
        }
        else {
            horizontal_line_square(i, j, lines_per_square, square_size)
        }
        
    }
}

function vertical_line_square(x, y, num_lines, size) {
    for(let i = 0; i < num_lines; i++) {
        turtle.penup();
        turtle.goto(x + -size/2 + size/(2*num_lines) + i*size/num_lines, y + size/2);
        turtle.pendown();
        turtle.goto(x + -size/2 + size/(2*num_lines) + i*size/num_lines, y - size/2);
    }
}

function horizontal_line_square(x, y, num_lines, size) {
    for(let i = 0; i < num_lines; i++) {
        turtle.penup();
        turtle.goto(x + size/2, y + -size/2 + size/(2*num_lines) + i*size/num_lines);
        turtle.pendown();
        turtle.goto(x - size/2, y + -size/2 + size/(2*num_lines) + i*size/num_lines);
    }
}