Clockwork-like

For automated patent-filing

```Canvas.setpenopacity(.25);

const polygons = Polygons();
const turtle = new Turtle();

let numGearSets = 10
let teethExtrude = 4
let teethPerCircumference = 0.15

let gears = []

function init() {
let prevPos = [0, 0]

for (let i = 0; i < numGearSets; i++) {
let numTeeth = 20 + Math.floor(20 * Math.random());
let hatchAngle = 45 + Math.random() * 20
let hatchAmount = 0.25 * (1 + (i % 2 == 0 ? 1 : 4) * i/numGearSets)

let pos = [0, 0]
if (i > 0) {
let direction = normalize([Math.random() * 2 - 1, Math.random() * 2 - 1])
let smallNumTeeth =  7 + Math.floor(6 * Math.random());

let distance = (smallNumTeeth + numTeeth) / (2 * Math.PI * teethPerCircumference) + teethExtrude + 2
pos = [prevPos[0] + distance * direction[0], prevPos[1] + distance * direction[1]]

let angle = Math.atan2(direction[1], direction[0])
gears.push(new Gear(smallNumTeeth, prevPos, angle - 2 * Math.PI / smallNumTeeth / 4, hatchAmount, hatchAngle - 90))
gears.push(new Gear(numTeeth, pos, angle + Math.PI, hatchAmount, hatchAngle))
}
else {
gears.push(new Gear(numTeeth, pos, 0, hatchAmount, hatchAngle))
}

prevPos = pos
}
}

function walk(i) {
if (i == 0) init();
let gear = gears[gears.length - 1 - i]
gear.draw()

return i < gears.length - 1
}

class Gear {
// TODO: rotation is in radians while hatch angle is in degrees
constructor(numTeeth, pos, rotation, hatchDensity = -1, hatchAngle = 0) {
this.numTeeth = numTeeth
this.radius = numTeeth / (2 * Math.PI * teethPerCircumference) // 2 * radius * Math.PI * teethPerCircumference
this.pos = pos
this.rotation = rotation
this.hatchDensity = hatchDensity
this.hatchAngle = hatchAngle
}

draw() {
this.drawPoints(this.getToothPoints(this.numTeeth, this.radius, this.rotation, teethExtrude), this.hatchAngle, this.hatchDensity)
}

drawPoints(points, hatchAngle = 0, hatchDensity = 0) {
const p = polygons.create()
if (hatchDensity != 0) {
}
polygons.draw(turtle, p)
}

getToothPoints() {
return [...Array(this.numTeeth).keys()].map((a) => {
return [...Array(5).keys()].map((b) => {
let angle = 2 * Math.PI * (a + (b/4)) / this.numTeeth + this.rotation
let extr = (b == 1 || b == 2) ? teethExtrude : 0
return [(this.radius + extr) * Math.cos(angle) + this.pos[0], (this.radius + extr) * Math.sin(angle) + this.pos[1]]
})
}).flat(1)
}

getAxisPoints() {
return [...Array(10).keys()].map((a) => {
let angle = 2 * Math.PI * a/10
})
}
}

// some utils

const deg2rad = Math.PI * 2 / 360

function normalize(a) {
let mag = Math.sqrt(a[0] * a[0] + a[1] * a[1])
return [a[0]/ mag, a[1] / mag]
}

let dotTurtle = new Turtle()
function drawDot(pos, radius = 1) {
dotTurtle.penup()
dotTurtle.pendown()
for (let i = 0; i < 10; i++) dotTurtle.circle(radius)
}

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

function Polygons() {
const polygonList = [];
const linesDrawn = [];
const Polygon = class {
constructor() {
this.cp = [];       // clip path: array of [x,y] pairs
this.dp = [];       // 2d line to draw
this.aabb = [];     // AABB bounding box
}
for (let i = 0; i < points.length; i++) this.cp.push(points[i]);
this.aabb = this.AABB();
}
for (let i = 0; i < points.length; i++) this.dp.push(points[i]);
}
for (let i = s, l = this.cp.length; i < l; i++) {
this.dp.push(this.cp[i], this.cp[(i + 1) % l]);
}
}
createPoly(x, y, c, r, a) {
this.cp.length = 0;
for (let i = 0; i < c; i++) {
this.cp.push([
x + Math.sin(i * Math.PI * 2 / c + a) * r,
y + Math.cos(i * Math.PI * 2 / c + a) * r
]);
}
this.aabb = this.AABB();
}
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];
const line_hash =
Math.min(d0[0], d1[0]).toFixed(2) +
"-" +
Math.max(d0[0], d1[0]).toFixed(2) +
"-" +
Math.min(d0[1], d1[1]).toFixed(2) +
"-" +
Math.max(d0[1], d1[1]).toFixed(2);

if (!linesDrawn[line_hash]) {
t.penup();
t.goto(d0);
t.pendown();
t.goto(d1);
linesDrawn[line_hash] = true;
}
}
}
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
];
}
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
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 {
list() {
return polygonList;
},
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);
}
}
};
}```