Applying Simplex, Bezier and rotations
Supernova ✨ (variation)
Supernova ✨ (variation)
Supernova ✨ (variation)
Log in to post a comment.
const seed = 'Change me, empty seed means random every run'; //type=string
const circleVerticeCount = 19; //min=1 max=50 step=1
const circleInnerRadius = 5; //min=0 max=50 step=.1
const circleOuterRadius = 150; //disabled min=50 max=150 step=.1
const outerRotation = 100; //min=0 max=360 step=.1
const raySpread = 40; //min=0 max=200 step=.5
const raysPerVertice = 9; //min=1 max=50 step=1
const raySpreadOffset = 1; //min=-1 max=1 step=.01
const innerForce = 25; //min=0 max=100 step=.1
const innerForceRotation = 70; //min=0 max=360 step=.1
const cutoffType = 1; //min=0 max=5 step=1 (None, Simplex field same radius, Simplex field independent radius, Independent Simplex fields same radius, Independent Simplex fields and radii, Rounded square)
const cutoffRadius = .5; //min=-1 max=1 step=.01 For cutoffType 'rounded square' only
const cutResolution = 500; //min=3 max=1000 step=1
const cutIrregularity = 1.4; //min=.0 max=30 step=.01
const cutNoiseAmp = .17; //min=0 max=1 step=.01
const cutDistanceAmp = 0;//min=-1 max=1 step=.01
const showNebula = 0; //min=0 max=1 step=1 (No, Yes)
// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);
// Global code will be evaluated once.
turtlelib_init();
seed == ''? R.seedRandom(): R.seed(seed);
const turtle = new Turtle();
const noise = Simplex().createNoise2D();
const outerRot = V.rot2d(-outerRotation * Math.PI / 180);
const normalRot = V.rot2d(-outerRotation * Math.PI / 180 -Math.PI/2);
const innerForceRot = V.rot2d(-innerForceRotation * Math.PI / 180);
const circlePtsNormals = PT.circular(1, circleVerticeCount);
const circlePtsInner = circlePtsNormals.map(pt => V.scale(pt, circleInnerRadius));
const spreadNormals = circlePtsNormals.map(pt => V.trans(normalRot, pt));
const circlePtsOuter = circlePtsNormals
.map(pt => V.trans(outerRot, V.scale(pt, circleOuterRadius)))
.map((pt, ptIdx) => Array.from({length: raysPerVertice}).map((e, i, a) => V.add(pt, V.scale(spreadNormals[ptIdx], raySpread * i/a.length + (raySpreadOffset/2 - .5) * raySpread))));
const cutoffPaths = getCutoffPaths(cutoffType);
if(showNebula == 1) cutoffPaths.forEach(path => PT.drawTour(turtle, path));
// The walk function will be called until it returns false.
function walk(i) {
const circlePtIdx = i/raysPerVertice|0;
const pt = circlePtsOuter[circlePtIdx][i%raysPerVertice];
const path = PT.bezier(
circlePtsInner[circlePtIdx],
V.add(circlePtsInner[circlePtIdx], V.trans(innerForceRot, V.scale(circlePtsNormals[circlePtIdx], innerForce))),
pt,
pt
);
turtle.jump(path[0]);
path.some((pt, ptIdx) => {
if(ptIdx == 0) return false;
const interInfo = (() => {
const cutoffPath = cutoffPaths[circlePtIdx%cutoffPaths.length];
for(let i = 0; i < cutoffPath.length; i++) {
let e = cutoffPath[i];
let eNext = cutoffPath[(i+1)%cutoffPath.length];
const intersect = Intersection.segment(path[ptIdx-1], pt, e, eNext);
if(intersect === false) continue;
return intersect;
}
return false;
})();
const gotoIntersection = interInfo !== false;
turtle.goto(gotoIntersection? interInfo: pt);
return gotoIntersection;
});
return i < circlePtsInner.length * raysPerVertice - 1;
}
function getCutoffPaths(type) {
const getSimplexCutoffPath = (displace = 0) => {
const mapDistortion = e => cutNoiseAmp*e+1;
const distortions = Array.from({length: cutResolution}, (e, i) => noise(cutIrregularity*Math.sin(2 * Math.PI * i/(cutResolution - 1)) + displace*30, cutIrregularity*Math.cos(2 * Math.PI * i/(cutResolution - 1)) + displace*30));
let minMax = [[0,0],[0,0]];
const coords = Array.from({length: distortions.length}, (e, i) => {
const coord = [
mapDistortion(distortions[i])*100*Math.sin(2 * Math.PI * i/(cutResolution - 1)),
mapDistortion(distortions[i])*100*Math.cos(2 * Math.PI * i/(cutResolution - 1))
];
minMax = [
[Math.min(minMax[0][0], coord[0]), Math.min(minMax[0][1], coord[1])],
[Math.max(minMax[1][0], coord[0]), Math.max(minMax[1][1], coord[1])]
]
return coord;
});
const span = [minMax[1][0] - minMax[0][0], minMax[1][1] - minMax[0][1]];
const transpose = [minMax[0][0] + span[0]/2, minMax[0][1] + span[1]/2].map(e => -e);
const scalar = Math.min(190/span[0], 190/span[1]);
const pp = 1;
return coords.map(ee =>
V.lerp(
V.scale(V.norm(V.add(transpose, ee)), 95 * pp),
V.scale(V.add(transpose, ee), scalar * 1),
Math.abs(cutDistanceAmp) * (cutDistanceAmp < 0? 1 - pp: pp) + (1-Math.abs(cutDistanceAmp))
)
);
}
const independentCuts = cutoffType == 3 || cutoffType == 4;
const independentRadii = cutoffType == 2 || cutoffType == 4;
const cutScales = Array.from({length: (independentCuts || independentRadii? circleVerticeCount: 1)})
.map((e, i, a) => !independentRadii? 1: (.6 + Math.random()*.4) )
.reduce((acc, cur, i, a) => [[[...acc[0][0], cur], Math.max(acc[0][1], cur)]], [[[], 0]])
.flatMap(acc => acc[0].map(v => v/acc[1]));
switch(type) {
case 0:
return [[]];
case 1:
case 2:
case 3:
case 4:
return Array.from({length: cutScales.length}, (e,i) => getSimplexCutoffPath(i).map(pt => V.scale(pt, cutScales[i])));
case 5:
const roundedRectR = cutoffRadius;
const maxRadius = 95;
const qCirc = PT.arc(maxRadius*Math.abs(roundedRectR), Math.PI/2, 0, null, true).map(pt => cutoffRadius >= 0? pt: V.add([-pt[0], -pt[1]], [maxRadius*roundedRectR, maxRadius*roundedRectR]));
if(cutoffRadius < 0) {
qCirc.reverse();
}
return [Array.from({length: 4}).flatMap((e,i) => {
return qCirc.map(pt => V.trans(V.rot2d(-i*Math.PI/2), V.add(pt, [maxRadius - maxRadius*roundedRectR, maxRadius - maxRadius*roundedRectR])));
})];
}
}
// Ported https://www.skypack.dev/view/simplex-noise
function Simplex() {
const F2 = 0.5 * (Math.sqrt(3) - 1);
const G2 = (3 - Math.sqrt(3)) / 6;
const F3 = 1 / 3;
const G3 = 1 / 6;
const F4 = (Math.sqrt(5) - 1) / 4;
const G4 = (5 - Math.sqrt(5)) / 20;
const fastFloor = (x) => Math.floor(x) | 0;
const grad2 = /* @__PURE__ */ new Float64Array([1,1,-1,1,1,-1,-1,-1,1,0,-1,0,1,0,-1,0,0,1,0,-1,0,1,0,-1]);
const grad3 = /* @__PURE__ */ new Float64Array([1,1,0,-1,1,0,1,-1,0,-1,-1,0,1,0,1,-1,0,1,1,0,-1,-1,0,-1,0,1,1,0,-1,1,0,1,-1,0,-1,-1]);
const grad4 = /* @__PURE__ */ new Float64Array([0,1,1,1,0,1,1,-1,0,1,-1,1,0,1,-1,-1,0,-1,1,1,0,-1,1,-1,0,-1,-1,1,0,-1,-1,-1,1,0,1,1,1,0,1,-1,1,0,-1,1,1,0,-1,-1,-1,0,1,1,-1,0,1,-1,-1,0,-1,1,-1,0,-1,-1,1,1,0,1,1,1,0,-1,1,-1,0,1,1,-1,0,-1,-1,1,0,1,-1,1,0,-1,-1,-1,0,1,-1,-1,0,-1,1,1,1,0,1,1,-1,0,1,-1,1,0,1,-1,-1,0,-1,1,1,0,-1,1,-1,0,-1,-1,1,0,-1,-1,-1,0]);
function createNoise2D(random = Math.random) {
const perm = buildPermutationTable(random);
const permGrad2x = new Float64Array(perm).map((v) => grad2[v % 12 * 2]);
const permGrad2y = new Float64Array(perm).map((v) => grad2[v % 12 * 2 + 1]);
return function noise2D(x, y) {
let n0 = 0;
let n1 = 0;
let n2 = 0;
const s = (x + y) * F2;
const i = fastFloor(x + s);
const j = fastFloor(y + s);
const t = (i + j) * G2;
const X0 = i - t;
const Y0 = j - t;
const x0 = x - X0;
const y0 = y - Y0;
let i1, j1;
if (x0 > y0) {
i1 = 1;
j1 = 0;
} else {
i1 = 0;
j1 = 1;
}
const x1 = x0 - i1 + G2;
const y1 = y0 - j1 + G2;
const x2 = x0 - 1 + 2 * G2;
const y2 = y0 - 1 + 2 * G2;
const ii = i & 255;
const jj = j & 255;
let t0 = 0.5 - x0 * x0 - y0 * y0;
if (t0 >= 0) {
const gi0 = ii + perm[jj];
const g0x = permGrad2x[gi0];
const g0y = permGrad2y[gi0];
t0 *= t0;
n0 = t0 * t0 * (g0x * x0 + g0y * y0);
}
let t1 = 0.5 - x1 * x1 - y1 * y1;
if (t1 >= 0) {
const gi1 = ii + i1 + perm[jj + j1];
const g1x = permGrad2x[gi1];
const g1y = permGrad2y[gi1];
t1 *= t1;
n1 = t1 * t1 * (g1x * x1 + g1y * y1);
}
let t2 = 0.5 - x2 * x2 - y2 * y2;
if (t2 >= 0) {
const gi2 = ii + 1 + perm[jj + 1];
const g2x = permGrad2x[gi2];
const g2y = permGrad2y[gi2];
t2 *= t2;
n2 = t2 * t2 * (g2x * x2 + g2y * y2);
}
return 70 * (n0 + n1 + n2);
};
}
function createNoise3D(random = Math.random) {
const perm = buildPermutationTable(random);
const permGrad3x = new Float64Array(perm).map((v) => grad3[v % 12 * 3]);
const permGrad3y = new Float64Array(perm).map((v) => grad3[v % 12 * 3 + 1]);
const permGrad3z = new Float64Array(perm).map((v) => grad3[v % 12 * 3 + 2]);
return function noise3D(x, y, z) {
let n0, n1, n2, n3;
const s = (x + y + z) * F3;
const i = fastFloor(x + s);
const j = fastFloor(y + s);
const k = fastFloor(z + s);
const t = (i + j + k) * G3;
const X0 = i - t;
const Y0 = j - t;
const Z0 = k - t;
const x0 = x - X0;
const y0 = y - Y0;
const z0 = z - Z0;
let i1, j1, k1;
let i2, j2, k2;
if (x0 >= y0) {
if (y0 >= z0) {
i1 = 1;
j1 = 0;
k1 = 0;
i2 = 1;
j2 = 1;
k2 = 0;
} else if (x0 >= z0) {
i1 = 1;
j1 = 0;
k1 = 0;
i2 = 1;
j2 = 0;
k2 = 1;
} else {
i1 = 0;
j1 = 0;
k1 = 1;
i2 = 1;
j2 = 0;
k2 = 1;
}
} else {
if (y0 < z0) {
i1 = 0;
j1 = 0;
k1 = 1;
i2 = 0;
j2 = 1;
k2 = 1;
} else if (x0 < z0) {
i1 = 0;
j1 = 1;
k1 = 0;
i2 = 0;
j2 = 1;
k2 = 1;
} else {
i1 = 0;
j1 = 1;
k1 = 0;
i2 = 1;
j2 = 1;
k2 = 0;
}
}
const x1 = x0 - i1 + G3;
const y1 = y0 - j1 + G3;
const z1 = z0 - k1 + G3;
const x2 = x0 - i2 + 2 * G3;
const y2 = y0 - j2 + 2 * G3;
const z2 = z0 - k2 + 2 * G3;
const x3 = x0 - 1 + 3 * G3;
const y3 = y0 - 1 + 3 * G3;
const z3 = z0 - 1 + 3 * G3;
const ii = i & 255;
const jj = j & 255;
const kk = k & 255;
let t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
if (t0 < 0)
n0 = 0;
else {
const gi0 = ii + perm[jj + perm[kk]];
t0 *= t0;
n0 = t0 * t0 * (permGrad3x[gi0] * x0 + permGrad3y[gi0] * y0 + permGrad3z[gi0] * z0);
}
let t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
if (t1 < 0)
n1 = 0;
else {
const gi1 = ii + i1 + perm[jj + j1 + perm[kk + k1]];
t1 *= t1;
n1 = t1 * t1 * (permGrad3x[gi1] * x1 + permGrad3y[gi1] * y1 + permGrad3z[gi1] * z1);
}
let t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
if (t2 < 0)
n2 = 0;
else {
const gi2 = ii + i2 + perm[jj + j2 + perm[kk + k2]];
t2 *= t2;
n2 = t2 * t2 * (permGrad3x[gi2] * x2 + permGrad3y[gi2] * y2 + permGrad3z[gi2] * z2);
}
let t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
if (t3 < 0)
n3 = 0;
else {
const gi3 = ii + 1 + perm[jj + 1 + perm[kk + 1]];
t3 *= t3;
n3 = t3 * t3 * (permGrad3x[gi3] * x3 + permGrad3y[gi3] * y3 + permGrad3z[gi3] * z3);
}
return 32 * (n0 + n1 + n2 + n3);
};
}
function createNoise4D(random = Math.random) {
const perm = buildPermutationTable(random);
const permGrad4x = new Float64Array(perm).map((v) => grad4[v % 32 * 4]);
const permGrad4y = new Float64Array(perm).map((v) => grad4[v % 32 * 4 + 1]);
const permGrad4z = new Float64Array(perm).map((v) => grad4[v % 32 * 4 + 2]);
const permGrad4w = new Float64Array(perm).map((v) => grad4[v % 32 * 4 + 3]);
return function noise4D(x, y, z, w) {
let n0, n1, n2, n3, n4;
const s = (x + y + z + w) * F4;
const i = fastFloor(x + s);
const j = fastFloor(y + s);
const k = fastFloor(z + s);
const l = fastFloor(w + s);
const t = (i + j + k + l) * G4;
const X0 = i - t;
const Y0 = j - t;
const Z0 = k - t;
const W0 = l - t;
const x0 = x - X0;
const y0 = y - Y0;
const z0 = z - Z0;
const w0 = w - W0;
let rankx = 0;
let ranky = 0;
let rankz = 0;
let rankw = 0;
if (x0 > y0)
rankx++;
else
ranky++;
if (x0 > z0)
rankx++;
else
rankz++;
if (x0 > w0)
rankx++;
else
rankw++;
if (y0 > z0)
ranky++;
else
rankz++;
if (y0 > w0)
ranky++;
else
rankw++;
if (z0 > w0)
rankz++;
else
rankw++;
const i1 = rankx >= 3 ? 1 : 0;
const j1 = ranky >= 3 ? 1 : 0;
const k1 = rankz >= 3 ? 1 : 0;
const l1 = rankw >= 3 ? 1 : 0;
const i2 = rankx >= 2 ? 1 : 0;
const j2 = ranky >= 2 ? 1 : 0;
const k2 = rankz >= 2 ? 1 : 0;
const l2 = rankw >= 2 ? 1 : 0;
const i3 = rankx >= 1 ? 1 : 0;
const j3 = ranky >= 1 ? 1 : 0;
const k3 = rankz >= 1 ? 1 : 0;
const l3 = rankw >= 1 ? 1 : 0;
const x1 = x0 - i1 + G4;
const y1 = y0 - j1 + G4;
const z1 = z0 - k1 + G4;
const w1 = w0 - l1 + G4;
const x2 = x0 - i2 + 2 * G4;
const y2 = y0 - j2 + 2 * G4;
const z2 = z0 - k2 + 2 * G4;
const w2 = w0 - l2 + 2 * G4;
const x3 = x0 - i3 + 3 * G4;
const y3 = y0 - j3 + 3 * G4;
const z3 = z0 - k3 + 3 * G4;
const w3 = w0 - l3 + 3 * G4;
const x4 = x0 - 1 + 4 * G4;
const y4 = y0 - 1 + 4 * G4;
const z4 = z0 - 1 + 4 * G4;
const w4 = w0 - 1 + 4 * G4;
const ii = i & 255;
const jj = j & 255;
const kk = k & 255;
const ll = l & 255;
let t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
if (t0 < 0)
n0 = 0;
else {
const gi0 = ii + perm[jj + perm[kk + perm[ll]]];
t0 *= t0;
n0 = t0 * t0 * (permGrad4x[gi0] * x0 + permGrad4y[gi0] * y0 + permGrad4z[gi0] * z0 + permGrad4w[gi0] * w0);
}
let t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
if (t1 < 0)
n1 = 0;
else {
const gi1 = ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]];
t1 *= t1;
n1 = t1 * t1 * (permGrad4x[gi1] * x1 + permGrad4y[gi1] * y1 + permGrad4z[gi1] * z1 + permGrad4w[gi1] * w1);
}
let t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
if (t2 < 0)
n2 = 0;
else {
const gi2 = ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]];
t2 *= t2;
n2 = t2 * t2 * (permGrad4x[gi2] * x2 + permGrad4y[gi2] * y2 + permGrad4z[gi2] * z2 + permGrad4w[gi2] * w2);
}
let t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
if (t3 < 0)
n3 = 0;
else {
const gi3 = ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]];
t3 *= t3;
n3 = t3 * t3 * (permGrad4x[gi3] * x3 + permGrad4y[gi3] * y3 + permGrad4z[gi3] * z3 + permGrad4w[gi3] * w3);
}
let t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
if (t4 < 0)
n4 = 0;
else {
const gi4 = ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]];
t4 *= t4;
n4 = t4 * t4 * (permGrad4x[gi4] * x4 + permGrad4y[gi4] * y4 + permGrad4z[gi4] * z4 + permGrad4w[gi4] * w4);
}
return 27 * (n0 + n1 + n2 + n3 + n4);
};
}
function buildPermutationTable(random) {
const tableSize = 512;
const p = new Uint8Array(tableSize);
for (let i = 0; i < tableSize / 2; i++) {
p[i] = i;
}
for (let i = 0; i < tableSize / 2 - 1; i++) {
const r = i + ~~(random() * (256 - i));
const aux = p[i];
p[i] = p[r];
p[r] = aux;
}
for (let i = 256; i < tableSize; i++) {
p[i] = p[i - 256];
}
return p;
}
class Simplex {
buildPermutationTable(random) { return buildPermutationTable(random); }
createNoise2D(random) { return createNoise2D(random); }
createNoise3D(random) { return createNoise3D(random); }
createNoise4D(random) { return createNoise4D(random); }
}
return new Simplex();
}
// Below is automatically maintained by Turtlelib 1.0
// Changes below this comment might interfere with its correct functioning.
function turtlelib_init() {
turtlelib_ns_c6665b0e9b_Jurgen_Vector_Math();
turtlelib_ns_c5f8fa95ed_Jurgen_Intersection();
turtlelib_ns_2d89bd6d64_Jurgen_Path_tools();
turtlelib_ns_13b81fd40e_Jurgen_Randomness();
}
// Turtlelib Jurgen Vector Math v 4 - start - {"id":"c6665b0e9b","package":"Jurgen","name":"Vector Math","version":"4"}
function turtlelib_ns_c6665b0e9b_Jurgen_Vector_Math() {
/////////////////////////////////////////////////////////
// Vector functions - Created by Jurgen Westerhof 2024 //
/////////////////////////////////////////////////////////
class Vector {
static add (a,b) { return a.map((v,i)=>v+b[i]); }
static sub (a,b) { return a.map((v,i)=>v-b[i]); }
static mul (a,b) { return a.map((v,i)=>v*b[i]); }
static div (a,b) { return a.map((v,i)=>v/b[i]); }
static scale(a,s) { return a.map(v=>v*s); }
static det(m) { return m.length == 1? m[0][0]: m.length == 2 ? m[0][0]*m[1][1]-m[0][1]*m[1][0]: m[0].reduce((r,e,i) => r+(-1)**(i+2)*e*this.det(m.slice(1).map(c => c.filter((_,j) => i != j))),0); }
static angle(a) { return Math.PI - Math.atan2(a[1], -a[0]); } //compatible with turtletoy heading
static rot2d(angle) { return [[Math.cos(angle), -Math.sin(angle)], [Math.sin(angle), Math.cos(angle)]]; }
static rot3d(yaw,pitch,roll) { return [[Math.cos(yaw)*Math.cos(pitch), Math.cos(yaw)*Math.sin(pitch)*Math.sin(roll)-Math.sin(yaw)*Math.cos(roll), Math.cos(yaw)*Math.sin(pitch)*Math.cos(roll)+Math.sin(yaw)*Math.sin(roll)],[Math.sin(yaw)*Math.cos(pitch), Math.sin(yaw)*Math.sin(pitch)*Math.sin(roll)+Math.cos(yaw)*Math.cos(roll), Math.sin(yaw)*Math.sin(pitch)*Math.cos(roll)-Math.cos(yaw)*Math.sin(roll)],[-Math.sin(pitch), Math.cos(pitch)*Math.sin(roll), Math.cos(pitch)*Math.cos(roll)]]; }
static trans(matrix,a) { return a.map((v,i) => a.reduce((acc, cur, ci) => acc + cur * matrix[ci][i], 0)); }
//Mirror vector a in a ray through [0,0] with direction mirror
static mirror2d(a,mirror) { return [Math.atan2(...mirror)].map(angle => this.trans(this.rot2d(angle), this.mul([-1,1], this.trans(this.rot2d(-angle), a)))).pop(); }
static equals(a,b) { return !a.some((e, i) => e != b[i]); }
static approx(a,b,p) { return this.len(this.sub(a,b)) < (p === undefined? .001: p); }
static norm (a) { return this.scale(a,1/this.len(a)); }
static len (a) { return Math.hypot(...a); }
static lenSq (a) { return a.reduce((a,c)=>a+c**2,0); }
static lerp (a,b,t) { return a.map((v, i) => v*(1-t) + b[i]*t); }
static dist (a,b) { return Math.hypot(...this.sub(a,b)); }
static dot (a,b) { return a.reduce((a,c,i) => a+c*b[i], 0); }
static cross(...ab) { return ab[0].map((e, i) => ab.map(v => v.filter((ee, ii) => ii != i))).map((m,i) => (i%2==0?-1:1)*this.det(m)); }
static clamp(a,min,max) { return a.map((e,i) => Math.min(Math.max(e, min[i]), max[i])) };
static rotateClamp(a,min,max) { return a.map((e,i) => {const d = max[i]-min[i];if(d == 0) return min[i];while(e < min[i]) { e+=d; }while(e > max[i]) { e-=d; }return e;});
}
}
this.V = Vector;
}
// Turtlelib Jurgen Vector Math v 4 - end
// Turtlelib Jurgen Intersection v 4 - start - {"id":"c5f8fa95ed","package":"Jurgen","name":"Intersection","version":"4"}
function turtlelib_ns_c5f8fa95ed_Jurgen_Intersection() {
///////////////////////////////////////////////////////////////
// Intersection functions - Created by Jurgen Westerhof 2024 //
///////////////////////////////////////////////////////////////
class Intersection {
//a-start, a-direction, b-start, b-direction
//returns false on no intersection or [[intersection:x,y], scalar a-direction, scalar b-direction
static info(as, ad, bs, bd) { const d = V.sub(bs, as), det = -V.det([bd, ad]); if(det === 0) return false; const res = [V.det([d, bd]) / det, V.det([d, ad]) / det]; return [V.add(as, V.scale(ad, res[0])), ...res]; }
static ray(a, b, c, d) { return this.info(a, b, c, d); }
static segment(a,b,c,d, inclusiveStart = true, inclusiveEnd = true) { const i = this.info(a, V.sub(b, a), c, V.sub(d, c)); return i === false? false: ( (inclusiveStart? 0<=i[1] && 0<=i[2]: 0<i[1] && 0<i[2]) && (inclusiveEnd? i[1]<=1 && i[2]<=1: i[1]<1 && i[2]<1) )?i[0]:false;}
static tour(tour, segmentStart, segmentDirection) { return tour.map((e, i, a) => [i, this.info(e, V.sub(a[(i+1)%a.length], e), segmentStart, segmentDirection)]).filter(e => e[1] !== false && 0 <= e[1][1] && e[1][1] <= 1).filter(e => 0 <= e[1][2]).map(e => ({position: e[1][0],tourIndex: e[0],tourSegmentPortion: e[1][1],segmentPortion: e[1][2],}));}
static inside(tour, pt) { return tour.map((e,i,a) => this.segment(e, a[(i+1)%a.length], pt, [Number.MAX_SAFE_INTEGER, 0], true, false)).filter(e => e !== false).length % 2 == 1; }
static circles(centerA, radiusA, centerB, radiusB) {const result = {intersect_count: 0,intersect_occurs: true,one_is_in_other: false,are_equal: false,point_1: [null, null],point_2: [null, null],};const dx = centerB[0] - centerA[0];const dy = centerB[1] - centerA[1];const dist = Math.hypot(dy, dx);if (dist > radiusA + radiusB) {result.intersect_occurs = false;}if (dist < Math.abs(radiusA - radiusB) && !N.approx(dist, Math.abs(radiusA - radiusB))) {result.intersect_occurs = false;result.one_is_in_other = true;}if (V.approx(centerA, centerB) && radiusA === radiusB) {result.are_equal = true;}if (result.intersect_occurs) {const centroid = (radiusA**2 - radiusB**2 + dist * dist) / (2.0 * dist);const x2 = centerA[0] + (dx * centroid) / dist;const y2 = centerA[1] + (dy * centroid) / dist;const prec = 10000;const h = (Math.round(radiusA**2 * prec)/prec - Math.round(centroid**2 * prec)/prec)**.5;const rx = -dy * (h / dist);const ry = dx * (h / dist);result.point_1 = [x2 + rx, y2 + ry];result.point_2 = [x2 - rx, y2 - ry];if (result.are_equal) {result.intersect_count = Infinity;} else if (V.equals(result.point_1, result.point_2)) {result.intersect_count = 1;} else {result.intersect_count = 2;}}return result;}
}
this.Intersection = Intersection;
}
// Turtlelib Jurgen Intersection v 4 - end
// Turtlelib Jurgen Path tools v 3 - start - {"id":"2d89bd6d64","package":"Jurgen","name":"Path tools","version":"3"}
function turtlelib_ns_2d89bd6d64_Jurgen_Path_tools() {
///////////////////////////////////////////////////////
// Path functions - Created by Jurgen Westerhof 2024 //
///////////////////////////////////////////////////////
class PathTools {
static bezier(p1, cp1, cp2, p2, steps = null) {steps = (steps === null? Math.max(3, (V.len(V.sub(cp1, p1)) + V.len(V.sub(cp2, cp1)) + V.len(V.sub(p2, cp2))) | 0): steps) - 1; return Array.from({length: steps + 1}).map((v, i, a, f = i/steps) => [[V.lerp(p1, cp1, f),V.lerp(cp1, cp2, f),V.lerp(cp2, p2, f)]].map(v => V.lerp(V.lerp(v[0], v[1], f), V.lerp(v[1], v[2], f), f))[0]);}
// https://stackoverflow.com/questions/18655135/divide-bezier-curve-into-two-equal-halves#18681336
static splitBezier(p1, cp1, cp2, p2, t=.5) {const e = V.lerp(p1, cp1, t);const f = V.lerp(cp1, cp2, t);const g = V.lerp(cp2, p2, t);const h = V.lerp(e, f, t);const j = V.lerp(f, g, t);const k = V.lerp(h, j, t);return [[p1, e, h, k], [k, j, g, p2]];}
static circular(radius,verticeCount,rotation=0) {return Array.from({length: verticeCount}).map((e,i,a,f=i*2*Math.PI/verticeCount+rotation) => [radius*Math.cos(f),radius*Math.sin(f)])}
static circle(r){return this.circular(r,Math.max(12, r*2*Math.PI|0));}
static arc(radius, extend = 2 * Math.PI, clockWiseStart = 0, steps = null, includeLast = false) { return [steps == null? (radius*extend+1)|0: steps].map(steps => Array.from({length: steps}).map((v, i, a) => [radius * Math.cos(clockWiseStart + extend*i/(a.length-(includeLast?1:0))), radius * Math.sin(clockWiseStart + extend*i/(a.length-(includeLast?1:0)))])).pop(); }
static draw(turtle, path) {path.forEach((pt, i) => turtle[i==0?'jump':'goto'](pt));}
static drawTour(turtle, path) {this.draw(turtle, path.concat([path[0]]));}
static drawPoint(turtle, pt, r = .1) {this.drawTour(turtle, this.circle(r).map(e => V.add(e, pt)));}
static drawArrow(turtle, s, d, width = 6, length = 3) {turtle.jump(s);const arrowHeadBase = V.add(s,d);turtle.goto(arrowHeadBase);turtle.goto(V.add(arrowHeadBase, V.trans(V.rot2d(-V.angle(d)), [-length, width/2])));turtle.jump(V.add(arrowHeadBase, V.trans(V.rot2d(-V.angle(d)), [-length, -width/2])));turtle.goto(arrowHeadBase);}
static circlesTangents(c1_center, c1_radius, c2_center, c2_radius, internal = false) {let middle_circle = [V.scale(V.sub(c1_center, c2_center), .5)].map(hwp => [V.add(c2_center, hwp), V.len(hwp)]).pop();if(!internal && c1_radius == c2_radius) {let target = V.sub(c2_center, c1_center);let scaledTarget = V.scale(target, c1_radius/V.len(target));let partResult = [V.add(c1_center, V.trans(V.rot2d(Math.PI/2), scaledTarget)),V.add(c1_center, V.trans(V.rot2d(Math.PI/-2), scaledTarget))];return [partResult,partResult.map(pt => V.add(pt, target))]}let swap = !internal && c2_radius > c1_radius;if(swap) {let t = [[...c1_center], c1_radius];c1_center = c2_center;c1_radius = c2_radius;c2_center = t[0];c2_radius = t[1];}let internal_waypoints = Intersection.circles(c1_center, c1_radius + (internal?c2_radius:-c2_radius), ...middle_circle);if(!internal_waypoints.intersect_occurs) return [];const circlePointAtDirection2 = (circle_center, radius, direction) => V.add(circle_center, V.scale(direction, radius/V.len(direction)));const result = [[circlePointAtDirection2(c1_center, c1_radius, V.sub(internal_waypoints.point_1, c1_center)),circlePointAtDirection2(c1_center, c1_radius, V.sub(internal_waypoints.point_2, c1_center))],[circlePointAtDirection2(c2_center, c2_radius, internal?V.sub(c1_center, internal_waypoints.point_1):V.sub(internal_waypoints.point_1, c1_center)),circlePointAtDirection2(c2_center, c2_radius, internal?V.sub(c1_center, internal_waypoints.point_2):V.sub(internal_waypoints.point_2, c1_center))]];return swap? [[result[1][1],result[1][0]],[result[0][1],result[0][0]]]: result;}
static vectors(path) {return Array.from({length: path.length - 1}).map((e, i) => V.sub(path[i+1], path[i]));}
static path(vectors) {return vectors.reduce((a,c) => a.length==0?[c]:[...a, V.add(c, a[a.length-1])], []);}
static redistributeLinear(path, length = .5) {const p = path.map(pt => [...pt]);const result = [[...p[0]]];let pointer = 1;doneAll: while(pointer < p.length) {let l = length;while(pointer < p.length) {const distance = V.len(V.sub(p[pointer], p[pointer - 1]));if(distance < l) {l-=distance;pointer++;continue;}if(distance == l) {if(pointer < p.length - 1) result.push([...p[pointer]]);pointer++;break doneAll;}if(l < distance) {const newPoint = V.lerp(p[pointer-1], p[pointer], l/distance);if(pointer < p.length - 1) result.push([...newPoint]);p[pointer - 1] = newPoint;break;}}}result.push(p.pop());return result;}
static length(path) { return this.lengths(path).reduce((c, a) => a + c, 0); }
static lengths(path) { return path.map((e, i, a) => V.len(V.sub(e, a[(i+1)%a.length]))).filter((e, i, a) => i < a.length - 1); }
static intersectInfoRay(path, origin, direction) {const vectors = this.vectors(path);const ri = vectors.map((e, i) => [i, Intersection.info(origin, direction, path[i], e)]).filter(e => 0 <= e[1][2] && e[1][2] <= 1 && 0 < e[1][1]).sort(e => e[1][1]);if(ri.length == 0) return false;const hit = ri[0];const lengths = this.lengths(path);const length = lengths.reduce((a, c) => a + c, 0);let l = 0;for(let i = 0; i < hit[0]; i++) {l += lengths[i];}return [hit[1][0], (l + (lengths[hit[0]] * hit[1][2])) / length, hit[1][1]];}
static lerp(path, part) {if(part < 0 || 1 < part) throw new Error('Range of part is 0 to 1, got ' + path);const lengths = this.lengths(path);const length = lengths.reduce((a, c) => a + c, 0);let l = length * part;for(let i = 0; i < lengths.length; i++) {if(lengths[i] < l) {l-=lengths[i];continue;}return V.lerp(path[i], path[i+1], l / V.len(V.sub(path[i+1], path[i])));}return [...path[path.length - 1]];}
static boundingBox(path) { return path.reduce((a, c) => [[Math.min(c[0], a[0][0]), Math.min(c[1], a[0][1])],[Math.max(c[0], a[1][0]), Math.max(c[1], a[1][1])]], [path[0], path[0]]); }
}
this.PT = PathTools;
}
// Turtlelib Jurgen Path tools v 3 - end
// Turtlelib Jurgen Randomness v 2 - start - {"id":"13b81fd40e","package":"Jurgen","name":"Randomness","version":"2"}
function turtlelib_ns_13b81fd40e_Jurgen_Randomness() {
///////////////////////////////////////////////////////////////
// Pseudorandom functions - Created by Jurgen Westerhof 2024 //
///////////////////////////////////////////////////////////////
class Random {
static #apply(seed) {
// Seedable random number generator by David Bau: http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html
!function(a,b,c,d,e,f,g,h,i){function j(a){var b,c=a.length,e=this,f=0,g=e.i=e.j=0,h=e.S=[];for(c||(a=[c++]);d>f;)h[f]=f++;for(f=0;d>f;f++)h[f]=h[g=s&g+a[f%c]+(b=h[f])],h[g]=b;(e.g=function(a){for(var b,c=0,f=e.i,g=e.j,h=e.S;a--;)b=h[f=s&f+1],c=c*d+h[s&(h[f]=h[g=s&g+b])+(h[g]=b)];return e.i=f,e.j=g,c})(d)}function k(a,b){var c,d=[],e=typeof a;if(b&&"object"==e)for(c in a)try{d.push(k(a[c],b-1))}catch(f){}return d.length?d:"string"==e?a:a+"\0"}function l(a,b){for(var c,d=a+"",e=0;e<d.length;)b[s&e]=s&(c^=19*b[s&e])+d.charCodeAt(e++);return n(b)}function m(c){try{return o?n(o.randomBytes(d)):(a.crypto.getRandomValues(c=new Uint8Array(d)),n(c))}catch(e){return[+new Date,a,(c=a.navigator)&&c.plugins,a.screen,n(b)]}}function n(a){return String.fromCharCode.apply(0,a)}var o,p=c.pow(d,e),q=c.pow(2,f),r=2*q,s=d-1,t=c["seed"+i]=function(a,f,g){var h=[];f=1==f?{entropy:!0}:f||{};var o=l(k(f.entropy?[a,n(b)]:null==a?m():a,3),h),s=new j(h);return l(n(s.S),b),(f.pass||g||function(a,b,d){return d?(c[i]=a,b):a})(function(){for(var a=s.g(e),b=p,c=0;q>a;)a=(a+c)*d,b*=d,c=s.g(1);for(;a>=r;)a/=2,b/=2,c>>>=1;return(a+c)/b},o,"global"in f?f.global:this==c)};if(l(c[i](),b),g&&g.exports){g.exports=t;try{o=require("crypto")}catch(u){}}else h&&h.amd&&h(function(){return t})}(this,[],Math,256,6,52,"object"==typeof module&&module,"function"==typeof define&&define,"random");
Math.seedrandom(seed);
}
static seedRandom() { this.#apply(new Date().getMilliseconds()); }
static seedDaily() { this.#apply(new Date().toDateString()); }
static seed(seed) { this.#apply(seed); }
static getInt(min, max) { if(max == undefined) {max = min + 1; min = 0; } const [mi, ma] = [Math.min(min, max), Math.max(min, max)]; return (mi + Math.random() * (ma - mi)) | 0;}
static get(min, max) {if(min == undefined) {return Math.random();}if(max == undefined) {max = min;min = 0;}const [mi, ma] = [Math.min(min, max), Math.max(min, max)];return mi + Math.random() * (ma - mi);}
static fraction(whole) { return this.get(0, whole); }
static getAngle(l = 1) { return l * this.get(0, 2*Math.PI); }
// Standard Normal variate using Box-Muller transform.
static getGaussian(mean=.5, stdev=.1) {const u = 1 - this.get(); /* Converting [0,1) to (0,1] */const v = this.get();const z = ( -2.0 * Math.log( u ) )**.5 * Math.cos( 2.0 * Math.PI * v );/* Transform to the desired mean and standard deviation: */return z * stdev + mean;}
static skew(value, skew = 0) { /*skew values (from 0 to 1) by a skew from -1 to 1, respectively right and left skewed (resp more values to left or to right), 0 is not skewed*/return (skew < 0)? value - (this.skew(1-value, -skew) - (1-value)): Math.pow(value, 1-Math.abs(skew));}
static getNormalDistributed(skew = 0) { /*skew values (from 0 to 1) by a skew from -1 to 1, respectively right and left skewed (resp more values to left or to right), 0 is not skewed*/let v = -1;while(v < 0 || 1 <= v) { v = this.getGaussian(.5, .1) };return this.skew(v, skew);}
}
this.R = Random;
}
// Turtlelib Jurgen Randomness v 2 - end