Sunflower shrinking

Fork of: Sunflower seed distribution
Essentially just reversing the growth in radius to shrinking instead.

Log in to post a comment.

// Forked from "Sunflower seed distribution" by Jurgen
// https://turtletoy.net/turtle/1631e17b11

let r = 1.5; //min=.5, max=4, step=.1
let margin = .4; //min=0, max=1, step=.1
let maxR = 90; //min=20, max=150, step=10
let shrinkf = 1.4; //min=0, max=5, step=.1
let maxSR = 16; //min=1, max=20, step=.5
let opacity = .8; //min=.1, max=1, step=.1
let fillCircles = 1; //min=0, max=1, step=1

Canvas.setpenopacity(.5);

const turtle = new Turtle();
turtle.setheading(90);

var gr = (1+Math.sqrt(5)) / 2;
var grRot = (gr - 1) * 2 * Math.PI;

var circles = [];
var rotation = 0;

function drawCircle(x, y, r) {
    for(var i = (fillCircles == 1? 0: r); i <= r; i+=.2) {
        turtle.jump(x + i, y);
        turtle.circle(i);
    }
    circles.push([x, y, r]);
}

function hasIntersections(x, y, r) {
    for(var i = 0; i < circles.length; i++) {
        if(Math.pow(x - circles[i][0], 2) + Math.pow(y - circles[i][1], 2) < Math.pow(r + circles[i][2] + margin, 2)) {
            return true;
        }
    }
    return false;
}

function walk(i) {
    rotation += grRot;
    
    for(var testR = 0; testR < maxR; testR += 0.1) {
        var x = testR * Math.cos(rotation);
        var y = testR * Math.sin(rotation);
        
        var useR = Math.min(maxSR, r - (shrinkf * testR / 100))
        
        if(!hasIntersections(x, y, useR, circles)) {
            drawCircle(x, y, useR);
            return true;
        }
    }
    
    return false;
}