Squares and Circles

Adapted from a CFDG code
https://www.contextfreeart.org/phpbb/viewtopic.php?f=3&t=116
Squares and Circles by signal ยป Thu Jul 07, 2005 7:46 pm

Created by ge1doot on 2019/1/26
60
0

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(0.8);

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
const polygons = Polygons();
//////////////////////////////////////////////////
//
//        /~\
//       C oo
//       _( ^)
//      /   ~\
Array.prototype.rotate = function(v) {
	const rad = Math.PI * v / 180;
	const cos = Math.cos(rad);
	const sin = Math.sin(rad);
	return [
		cos * this[0] + sin * this[2],
		cos * this[1] + sin * this[3],
		cos * this[2] - sin * this[0],
		cos * this[3] - sin * this[1],
		this[4],
		this[5]
	];
};
Array.prototype.translate = function(x, y = 0) {
	return [
		this[0],
		this[1],
		this[2],
		this[3],
		this[4] + x * this[0] + y * this[2],
		this[5] + x * this[1] + y * this[3]
	];
};
Array.prototype.scale = function(x = 1, y = x) {
	return [this[0] * x, this[1] * x, this[2] * y, this[3] * y, this[4], this[5]];
};
Array.prototype.isTooSmall = function(m) {
	const x = this[0] * this[0] + this[1] * this[1];
	const y = this[2] * this[2] + this[3] * this[3];
	return x < m || y < m;
};
////////////////////////////////////////////////
const contextFree = {
	init() {
		this.box = [100, 100, -100, -100];
		this.shapes = [];
	},
	push(m) {
        if (m[4] - 0.5 * m[0] < this.box[0]) this.box[0] = m[4] - 0.5 * m[0];
		else if (m[4] + 0.5 * m[0] > this.box[2]) this.box[2] = m[4] + 0.5 * m[0];
		if (m[5] + 0.5 * m[3] < this.box[1]) this.box[1] = m[5] + 0.5 * m[3];
		else if (m[5] - 0.5 * m[3] > this.box[3]) this.box[3] = m[5] - 0.5 * m[3];
		this.shapes.unshift(m);
	},
	SQUARE(m, a = 0, s = 0, outline = 1) {
		m[6] = 0;
		m[7] = a;
		m[8] = s;
		m[9] = outline;
		this.push(m);
	},
	CIRCLE(m, a = 0, s = 0, outline = 1) {
		m[6] = 1;
		m[7] = a;
		m[8] = s;
		m[9] = outline;
		this.push(m);
	},
	transform(x, y, m) {
		const m0 = m[0] * this.zoom;
		const m1 = m[1] * this.zoom;
		const m2 = m[2] * this.zoom;
		const m3 = m[3] * this.zoom;
		const m4 = m[4] * this.zoom - this.ox;
		const m5 = m[5] * this.zoom - this.oy;
		return [
            m0 * x + m2 * y + m4, 
            m1 * x + m3 * y + m5
		];
	},
	scale(margin = 0.95) {
		this.zoom = Math.min(
			margin * 200 / (this.box[2] - this.box[0]),
			margin * 200 / (this.box[3] - this.box[1])
		);
		this.ox = (this.box[0] + this.box[2]) * 0.5 * this.zoom;
		this.oy = (this.box[3] + this.box[1]) * 0.5 * this.zoom;
	},
	draw() {
		if (!this.shapes.length) return false;
		let p;
		const m = this.shapes.pop();
		switch (m[6]) {
			case 0:
				// SQUARE
				p = polygons.create();
				const p0 = this.transform(-0.5, -0.5, m);
				const p1 = this.transform(+0.5, -0.5, m);
				const p2 = this.transform(+0.5, +0.5, m);
				const p3 = this.transform(-0.5, +0.5, m);
				p.addPoints(p0, p1, p2, p3);
				if (m[9] === 1) {
					p.addSegments(p0, p1, p1, p2, p2, p3, p3, p0);
				}
				if (m[8] !== 0) p.addHatching(m[7], m[8]);
				polygons.draw(turtle, p);
				break;
			case 1:
				// CIRCLE
				p = polygons.create();
				p.addPoints(this.transform(0.5, 0, m));
				for (let a = Math.PI / 36; a <= 2 * Math.PI; a += Math.PI / 36) {
					p.addPoints(this.transform(Math.cos(a) * 0.5, -Math.sin(a) * 0.5, m));
				}
				if (m[9] === 1) p.addOutline(0);
				if (m[8] !== 0) p.addHatching(m[7], m[8]);
				polygons.draw(turtle, p);
				break;
		}
		return true;
	},
	////////////////////////// context Free //////////////////////////
	SQUARES(m) {
		if (m.isTooSmall(0.005)) return;
		const r = Math.random() * 1.5;
		switch (true) {
			case r <= 0.1:
				this.SQUARES(
					m
						.translate(1, 0)
						.rotate(45)
						.scale(0.5)
				);
				this.SQUARESORHALT(m);
				break;
			case r <= 0.2:
				this.SQUARES(
					m
						.translate(1, 0)
						.rotate(-90)
						.scale(0.9)
				);
				this.SQUARESORHALT(m);
				break;
			case r <= 0.5:
				this.SQUARES(
					m
						.translate(1, 0)
						.rotate(90)
						.scale(0.5)
				);
				this.SQUARESORHALT(m);
				break;
			default:
				this.SQUARE(m);
				this.SQUARE(m.translate(0, 0.2), -Math.PI / 4, 0.5, 0);
				return this.SQUARES(m.translate(0.9));
		}
	},
	SQUARESORHALT(m) {
		const r = Math.random() * 1.9;
		switch (true) {
			case r <= 1:
				return this.SQUARES(m);
			default:
				this.CIRCLE(m.translate(0, 0), 0, 0.1, 0);
				this.CIRCLE(m.translate(0, 0.15), -Math.PI / 4, 0.5, 0);
		}
	}
};
/////////////////////////////////////////////////////////////////////////////
const m = [1, 0, 0, 1, 0, 0];
do {
	contextFree.init();
	contextFree.SQUARES(m);
} while (contextFree.shapes.length < 500);
contextFree.scale();

// The walk function will be called until it returns false.
function walk(i) {
	return contextFree.draw();
}


////////////////////////////////////////////////////////////////
// reinder's occlusion code parts from "Cubic space division #2"
// Optimizations and code clean-up by ge1doot
////////////////////////////////////////////////////////////////

function Polygons() {
	const polygonList = [];
	const Polygon = class {
		constructor() {
			this.cp = [];       // clip path: array of [x,y] pairs
			this.dp = [];       // 2d line to draw
			this.aabb = [];     // AABB bounding box
		}
		addPoints(...points) {
		    for (let i = 0; i < points.length; i++) this.cp.push(points[i]);
		    this.aabb = this.AABB();
		}
		addSegments(...points) {
		    for (let i = 0; i < points.length; i++) this.dp.push(points[i]);
		}
		addOutline(s = 0) {
			for (let i = s, l = this.cp.length; i < l; i++) {
				this.dp.push(this.cp[i], this.cp[(i + 1) % l]);
			}
		}
		draw(t) {
			if (this.dp.length === 0) return;
			for (let i = 0, l = this.dp.length; i < l; i+=2) {
				const d0 = this.dp[i];
				const d1 = this.dp[i + 1];
					t.penup();
					t.goto(d0);
					t.pendown();
					t.goto(d1);
			}
		}
		AABB() {
			let xmin = 2000;
			let xmax = -2000;
			let ymin = 2000;
			let ymax = -2000;
			for (let i = 0, l = this.cp.length; i < l; i++) {
				const x = this.cp[i][0];
				const y = this.cp[i][1];
				if (x < xmin) xmin = x;
				if (x > xmax) xmax = x;
				if (y < ymin) ymin = y;
				if (y > ymax) ymax = y;
			}
			// Bounding box: center x, center y, half w, half h
			return [
				(xmin + xmax) * 0.5,
				(ymin + ymax) * 0.5,
				(xmax - xmin) * 0.5,
				(ymax - ymin) * 0.5
			];
		}
		addHatching(a, d) {
			const tp = new Polygon();
			tp.cp.push(
			    [this.aabb[0] - this.aabb[2], this.aabb[1] - this.aabb[3]],
			    [this.aabb[0] + this.aabb[2], this.aabb[1] - this.aabb[3]],
			    [this.aabb[0] + this.aabb[2], this.aabb[1] + this.aabb[3]],
			    [this.aabb[0] - this.aabb[2], this.aabb[1] + this.aabb[3]]
			);
			const dx = Math.sin(a) * d, dy = Math.cos(a) * d;
			const cx = Math.sin(a) * 200, cy = Math.cos(a) * 200;
			for (let i = 0.5; i < 150 / d; i++) {
				tp.dp.push([dx * i + cy, dy * i - cx], [dx * i - cy, dy * i + cx]);
				tp.dp.push([-dx * i + cy, -dy * i - cx], [-dx * i - cy, -dy * i + cx]);
			}
			tp.boolean(this, false);
			for (let i = 0, l = tp.dp.length; i < l; i++) this.dp.push(tp.dp[i]);
		}
		inside(p) {
			// find number of i ntersection points from p to far away
			// if even your outside
			const p1 = [0.1, -1000];
			let int = 0;
			for (let i = 0, l = this.cp.length; i < l; i++) {
				if (
					this.vec2_find_segment_intersect(
						p,
						p1,
						this.cp[i],
						this.cp[(i + 1) % l]
					) !== false
				) {
					int++;
				}
			}
			return int & 1;
		}
		boolean(p, diff = true) {
			// polygon diff algorithm (narrow phase)
			const ndp = [];
			for (let i = 0, l = this.dp.length; i < l; i+=2) {
				const ls0 = this.dp[i];
				const ls1 = this.dp[i + 1];
				// find all intersections with clip path
				const int = [];
				for (let j = 0, cl = p.cp.length; j < cl; j++) {
					const pint = this.vec2_find_segment_intersect(
						ls0,
						ls1,
						p.cp[j],
						p.cp[(j + 1) % cl]
					);
					if (pint !== false) {
						int.push(pint);
					}
				}
				if (int.length === 0) {
					// 0 intersections, inside or outside?
					if (diff === !p.inside(ls0)) {
						ndp.push(ls0, ls1);
					}
				} else {
					int.push(ls0, ls1);
					// order intersection points on line ls.p1 to ls.p2
					const cmpx = ls1[0] - ls0[0];
					const cmpy = ls1[1] - ls0[1];
					for (let i = 0, len = int.length; i < len; i++) {
					    let j = i;
					    const item = int[j];
						for (
							const db = (item[0] - ls0[0]) * cmpx + (item[1] - ls0[1]) * cmpy;
							j > 0 && (int[j - 1][0] - ls0[0]) * cmpx + (int[j - 1][1] - ls0[1]) * cmpy < db;
							j--
						) int[j] = int[j - 1];
						int[j] = item;
					}
					for (let j = 0; j < int.length - 1; j++) {
						if (
							(int[j][0] - int[j + 1][0]) ** 2 + (int[j][1] - int[j + 1][1]) ** 2 >= 0.01
						) {
							if (
								diff ===
								!p.inside([
									(int[j][0] + int[j + 1][0]) / 2,
									(int[j][1] + int[j + 1][1]) / 2
								])
							) {
								ndp.push(int[j], int[j + 1]);
							}
						}
					}
				}
			}
			this.dp = ndp;
			return this.dp.length > 0;
		}
		//port of http://paulbourke.net/geometry/pointlineplane/Helpers.cs
		vec2_find_segment_intersect(l1p1, l1p2, l2p1, l2p2) {
			const d =
				(l2p2[1] - l2p1[1]) * (l1p2[0] - l1p1[0]) -
				(l2p2[0] - l2p1[0]) * (l1p2[1] - l1p1[1]);
			if (d === 0) return false;
			const n_a =
				(l2p2[0] - l2p1[0]) * (l1p1[1] - l2p1[1]) -
				(l2p2[1] - l2p1[1]) * (l1p1[0] - l2p1[0]);
			const n_b =
				(l1p2[0] - l1p1[0]) * (l1p1[1] - l2p1[1]) -
				(l1p2[1] - l1p1[1]) * (l1p1[0] - l2p1[0]);
			const ua = n_a / d;
			const ub = n_b / d;
			if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
				return [
					l1p1[0] + ua * (l1p2[0] - l1p1[0]),
					l1p1[1] + ua * (l1p2[1] - l1p1[1])
				];
			}
			return false;
		}
	};
	return {
		create() {
			return new Polygon();
		},
		draw(turtle, p) {
			let vis = true;
			for (let j = 0; j < polygonList.length; j++) {
				const p1 = polygonList[j];
				// AABB overlapping test - still O(N2) but very fast
				if (
					Math.abs(p1.aabb[0] - p.aabb[0]) - (p.aabb[2] + p1.aabb[2]) < 0 &&
					Math.abs(p1.aabb[1] - p.aabb[1]) - (p.aabb[3] + p1.aabb[3]) < 0
				) {
					if (p.boolean(p1) === false) {
						vis = false;
						break;
					}
				}
			}
			if (vis) {
				p.draw(turtle);
				polygonList.push(p);
			}
		}
	};
}