my phenomenological perspective

codepen.io/ge1doot/full/VQNapo

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(0.5);
// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
/////////////////////////////////////////////////////
const Mat2D = class {
    constructor (m) { this.m = m; }
    rotate (v) {
    	const rad = Math.PI * v / 180;
    	const cos = Math.cos(rad);
    	const sin = Math.sin(rad);
    	return new Mat2D([
    		cos * this.m[0] + sin * this.m[2],
    		cos * this.m[1] + sin * this.m[3],
    		cos * this.m[2] - sin * this.m[0],
    		cos * this.m[3] - sin * this.m[1],
    		this.m[4],
    		this.m[5]
    	]);
    } 
    translate (x, y = 0) {
    	return new Mat2D([
    		this.m[0],
    		this.m[1],
    		this.m[2],
    		this.m[3],
    		this.m[4] + x * this.m[0] + y * this.m[2],
    		this.m[5] + x * this.m[1] + y * this.m[3]
    	]);
    }
    scale (x = 1, y = x) {
        return new Mat2D([
    		this.m[0] * x,
    		this.m[1] * x,
    		this.m[2] * y,
    		this.m[3] * y,
    		this.m[4],
    		this.m[5]
    	]);
	}
	size () {
        const x = this.m[0] * this.m[0] + this.m[1] * this.m[1];
        const y = this.m[2] * this.m[2] + this.m[3] * this.m[3];
        return Math.sqrt(x * x + y * y);
	}
    transform (x, y) {
		const m0 = this.m[0] * zoom;
		const m1 = this.m[1] * zoom;
		const m2 = this.m[2] * zoom;
		const m3 = this.m[3] * zoom;
		const m4 = this.m[4] * zoom - ox;
		const m5 = this.m[5] * zoom - oy;
		return [
            m0 * x + m2 * y + m4, 
            m1 * x + m3 * y + m5
		];
	}
	boundingBox (box) {
        const p0 = this.transform(-0.5, 0);
        const p1 = this.transform(0.5, 0);
        const minx = Math.min(p0[0], p1[0]);
        const maxx = Math.max(p0[0], p1[0]);
        const miny = Math.min(p0[1], p1[1]);
        const maxy = Math.max(p0[1], p1[1]);
    	if (minx < box[0]) box[0] = minx; else if (maxx > box[2]) box[2] = maxx;
    	if (miny < box[1]) box[1] = miny; else if (maxy > box[3]) box[3] = maxy;
    }
};
///////////////////////////////////////////////////////
const minSize = 0.001;
const shapes =  [];
let zoom = 1, ox = 0, oy = 0;
const box = [100, 100, -100, -100];
const rect = m => {
	m.boundingBox(box);
	shapes.push(m);
};
const draw = m => {
	turtle.goto(m.transform(-0.5, -0.12));
	turtle.down();
	turtle.goto(m.transform(-0.5, 0));
	turtle.goto(m.transform( 0.5, 0));
	turtle.goto(m.transform( 0.5, -0.12));
	turtle.up();
};
const scale = (w, h, margin = 0.9) => {
	zoom = Math.min(
		margin * w / (box[2] - box[0]),
		margin * h / (box[3] - box[1])
	);
	ox = (box[0] + box[2]) * 0.5 * zoom;
	oy = (box[3] + box[1]) * 0.5 * zoom;
};
////////////////////////////////////////////////////
const branch = m => {
    if (m.size() < minSize) return;
    const r = Math.random() * 50;
    let weight = 0;
    switch (true) {
        case r <= (weight += 0.2):
            branch(m);
            branch(m.scale(-1, 1));
            break;
        case r <= (weight += 0.2):
            branch(m.scale(-1, 1));
            break;
        default:
            rect(m.scale(0.8));
            return branch(m.translate(0, 0.1).rotate(3.4).scale(0.994));
    }
}
/////////////////// render scene //////////////////////////////
branch(new Mat2D([1, 0, 0, -1, 0, 0]));
branch(new Mat2D([1, 0, 0, -1, 0, 0]).translate(0, -0.1).rotate(180));
scale(200, 200, 0.95);

// The walk function will be called until it returns false.
function walk(i) {
	const m = shapes.pop();
	if (!m) return false;
	draw(m);
	return true;
}