Ball?

Generate a 3D sphere-like form using just arc&rectangle drawing.

Log in to post a comment.

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

// constant variables
const circle_radius = 12;
const square_size = 20;

const radius_levels = [0.9,.8,.7,.6,.5,.4,.3,.2,.1];
const inv_levels = 1.0/radius_levels;
const canvas_size = 256;
const offset_x = 7;
const offset_y = 8;
// Global code will be evaluated once.
const turtle = new Turtle();

function line(x1,y1,x2,y2){turtle.penup();turtle.goto(x1,y1);turtle.pendown();turtle.goto(x2,y2);}
function quad(left,right,top,bottom){line(left,top,left,bottom);line(left,bottom,right,bottom);line(right,bottom,right,top);line(right,top,left,top);}
function square(x,y,size){let sz = size*0.5;quad(x-sz,x+sz,y-sz,y+sz);}
function circle(x,y,radius,extent=undefined){turtle.penup();turtle.goto(x,y);turtle.pendown();turtle.circle(radius,extent);}
function InCircle(x,y,cx,cy,radius){let dx = x - cx;let dy = y - cy;return dx*dx + dy*dy <= radius*radius;}

function Halton(index, base){let result = 0;let invBase = 1.0 / base;let frac = invBase;while(index>0){result += (index%base)*frac;index /= base;frac *= invBase;}return result;}
function Halton2D(index,base1,base2,range){let HaltonX = Halton(index,base1)-0.5;let HaltonY = Halton(index,base2)-0.5;let x = HaltonX * range;let y = HaltonY * range;return [x,y];}

// The walk function will be called until it returns false.
function walk(i) {
    let A = Halton2D(i,2,3,canvas_size);
    let x = A[0], y = A[1];
    let inCircle = false;
    
    for(let k=0;k<radius_levels.length;k++)
    {
        if(InCircle(x,y,-offset_x,-offset_y,radius_levels[k]*canvas_size*0.2))
        {
            let scale = k / radius_levels.length;
            for(let m = 0;m<k;m++)
            {
                circle(x,y,circle_radius*Math.random()*scale,Math.random()*180.0);
                circle(x,y,circle_radius*Math.random()*scale,Math.random()*90.0);
                circle(x,y,circle_radius*Math.random()*scale,Math.random()*45.0);
            }
          
            inCircle = true;
        }
    }
    
    if(!inCircle)
    {
        square(x+offset_x,y+offset_y,square_size);
    }
    
    return i < 10000;
}