function createBezierCurveTo_circularSubdivided(
subdivisions,
debuggin = false
) {
return function bezierCurveTo_circular(
ctx,
x1,
y1,
cp1x,
cp1y,
cp2x,
cp2y,
x2,
y2
) {
function pointForT(t) {
return bezierCurve(t, [x1, y1], [cp1x, cp1y], [cp2x, cp2y], [x2, y2]);
}
const colors = colorRotator();
for (let i = 1; i <= subdivisions; i++) {
const t = (i - 1) / subdivisions;
const e = i / subdivisions;
const n = (t + e) / 2;
const p1 = pointForT(t);
const p2 = pointForT(n);
const p3 = pointForT(e);
const { center, radius } = circleFromThreePoints(p1, p2, p3);
const check1 = pointForT((t + n) / 2);
const check2 = pointForT((n + e) / 2);
const error1 = computeCircularError(check1, center, radius);
const error2 = computeCircularError(check2, center, radius);
const maxError = Math.max(error1, error2);
const startAngle = angle(center, p1);
const middleAngle = angle(center, p2);
const endAngle = angle(center, p3);
ctx.beginPath();
ctx.strokeStyle = colors.next().value;
ctx.arc(
...center,
radius,
startAngle,
endAngle,
startAngle > middleAngle || middleAngle > endAngle
);
ctx.stroke();
if (debuggin) {
ctx.beginPath();
ctx.globalAlpha = 0.25;
ctx.lineWidth = 1;
ctx.arc(...center, radius, 0, 2 * Math.PI);
ctx.stroke();
ctx.lineWidth = 2;
ctx.globalAlpha = 1;
}
}
};
}