console.clear();
Canvas.setpenopacity(1);
const turtle = new Turtle();
turtle.pendown();
/////////////////////////////////////////////////////
// Mat2
//
const identity = () => [
	1, 0, 
	0, 1, 
	0, 0
];

const rotate = (m, a) => {
	const rad = (Math.PI * a) / 180;
	const cos = Math.cos(rad);
	const sin = Math.sin(rad);
	return [
		cos * m[0] + sin * m[2],
		cos * m[1] + sin * m[3],
		cos * m[2] - sin * m[0],
		cos * m[3] - sin * m[1],
		m[4],
		m[5],
	];
};

const translate = (m, x, y = 0) => [
	m[0],
	m[1],
	m[2],
	m[3],
	m[4] + x * m[0] + y * m[2],
	m[5] + x * m[1] + y * m[3],
];

const scale = (m, x = 1, y = x) => [
	m[0] * x,
	m[1] * x,
	m[2] * y,
	m[3] * y,
	m[4],
	m[5],
];

const tooSmall = (m, minSize = 0.1) => {
	const x = m[0] * m[0] + m[1] * m[1];
	const y = m[2] * m[2] + m[3] * m[3];
	
	return x < minSize || y < minSize;
};

const transform = (m, p) => [
	m[0] * p[0] + m[2] * p[1] + m[4],
	m[1] * p[0] + m[3] * p[1] + m[5],
];

const project = (p, ox, oy, zoom) => [
	p[0] * zoom - ox, 
	p[1] * zoom - oy
];

///////////////////////////////////////////////////////

const minSize = 0.002;
const shapes = [];
const box = [100, 100, -100, -100];

const rect = (m) => {
	boundingBox(m, box);
	shapes.push(m);
};

const boundingBox = (m, box) => {
	const hx = 0.5 * (Math.abs(m[0]) + Math.abs(m[2]));

	const hy = 0.5 * (Math.abs(m[1]) + Math.abs(m[3]));

	box[0] = Math.min(box[0], m[4] - hx);
	box[1] = Math.min(box[1], m[5] - hy);
	box[2] = Math.max(box[2], m[4] + hx);
	box[3] = Math.max(box[3], m[5] + hy);
};

const draw = (m, ox, oy, zoom) => {
	turtle.jump(project(transform(m, [-0.5, -0.5]), ox, oy, zoom));
	turtle.goto(project(transform(m, [+0.5, -0.5]), ox, oy, zoom));
	turtle.goto(project(transform(m, [+0.5, +0.5]), ox, oy, zoom));
	turtle.goto(project(transform(m, [-0.5, +0.5]), ox, oy, zoom));
	turtle.goto(project(transform(m, [-0.5, -0.5]), ox, oy, zoom));
};

const autoScale = (w, h, margin = 0.9) => {
	const zoom = Math.min(
		(margin * w) / (box[2] - box[0]),
		(margin * h) / (box[3] - box[1]),
	);
	
	const ox = (box[0] + box[2]) * 0.5 * zoom;
	const oy = (box[3] + box[1]) * 0.5 * zoom;
	
	return [ox, oy, zoom];
};

const branch = (m) => {
	m = bit(m);
	if (Math.random() < 0.5) {
		left(rotate(m, -3));
	} else {
		right(rotate(m, 3));
	}
};

const left = (m) => {
	if (Math.random() * 5 < 4) {
		m = bit(m);
		left(
			rotate(m, 3)
		);
	} else {
		split(m);
	}
};

const right = (m) => {
	if (Math.random() * 5 < 4) {
		m = bit(m);
		right(
			rotate(m, -3)
		);
	} else {
		split(m);
	}
};

const split = (m) => {
	if (tooSmall(m, minSize)) return;
	m = bit(m);
	
	branch(
		rotate(
			scale(
				translate(m, -0.2, -0.5), 
				0.6
			),
		 -30
		)
	);
	
	branch(
		rotate(
			scale(
				translate(m, -0.2, 0.5), 
				0.6
			), 
			30
		)
	);
};

const pixel = (m, x, y) => rect(
	scale(
		translate(m, x, y), 
		0.16
	)
);

const bit = (m) => {
	if (Math.random() > 0.5) {
		pixel(m, 0, 0.4);
		pixel(m, 0, 0.4);
		pixel(m, 0, 0.2);
		pixel(m, 0, 0);
		pixel(m, 0, -0.2);
		pixel(m, 0, -0.4);
		pixel(m, -0.2, -0.2);
		return translate(m, 0.7);
	} else {
		pixel(m, 0.4, 0.2);
		pixel(m, 0.4, 0);
		pixel(m, 0.4, -0.2);
		pixel(m, -0.4, 0.2);
		pixel(m, -0.4, 0);
		pixel(m, -0.4, -0.2);
		pixel(m, 0.2, 0.4);
		pixel(m, 0, 0.4);
		pixel(m, -0.2, 0.4);
		pixel(m, 0.2, -0.4);
		pixel(m, 0, -0.4);
		pixel(m, -0.2, -0.4);
		return translate(m, 1.1);
	}
};

///////////////////////////////////////////////////////////

branch(rotate(identity(), -90));
const [ox, oy, zoom] = autoScale(200, 200, 0.95);

let shapeIndex = 0;
let t0 = performance.now();
const DT = 0.1; // ms

function walk(i) {
	if (shapeIndex >= shapes.length) return false;

	const t = performance.now();
	if (t < t0 + DT) return true;
	t0 = t;

	draw(shapes[shapeIndex++], ox, oy, zoom);
	return true;
}