// Simplex Noise. Created by Reinder Nijhoff 2020 - @reindernijhoff // The MIT License // // https://turtletoy.net/turtle/6e4e06d42e // const turtle = new Turtle(); const frequency = 1.5; //min=.1, max=10, step=.01 const gridX = 500; // min=10, max=1000, step=1 const gridY = 150; // min=10, max=400, step=1 const seed = 0; // min=1, max=100, step=1 const waveSize = 16; // min=0, max=60, step=.1 const fieldSize = 190; // min=100, max=200, step=1 const noise = new SimplexNoise(seed); const minHeights = []; for (let x=0; x<gridX; x++) { minHeights[x] = 100; } function walk(i) { const gx = (i % gridX); const gy = (i / gridX) | 0; const x = gx*fieldSize/(gridX-1) - fieldSize/2; const y = (gridY - gy)/gridY*fieldSize-fieldSize/2; let r = FBM(x*frequency/fieldSize, y*frequency/fieldSize); r *= waveSize; const h = Math.min(y, y + r, minHeights[gx]); minHeights[gx] = h; if (gy != (((i-1) / gridX)|0) || i < 2) turtle.up(); else turtle.down(); if (h == y) { if ((i/gridX|0) % 4 === 0) turtle.goto(x , Math.sin(x+y/5)/5 + h); else turtle.jump(x , h); } else { turtle.goto(x, h); } return i < gridX*gridY-1; } function FBM(x, y) { let f = 1.; var fbm = noise.noise2D([x * f, y * f]); f *= 2; x += 32; fbm += noise.noise2D([x * f, y * f]) * 0.5; f *= 2; x += 42; fbm += noise.noise2D([x * f,y * f]) * 0.25; f *= 2; x += 9973; fbm += noise.noise2D([x * f,y * f]) * 0.125; f *= 2; x += 824; fbm += noise.noise2D([x * f,y * f]) * 0.065; return fbm; } //////////////////////////////////////////////////////////////// // Simplex Noise utility code. Created by Reinder Nijhoff 2020 // https://turtletoy.net/turtle/6e4e06d42e // Based on: http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf //////////////////////////////////////////////////////////////// function SimplexNoise(seed = 1) { const grad = [ [1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0], [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1], [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1] ]; const perm = new Uint8Array(512); const F2 = (Math.sqrt(3) - 1) / 2, F3 = 1/3; const G2 = (3 - Math.sqrt(3)) / 6, G3 = 1/6; const dot2 = (a, b) => a[0] * b[0] + a[1] * b[1]; const sub2 = (a, b) => [a[0] - b[0], a[1] - b[1]]; const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; const sub3 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]]; class SimplexNoise { constructor(seed = 1) { for (let i = 0; i < 512; i++) { perm[i] = i & 255; } for (let i = 0; i < 255; i++) { const r = (seed = this.hash(i+seed)) % (256 - i) + i; const swp = perm[i]; perm[i + 256] = perm[i] = perm[r]; perm[r + 256] = perm[r] = swp; } } noise2D(p) { const s = dot2(p, [F2, F2]); const c = [Math.floor(p[0] + s), Math.floor(p[1] + s)]; const i = c[0] & 255, j = c[1] & 255; const t = dot2(c, [G2, G2]); const p0 = sub2(p, sub2(c, [t, t])); const o = p0[0] > p0[1] ? [1, 0] : [0, 1]; const p1 = sub2(sub2(p0, o), [-G2, -G2]); const p2 = sub2(p0, [1-2*G2, 1-2*G2]); let n = Math.max(0, 0.5-dot2(p0, p0))**4 * dot2(grad[perm[i+perm[j]] % 12], p0); n += Math.max(0, 0.5-dot2(p1, p1))**4 * dot2(grad[perm[i+o[0]+perm[j+o[1]]] % 12], p1); n += Math.max(0, 0.5-dot2(p2, p2))**4 * dot2(grad[perm[i+1+perm[j+1]] % 12], p2); return 70 * n; } noise3D(p) { const s = dot3(p, [F3, F3, F3]); const c = [Math.floor(p[0] + s), Math.floor(p[1] + s), Math.floor(p[2] + s)]; const i = c[0] & 255, j = c[1] & 255, k = c[2] & 255; const t = dot3(c, [G3, G3, G3]); const p0 = sub3(p, sub3(c, [t, t, t])); const [o0, o1] = p0[0] >= p0[1] ? p0[1] >= p0[2] ? [ [1, 0, 0], [1, 1, 0] ] : p0[0] >= p0[2] ? [ [1, 0, 0], [1, 0, 1] ] : [ [0, 0, 1], [1, 0, 1] ] : p0[1] < p0[2] ? [ [0, 0, 1], [0, 1, 1] ] : p0[0] < p0[2] ? [ [0, 1, 0], [0, 1, 1] ] : [ [0, 1, 0], [1, 1, 0] ]; const p1 = sub3(sub3(p0, o0), [-G3, -G3, -G3]); const p2 = sub3(sub3(p0, o1), [-2*G3, -2*G3, -2*G3]); const p3 = sub3(p0, [1-3*G3, 1-3*G3, 1-3*G3]); let n = Math.max(0, 0.6-dot3(p0, p0))**4 * dot3(grad[perm[i+perm[j+perm[k]]] % 12], p0); n += Math.max(0, 0.6-dot3(p1, p1))**4 * dot3(grad[perm[i+o0[0]+perm[j+o0[1]+perm[k+o0[2]]]] % 12], p1); n += Math.max(0, 0.6-dot3(p2, p2))**4 * dot3(grad[perm[i+o1[0]+perm[j+o1[1]+perm[k+o1[2]]]] % 12], p2); n += Math.max(0, 0.6-dot3(p3, p3))**4 * dot3(grad[perm[i+1+perm[j+1+perm[k+1]]] % 12], p3); return 32 * n; } hash(i) { i = 1103515245 * ((i >> 1) ^ i); const h32 = 1103515245 * (i ^ (i>>3)); return h32 ^ (h32 >> 16); } } return new SimplexNoise(seed); }