Increasingly Wiggly Spirals

Making spirals more and more wiggly

Log in to post a comment.

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

const radmax = 38 //min=1 max=100 step=1
const size = 500 //min=0 max=1000 step=1
const spiral_1 = 100 //min=0 max=1000 step=1
const spiral_2 = 0 //min=0 max=10 step=.1

const noise_amp_start = 10 //min=-10 max=100 step=0.1
const noise_freq_inc = 1 //min=0 max=10 step=0.01
const noise_amp_inc = 1 //min=0 max=100 step=0.01
const noise_octives = 1 //min=0 max=10 step=0.1
const noise_shift = 0 //min=-5 max=5 step=0.1

const grid_x = 3 //min=1 max=20 step=1
const grid_y = 3 //min=1 max=20 step=1

const space_x = 66 //min=1 max=200 step=1
const space_y = 66 //min=1 max=200 step=1
const xstart = -66 //min=-200 max=200 step=1
const ystart = -66 //min=-200 max=200 step=1


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


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

let last_x = 0
let last_y = 0


class Noise {
	// http://mrl.nyu.edu/~perlin/noise/
	constructor(octaves = 1) {
		this.p = new Uint8Array(512);
		this.octaves = octaves;
		for (let i = 0; i < 512; ++i) {
			this.p[i] = Math.random() * 256;
		}
	}
	lerp(t, a, b) {
		return a + t * (b - a);
	}
	grad2d(i, x, y) {
		const v = (i & 1) === 0 ? x : y;
		return (i & 2) === 0 ? -v : v;
	}
	noise2d(x2d, y2d) {
		const X = Math.floor(x2d) & 255;
		const Y = Math.floor(y2d) & 255;
		const x = x2d - Math.floor(x2d);
		const y = y2d - Math.floor(y2d);
		const fx = (3 - 2 * x) * x * x;
		const fy = (3 - 2 * y) * y * y;
		const p0 = this.p[X] + Y;
		const p1 = this.p[X + 1] + Y;
		return this.lerp(
			fy,
			this.lerp(
				fx,
				this.grad2d(this.p[p0], x, y),
				this.grad2d(this.p[p1], x - 1, y)
			),
			this.lerp(
				fx,
				this.grad2d(this.p[p0 + 1], x, y - 1),
				this.grad2d(this.p[p1 + 1], x - 1, y - 1)
			)
		);
	}
	noise(x, y) {
		let e = 1,
			k = 1,
			s = 0;
		for (let i = 0; i < this.octaves; ++i) {
			e *= 0.5;
			s += e * (1 + this.noise2d(k * x, k * y)) / 2;
			k *= 2;
		}
		return s;
	}
}
 
let random_walk = 0


function walk(i) {
    let max_i = grid_x * grid_y - 1

    let x = i%grid_x
    let y = Math.floor(i/grid_x)

    xpos = x*space_x + xstart
    ypos = y*space_y + ystart
    dist_to_center = 125 - Math.sqrt((xpos)**2 + (ypos)**2)
    noise_freq = i * noise_freq_inc
    noise_amp = i * noise_amp_inc
    
    turtle.penup()
    spiral(xpos, ypos, noise_freq, noise_amp)


    return i<max_i
}

function spiral(xshift, yshift, noise_freq, noise_amp) {
    perlin = new Noise(noise_octives/10);
    for (let i = 0; i/10000 < Math.PI*radmax; ++i) { 
        let angle = i/10000
    
        let radius = angle*(spiral_1 + angle*spiral_2)/size
        //radius = 50
        if (i%5==0) {
            random_walk += Math.random()-0.5
        }
        radius += perlin.noise((radius+10)**2*noise_freq/10, yshift)*noise_amp - noise_shift
        let pen_x = radius * Math.cos(angle) + xshift
        let pen_y = radius * Math.sin(angle) + yshift
            

        //check for small moves
        
        if (fdiff_r(pen_x-last_x, pen_y-last_y) > .1) {
            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
                } 
            }
        turtle.pendown()

    
    }
    return
}