Bubbles

Exploring limits.

Log in to post a comment.

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

const maxCircleSize = 20;           //min = 5, max = 80, step = 1
const height = 90;                  //min = 10, max = 100, step = 1
const width = 45;                   //min = 10, max = 100, step = 1
let depth = 10;                     //min = 1, max = 10, step = 0.5
depth = 2**(depth * 1.5);

const traceAll = 1;                 ////min = 0, max = 1, step = 1, (no, yes)

// Global code will be evaluated once.
const turtle = new Turtle();

let circles = [];

let cancelled = 0;

class Circle{
    constructor(x, y, r, trace = 1){
        this.x = x;
        this.y = y;
        this.r = r;
        
        for(let circle of circles){
            let deltaX = x - circle.x;
            let deltaY = y - circle.y;
            
            let delta = Math.sqrt(deltaX**2 + deltaY**2);
            // first test if this new circle will be in one already existing.
            if(delta < circle.r){
/*
                console.log("circle inside");
                while(this.r > 0.1){
                    turtle.jump(this.x+this.r, this.y);
                    turtle.seth(90);
                    turtle.circle(this.r);
                    this.r -= 0.1;
                }
*/              
                cancelled++;
                return;
            }
            // then test if the circles are overlaping

            if(delta < (circle.r + this.r)){
                this.r = delta - circle.r;
                if(this.r < 0.5){
                    cancelled++;
                    return;
                }
            }
        }
        
        if(trace){
            turtle.jump(this.x+this.r, this.y);
            turtle.seth(90);
            turtle.circle(this.r);
        }
        
        cancelled = 0;
        
        circles.push(this);
    }
}

// The walk function will be called until it returns false.
function walk(i) {
    
    let r = Math.random() * maxCircleSize;
    if(r < 0.3) r = 0.5;
    let trace = 1;
    if(!traceAll && r > (maxCircleSize / 3)) trace = 0;

    let x = Math.random() * 2 * (width - r) - width + r;
    let y = Math.random() * 2 * (height - r) - height + r;
    
    
    new Circle(x, y, r, trace);
    
    if(cancelled > depth){
//        console.log("run ", i, circles.length, " bubbles !");
        return false;
    }
  
    
    return true;
}