Another Wave Machine

A wave thing that I do not really understand, but have made to do things based on the work of smarter people.

Log in to post a comment.

// Forked from "way wavy way" by ge1doot
// https://turtletoy.net/turtle/65cb465053

// 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 Randomize = 2; // min=1, max=100, step=1 


class Noise {
	// http://mrl.nyu.edu/~perlin/noise/
	constructor(octaves = Randomize) {
		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;
	}
}
const perlin = new Noise(3);


const Position = 80; //min=0, max=200, step=1
const Left = -101; //min=-101, max=0, step=1
const Right = 100; //min=0, max=100, step=1
const Lines = 120; //min=1, max=1000, step=1
const Spacing = 0.32;  //min=0, max=20 step=0.01
const WavyX = 0.01; //min=0.0001, max=0.2, step=0.0001
const WavyY = 0.01; //min=0.0001, max=0.2, step=0.0001

// The walk function will be called until it returns false.
turtle.up();
function walk(i) {
    for (let j = Left; j <= Right; j++) {
        
	    const h = perlin.noise(100 + j * WavyX, 100 + i * WavyY);
	    const height = Spacing * i + h * 100 - 150;
	    const Offset = Spacing * i
	    turtle.goto(j, height + Position + Offset);
	    turtle.down();
	}
    turtle.up();
    return i < Lines;
}