Not a good planet

Started by trying to draw a planet. Ended with this... thing.

Log in to post a comment.

// LL2021

Canvas.setpenopacity(1);

const phase = 0.8; // min=0 max=10 step=0.01
const rings = 100; // min=10 max=500 step=1
const precision = 2000; /// min=10 max=2000 step=1

const turtle = new Turtle();

const min_size = 0;
const min_factor = 0;
const max_size = 70;
const max_factor = 20; // min=0 max=50 step=0.1
const fbm_factor = 6; // min=1 max=30 step=0.1
const power = 20; // min=1 max=50 step=0.1

function walk(i, t) {
    if (i==0) turtle.jump(0, 0);
    
    const radius1 = min_size + (max_size - min_size) / (rings - 1) * i;
    const radius2 = min_size + (max_size - min_size) / (rings - 1) * (i+1);
    const factor1 = min_factor + (max_factor - min_factor) * Math.pow(i / (rings - 1), power);
    const factor2 = max_factor + (min_factor - max_factor) * Math.pow(i / (rings - 1), power);
    
    drawRing(radius1, radius2, factor1, factor2, t);
    
    return i < rings;
}

function drawRing(radius1, radius2, factor1, factor2, t) {
    const astep = 1/precision;

    for (var a = 0; a < 1 + astep/2; a += astep) {
        const angle = a * Math.PI * 2;
        const angle_t = t * Math.PI * 2;
        const height = getHeight(phase +  0.01 + angle + angle_t);
        const radius = radius1 + (radius2 - radius1) * a;
        const r = Math.max(radius / 2, radius + height * factor1 + height * factor2 * factor1);
        const x = r * Math.cos(angle);
        const y = r * Math.sin(angle);
        turtle.goto(x, y);
    }
}

function getHeight(a) {
    //return fbm(a * fbm_factor);
    return Math.max(0.05, Math.pow(fbm(a * fbm_factor), 2));
}

function fbm(x) {
    const octaves = 5;
	var v = 0;
	var a = 0.5;
	var shift = 100;
	for (var i = 0; i < octaves; i++) {
		v += a * noise(x);
		x = x * 2.0 + shift;
		a *= 0.5;
	}
	return v;
}

function noise(p) {
	const fl = Math.floor(p);
    const fc = p - Math.trunc(p);
    const r1 = rand(fl);
    const r2 = rand(fl + 1);
    return r1 + (r2 - r1) * fc;
}

function rand(n) {
    const s = Math.sin(n) * 43758.5453123;
    return s - Math.trunc(s);
}