Spiral with bumps

bumpy spiral!

My first published turtle :D Thanks for the amazing tool and the fun community, I really have been enjoying making these.

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 n_phases = 5 //min=1 max=6 step=1
const growth_step = 0 //min=0 max=10 step=0.1

const linewidth = 25 //min=0.1 max=50 step=0.1

const k = 10; //min=0 max=20 step=0.1
const radstart = 0 //min=0 max=100 step=1
const radmax = 38 //min=1 max=100 step=1
const radmult = 1 //min=1 max=6.28 step=0.01
const anglestep = 0.01

const str_lin = 1 //min=0 max=10 step=.1
const rep_str = 30 // min=-100 max=100 step = 1

const past_str = 69 // min=0 max=200 step = 1
const future_str = 69 //min=0 max=200 step=1

const rep_n = 100 //min=0 max=200 step=1
const rep_width = 5.4 //min=0 max=50 step=0.1
const time_decay = 10 //min=1 max=50 step=0.1

const center_x = 0 //min=-100 max=100 step=1
const center_y = 0 //min=-100 max=100 step=1

const total_scale_d = 2.5 //min=0 max=10.0 step=0.1
const total_scale_n = 1.0 //min=0 max=10.0 step=0.1

function fdiff_r(diffx, diffy) {
    return Math.sqrt(diffx**2 + diffy**2)
}

function f_exp(value) {
    return Math.E**(-1*value**2)
}

function walk(i) {
    one_spiral(i*growth_step,i*(360/n_phases))
    i += 1
    return i<n_phases
}

// The walk function will be called until it returns false.
function one_spiral(growth, phasing) {
    turtle.penup()
    let last_x=0
    let last_y=0
    
    let anglemax = (radmax*360)+anglestep

    let randlocs = new Array(rep_n)
    for (let rep_i=0; rep_i<rep_n; rep_i+=1) {
        randlocs[rep_i] = Math.random()*(anglemax-radstart*360) + radstart*360
    }

    for (let angle=radstart*360; angle<=anglemax; angle+=anglestep) {

        let radius = (1 + growth/100)*((linewidth*((angle+phasing)/360))**radmult)/k
        let theta = (2*angle)*(Math.PI/360)
        let pen_x = radius * Math.sin(theta) + center_x ;
        let pen_y = radius * Math.cos(theta) + center_y;
        
        let forces_x = 0
        let forces_y = 0
        
        for (let rep_i=0; rep_i<rep_n; rep_i+=1) {
            let angle_diff = Math.abs(angle - randlocs[rep_i])
            
            let force = 0
            let magshift = angle_diff/rep_width
            
            force = f_exp(magshift) * rep_str


            let nshift = (((angle_diff-180)%360) - 180)/rep_width
            let time_force =  f_exp(nshift) * f_exp(angle_diff*time_decay/anglemax)
            
            if (angle < randlocs[rep_i]) {
                force += time_force * future_str
            }
            else {
                force -= time_force  * past_str            
            }
            
            force *= (angle/anglemax)*str_lin
            
            pen_x -= force * Math.sin(theta) 
            pen_y -= force * Math.cos(theta)
        }
        
        //total scale
        pen_x *= (total_scale_n/total_scale_d)
        pen_y *= (total_scale_n/total_scale_d)        
        

        //check for small moves
        if (fdiff_r(pen_x-last_x, pen_y-last_y) > 0.2) {
            if ((Math.abs(pen_x)-100 < 0) & (Math.abs(pen_y)-100 < 0)) {
                turtle.goto(pen_x, pen_y); }
            turtle.pendown()
            last_x=pen_x
            last_y=pen_y
        }
        
    }

}