pantograph2d 0.11.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{QuadraticBezier-B2g_Iyyl.js → QuadraticBezier-BeY2TXKH.js} +464 -433
- package/dist/QuadraticBezier-BeY2TXKH.js.map +1 -0
- package/dist/{QuadraticBezier-CuRsIP_D.cjs → QuadraticBezier-eSR75IzC.cjs} +9 -9
- package/dist/QuadraticBezier-eSR75IzC.cjs.map +1 -0
- package/dist/draw-BmJ8wIc9.cjs +2 -0
- package/dist/draw-BmJ8wIc9.cjs.map +1 -0
- package/dist/{draw-BJW5kfm9.js → draw-DzKVhurb.js} +94 -89
- package/dist/draw-DzKVhurb.js.map +1 -0
- package/dist/{models-DdZq-waE.js → models-BHeLX0dj.js} +544 -527
- package/dist/models-BHeLX0dj.js.map +1 -0
- package/dist/models-Fv9xfQBT.cjs +4 -0
- package/dist/models-Fv9xfQBT.cjs.map +1 -0
- package/dist/pantograph/drawShape.cjs +1 -1
- package/dist/pantograph/drawShape.js +2 -2
- package/dist/pantograph/models.cjs +1 -1
- package/dist/pantograph/models.js +2 -2
- package/dist/pantograph/svg.cjs +1 -1
- package/dist/pantograph/svg.js +1 -1
- package/dist/pantograph.cjs +2 -2
- package/dist/pantograph.cjs.map +1 -1
- package/dist/pantograph.js +83 -83
- package/dist/pantograph.js.map +1 -1
- package/dist/svg-BTO7h1ix.js +62 -0
- package/dist/svg-BTO7h1ix.js.map +1 -0
- package/dist/svg-b6SOnbda.cjs +8 -0
- package/dist/svg-b6SOnbda.cjs.map +1 -0
- package/dist/types/src/algorithms/offsets/offsetSegment.d.ts +1 -0
- package/dist/types/src/models/BoundingBox.d.ts +1 -0
- package/dist/types/src/models/Diagram.d.ts +1 -0
- package/dist/types/src/models/Figure.d.ts +1 -0
- package/dist/types/src/models/Loop.d.ts +1 -0
- package/dist/types/src/models/Strand.d.ts +5 -0
- package/dist/types/src/models/TransformationMatrix.d.ts +1 -0
- package/dist/types/src/models/segments/Arc.d.ts +1 -0
- package/dist/types/src/models/segments/CubicBezier.d.ts +1 -0
- package/dist/types/src/models/segments/EllipseArc.d.ts +1 -0
- package/dist/types/src/models/segments/Line.d.ts +2 -0
- package/dist/types/src/models/segments/QuadraticBezier.d.ts +1 -0
- package/dist/types/src/tesselationOperations.d.ts +4 -4
- package/package.json +2 -2
- package/dist/QuadraticBezier-B2g_Iyyl.js.map +0 -1
- package/dist/QuadraticBezier-CuRsIP_D.cjs.map +0 -1
- package/dist/draw-9Elv4xz6.cjs +0 -2
- package/dist/draw-9Elv4xz6.cjs.map +0 -1
- package/dist/draw-BJW5kfm9.js.map +0 -1
- package/dist/models-DdZq-waE.js.map +0 -1
- package/dist/models-LHGiMarC.cjs +0 -4
- package/dist/models-LHGiMarC.cjs.map +0 -1
- package/dist/svg-BzloQ9l1.cjs +0 -8
- package/dist/svg-BzloQ9l1.cjs.map +0 -1
- package/dist/svg-D8vwkQf7.js +0 -62
- package/dist/svg-D8vwkQf7.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { D as s, F as e, L as i, S as o, g as n } from "../models-
|
|
2
|
-
import { A as m, B as g, C as B, E as c, L as f, Q as p, G as u, T as x } from "../QuadraticBezier-
|
|
1
|
+
import { D as s, F as e, L as i, S as o, g as n } from "../models-BHeLX0dj.js";
|
|
2
|
+
import { A as m, B as g, C as B, E as c, L as f, Q as p, G as u, T as x } from "../QuadraticBezier-BeY2TXKH.js";
|
|
3
3
|
export {
|
|
4
4
|
m as Arc,
|
|
5
5
|
g as BoundingBox,
|
package/dist/pantograph/svg.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("../svg-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("../svg-b6SOnbda.cjs");exports.svgDiagram=g.svgDiagram;exports.svgFigure=g.svgFigure;exports.svgLoop=g.svgLoop;exports.svgSegmentToPath=g.svgSegmentToPath;exports.svgStrand=g.svgStrand;exports.svgViewbox=g.svgViewbox;
|
|
2
2
|
//# sourceMappingURL=svg.cjs.map
|
package/dist/pantograph/svg.js
CHANGED
package/dist/pantograph.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var bt=Object.defineProperty;var It=(t,n,e)=>n in t?bt(t,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[n]=e;var w=(t,n,e)=>It(t,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("./draw-9Elv4xz6.cjs"),c=require("./models-LHGiMarC.cjs"),a=require("./QuadraticBezier-CuRsIP_D.cjs"),D=require("./svg-BzloQ9l1.cjs");function I(t){if(t instanceof c.Figure)return[t];if(t instanceof c.Loop)return[new c.Figure(t)];if(t instanceof c.Diagram)return t.figures;throw new Error("Unknown shape")}function ct(t,n,e=1e-9){let r=[];const i=[],s=new Array(n.segments.length).fill(0).map(()=>[]);n.segments.forEach((f,l)=>{t.segments.forEach(u=>{const{intersections:h,overlaps:p}=c.findIntersectionsAndOverlaps(f,u,e);r.push(...h),s[l].push(...h),i.push(...p);const d=p.flatMap(m=>[m.firstPoint,m.lastPoint]);r.push(...d),s[l].push(...d)})}),r=a.removeDuplicatePoints(r,e);const o=a.zip([n.segments,s]).flatMap(([f,l])=>l.length?f.splitAt(l):[f]);return Array.from(c.strandsBetweenIntersections(o,r,i))}function H(t,n,e=!1){return ct(n,t).filter(i=>{const s=i.segments[0].midPoint;return n.onStroke(s)?!e:!n.contains(s)})}function K(t,n,e=!1){return ct(n,t).filter(i=>{const s=i.segments[0].midPoint;return n.onStroke(s)?!e:n.contains(s)})}function Z(t,n,e=!1){const r=H(t,n.contour,e),i=n.holes.flatMap(s=>K(t,s,e));return[...r,...i]}function W(t,n,e=!1){let r=K(t,n.contour,e);return n.holes.forEach(i=>{r=r.flatMap(s=>H(s,i,e))}),r}function at(t,n){return new c.Diagram(c.fuseFiguresLists(I(t),I(n)))}function E(t){return t.reduce((n,e)=>at(n,e),new c.Diagram)}function J(t,n){return new c.Diagram(c.cutFiguresLists(I(t),I(n)))}function Dt(t,n){return new c.Diagram(c.intersectFiguresLists(I(t),I(n)))}function vt(t,n,e=!0){if(n instanceof c.Loop)return H(t,n,e);if(n instanceof c.Figure)return Z(t,n,e);let r=[new c.Strand([...t.segments])];return n.figures.forEach(i=>{r=r.flatMap(s=>Z(s,i,e))}),r}function Ft(t,n,e=!1){if(n instanceof c.Loop)return K(t,n,e);if(n instanceof c.Figure)return W(t,n,e);let r=[new c.Strand([...t.segments])];return n.figures.forEach(i=>{r=r.flatMap(s=>W(s,i,e))}),r}function j(t){return t<0?"before":t>1?"after":"between"}const tt=(t,n,e)=>{if(e==="before")return t.distanceFrom(n.firstPoint);if(e==="after")return t.distanceFrom(n.lastPoint);throw new Error("Invalid position")};function Et(t,n){const e=a.lineLineParams(t,n);if(e==="parallel")return Math.min(t.distanceFrom(n.firstPoint),t.distanceFrom(n.lastPoint));const{intersectionParam1:r,intersectionParam2:i}=e,s=j(r),o=j(i);if(s==="between"&&o==="between")return 0;if(s==="between"&&o!=="between")return tt(t,n,o);if(o==="between"&&s!=="between")return tt(n,t,s);if(s==="before"&&o==="before")return a.distance(t.firstPoint,n.firstPoint);if(s==="after"&&o==="after")return a.distance(t.lastPoint,n.lastPoint);if(s==="before"&&o==="after")return a.distance(t.firstPoint,n.lastPoint);if(s==="after"&&o==="before")return a.distance(t.lastPoint,n.firstPoint);throw new Error("Invalid position")}function nt(t,n){if(c.lineArcIntersection(t,n).length>0)return 0;const e=c.projectPointOnLine(t,n.center);if(t.isOnSegment(e)){const r=a.distance(e,n.center);if(Math.abs(r-n.radius)<t.precision&&n.isOnSegment(e))return 0;if(r-n.radius>t.precision){const i=a.normalize(a.subtract(e,n.center)),s=a.add(n.center,a.scalarMultiply(i,n.radius));if(n.isOnSegment(s))return a.distance(s,e)}}return Math.min(n.distanceFrom(t.firstPoint),n.distanceFrom(t.lastPoint),t.distanceFrom(n.firstPoint),t.distanceFrom(n.lastPoint))}const kt=(t,n)=>{const e=t.angleToParam(n.firstAngle);if(t.isValidParameter(e))return!0;const r=t.angleToParam(n.lastAngle);return!!t.isValidParameter(r)};function Ct(t,n){if(c.arcArcIntersection(t,n,!0).length>0)return 0;const e=a.distance(t.center,n.center);if(e<t.precision&&kt(t,n))return Math.abs(t.radius-n.radius);const r=a.normalize(a.subtract(n.center,t.center)),i=e-Math.abs(t.radius-n.radius)<t.precision;let s=a.polarAngle(r);i&&n.radius>t.radius&&(s+=Math.PI);const o=i?s:s+Math.PI,f=t.angleToParam(s),l=n.angleToParam(o);return t.isValidParameter(f)&&n.isValidParameter(l)?a.distance(t.paramPoint(f),n.paramPoint(l)):Math.min(t.distanceFrom(n.firstPoint),t.distanceFrom(n.lastPoint),n.distanceFrom(t.firstPoint),n.distanceFrom(t.lastPoint))}function yt(t,n,e){let r=0,i=t.length-1;for(;r<=i;){const s=Math.floor((r+i)/2),o=e(t[s],n);if(o<0)r=s+1;else if(o>0)i=s-1;else return s}return-(r+1)}function xt(t,n,e){const r=yt(t,n,e);r<0?t.splice(-(r+1),0,n):t.splice(r,0,n)}class Ot{constructor(){w(this,"buckets");this.buckets=[]}addInterval(n){const e=this.buckets[n.rectangle.diagonalBucketIndex];e===void 0?this.buckets[n.rectangle.diagonalBucketIndex]=[n]:xt(e,n,(r,i)=>r.value-i.value)}removeInterval(n){const e=this.buckets[n.rectangle.diagonalBucketIndex];if(e===void 0)throw new Error("Interval not found");e.shift()}getBottomRightHullIntervals(){const n=[];for(let e=this.buckets.length-1;e>=0;e--){const r=this.buckets[e];if(!r===void 0)continue;const i=r[0];if(i!==void 0){if(!n.length){n.push(i);continue}for(;n.length&&n[n.length-1].value>=i.value;)n.pop();for(;n.length>=2;){const s=n[n.length-1],o=n[n.length-2],f=(i.value-o.value)/((i.rectangle.diagonal-o.rectangle.diagonal)*2);if(o.value+(s.rectangle.diagonal-o.rectangle.diagonal)/2*f<s.value)n.pop();else break}n.push(i)}}return n}}class Bt{constructor(n,e){w(this,"diagonal");w(this,"diagonalBucketIndex");w(this,"xLength");w(this,"yLength");w(this,"index");this.x=n,this.y=e,this.xLength=Math.pow(3,-n),this.yLength=Math.pow(3,-e),this.diagonal=Math.sqrt(this.xLength*this.xLength+this.yLength*this.yLength),this.diagonalBucketIndex=n+e,this.index=`${n},${e}`}}class Rt{constructor(n,e=1e-8,r=1e3,i=1e-6){w(this,"rectangles");w(this,"buckets");w(this,"fMin");w(this,"argMin");w(this,"tol");this.fcn=n,this.endTolerance=e,this.maxIterations=r,this.epsilon=i,this.fcn=n,this.epsilon=i,this.endTolerance=e,this.maxIterations=r,this.rectangles=new Map,this.buckets=new Ot;const s=[.5,.5],o=this.rect(0,0),f=this.fcn(s);this.buckets.addInterval(new y(s,f,o)),this.fMin=f,this.argMin=s,this.tol=o.diagonal}registerInterval(n){this.buckets.addInterval(n),n.value<=this.fMin&&(this.fMin=n.value,this.argMin=n.center,this.tol=n.rectangle.diagonal)}rect(n,e){const r=`${n},${e}`;return this.rectangles.has(r)||this.rectangles.set(r,new Bt(n,e)),this.rectangles.get(r)}splitInterval(n){let e,r,i;const[s,o]=n.center;return n.rectangle.x<=n.rectangle.y?(e=this.rect(n.rectangle.x+1,n.rectangle.y),r=[s-e.xLength,o],i=[s+e.xLength,o]):(e=this.rect(n.rectangle.x,n.rectangle.y+1),r=[s,o-e.yLength],i=[s,o+e.yLength]),[new y(r,this.fcn(r),e),new y(n.center,n.value,e),new y(i,this.fcn(i),e)]}single_iteration(){const n=this.buckets.getBottomRightHullIntervals();for(;n.length>=2;){const e=n[0],r=n[1],i=(r.value-e.value)/((r.rectangle.diagonal-e.rectangle.diagonal)/2),s=e.value-i*r.value/2;if((this.fMin-s)/Math.abs(this.fMin)<this.epsilon)n.shift();else break}n.forEach(e=>{this.buckets.removeInterval(e)});for(const e of n){const[r,i,s]=this.splitInterval(e);this.registerInterval(r),this.registerInterval(i),this.registerInterval(s)}}run(){let n=0;for(;this.tol>this.endTolerance/2&&(this.single_iteration(),n++,!(n>this.maxIterations)););return{fMin:this.fMin,argMin:this.argMin,tol:this.tol,iterations:n}}}class y{constructor(n,e,r){this.center=n,this.value=e,this.rectangle=r}}function Tt(t,n=1e-8,e=1e3,r=1e-6){return new Rt(t,n,e,r).run()}function zt(t,n,e=1e-9){const r=Tt(i=>{const s=t.paramPoint(i[0]),o=n.paramPoint(i[1]);return a.squareDistance(s,o)},e);return Math.sqrt(r.fMin)}function Vt(t,n){return t instanceof a.Line&&n instanceof a.Line?Et(t,n):t instanceof a.Line&&n instanceof a.Arc?nt(t,n):t instanceof a.Arc&&n instanceof a.Line?nt(n,t):t instanceof a.Arc&&n instanceof a.Arc?Ct(t,n):zt(t,n)}function ft(t){if(t instanceof a.CubicBezier){const i=a.subtract(t.lastPoint,t.firstPoint),s=a.subtract(t.firstControlPoint,t.firstPoint),o=a.subtract(t.lastControlPoint,t.firstPoint),f=a.angle(i,s),l=a.angle(i,o);if(f>0&&l<0||f<0&&l>0)return!1}const n=a.perpendicular(t.tangentAtFirstPoint),e=a.perpendicular(t.tangentAtLastPoint);let r=a.dotProduct(n,e);return Math.abs(Math.acos(r))<Math.PI/3}function V(t){if(ft(t))return[t];const[n,e]=t.splitAtParameters([.5]);return[...V(n),...V(e)]}function et(t){return ft(t)?[t]:t.splitAtParameters(t.getParametersOfExtrema()).flatMap(V)}function Gt(t){const n=t.deltaAngle,e=Math.ceil(Math.abs(n)/(Math.PI/2)),r=n/e,i=4*Math.tan(r/4)/3,s=t.clockwise?-1:1,o=Array.from({length:e+1},(l,u)=>t.firstAngle+u*r*s);return o.slice(0,-1).map((l,u)=>[l,o[u+1]]).map(([l,u])=>{const h=Math.cos(l),p=Math.sin(l),d=Math.cos(u),m=Math.sin(u),[g,S]=t.center,P=t.minorRadius,M=t.majorRadius,L=new a.TransformationMatrix().rotate(t.tiltAngle,t.center),A=At=>L.transform(At),C=A([g+M*h,S+P*p]),B=A([g+M*(h-i*p*s),S+P*(p+i*h*s)]),Mt=A([g+M*(d+i*m*s),S+P*(m-i*d*s)]),Lt=A([g+M*d,S+P*m]);return new a.CubicBezier(C,Lt,B,Mt)})}const X=1e-8,ut=t=>t.flatMap(n=>n instanceof a.Line?[n]:n instanceof a.Arc?[n]:n instanceof a.EllipseArc?Gt(n).flatMap(e=>et(e)):n instanceof a.QuadraticBezier||n instanceof a.CubicBezier?et(n):[new a.Line(n.firstPoint,n.lastPoint)]);function G(t,n,e=!0){const r=t.map(u=>({offset:b.offsetSegment(u,n),original:u})),i=[];let s=e?null:r.at(-1),o=e?r.at(-1):null;if(i.length===1)return i;const f=u=>{s?u.offset instanceof b.DegenerateSegment?a.sameVector(u.offset.firstPoint,u.offset.lastPoint)||i.push(new a.Line(u.offset.firstPoint,u.offset.lastPoint)):i.push(u.offset):s=u},l=function*(){for(const u of r.slice(0,-1))yield u;if(!s)throw new Error("Bug in the offset algorithm");yield s};for(const u of l()){if(!o){o=u;continue}const h=o.offset.lastPoint,p=u.offset.firstPoint;if(a.sameVector(h,p)){f(o),o=u;continue}let d=[];if(!(o.offset instanceof b.DegenerateSegment)&&!(u.offset instanceof b.DegenerateSegment)){const{intersections:P,overlaps:M}=c.findIntersectionsAndOverlaps(o.offset,u.offset,X/100);d=[...P,...M.flatMap(L=>[L.firstPoint,L.lastPoint])]}if(d.length>0){let P=d[0];if(d.length>1){const A=o==null?void 0:o.original.lastPoint,C=d.map(B=>a.squareDistance(B,A));P=d[C.indexOf(Math.min(...C))]}const M=o.offset.splitAt([P])[0],L=u.offset.splitAt([P]).at(-1);if(!L)throw new Error("Bug in the splitting algo in offset");f({offset:M,original:o.original}),o={offset:L,original:u.original};continue}const m=o.original.lastPoint,g=a.crossProduct(a.subtract(p,m),a.subtract(h,m))>0,S=new a.Arc(h,p,m,g);f(o),i.push(S),o=u}return o&&f(o),i}function lt(t){const n=new Map,e=(r,i)=>{const s=n.get(r)||[];n.set(r,[...s,...i])};return t.forEach((r,i)=>{t.slice(i+1).forEach((s,o)=>{const{intersections:f,overlaps:l}=c.findIntersectionsAndOverlaps(r,s,X),u=[...f,...l.flatMap(h=>[h.firstPoint,h.lastPoint])].filter(h=>{const p=a.sameVector(h,r.firstPoint)||a.sameVector(h,r.lastPoint),d=a.sameVector(h,s.firstPoint)||a.sameVector(h,s.lastPoint);return!(p&&d)});u.length&&(e(i,u),e(o+i+1,u))})}),n}function ht(t,n){return n.flatMap((e,r)=>{if(!t.has(r))return e;const i=t.get(r);return i?e.splitAt(i):e})}function gt(t,n,e){return t.filter(r=>!n.segments.some(s=>Vt(s,r)<Math.abs(e)-X))}function O(t,n){const e=t.clockwise?n:-n,r=ut(t.segments),i=G(r,e);if(i.length<2)return new c.Diagram;const s=lt(i);if(!s.size){const h=new c.Loop(i);return new c.Diagram([new c.Figure(h)])}const o=ht(s,i),f=gt(o,t,n);if(!f.length)return new c.Diagram;const u=c.stitchSegments(f).filter(h=>h.length>1).filter(h=>a.sameVector(h[0].firstPoint,h.at(-1).lastPoint)).map(h=>new c.Loop(h));return u.length?new c.Diagram(u.map(h=>new c.Figure(h))):new c.Diagram}function _t(t,n,e="round"){const r=n/2,i=ut(t.segments),s=G(i,r,!1),o=G(i,-r,!1).map(g=>g.reverse());o.reverse();const f=(g,S)=>e==="round"?a.tangentArc(g.lastPoint,S.firstPoint,g.tangentAtLastPoint):new a.Line(g.lastPoint,S.firstPoint),l=[...s,f(s[s.length-1],o[0]),...o,f(o[o.length-1],s[0])],u=lt(l);if(!u.size){const g=new c.Loop(l);return new c.Diagram([new c.Figure(g)])}const h=ht(u,l),p=gt(h,t,r);if(!p.length)return new c.Diagram;const m=c.stitchSegments(p).filter(g=>g.length>1).filter(g=>a.sameVector(g[0].firstPoint,g.at(-1).lastPoint)).map(g=>new c.Loop(g));return m.length?new c.Diagram(m.map(g=>new c.Figure(g))):new c.Diagram}function Ut(t,n){const e=t.map(r=>{const i=E(r.holes.map(s=>O(s,n)));return J(O(r.contour,n),i)});return E(e)}function $t(t,n){const e=Math.abs(n/2),r=t.map(i=>E(i.allLoops.map(s=>J(O(s,e),O(s,-e)))));return E(r)}function qt(t,n){return Ut(I(t),n)}function Nt(t,n,{endCap:e="round"}={}){return t instanceof c.Strand?_t(t,n,e):$t(I(t),n)}class Ht{constructor(){w(this,"filters");this.filters=[]}delete(){this.filters=[]}and(n){return n.forEach(e=>e(this)),this}not(n){const e=new this.constructor;n(e);const r=({element:i})=>!e.shouldKeep(i);return this.filters.push(r),this}either(n){const e=n.map(i=>{const s=new this.constructor;return i(s),s}),r=({element:i})=>e.some(s=>s.shouldKeep(i));return this.filters.push(r),this}}const R=2*Math.PI,rt=t=>{const n=t%R,e=n<0?n+R:n;return e<Math.PI?e:e===Math.PI?0:Math.abs(e-R)};class k extends Ht{clone(){const n=new k;return n.filters=[...this.filters],n}inList(n){const e=({element:r})=>!!n.find(i=>a.sameVector(i,r.point));return this.filters.push(e),this}atDistance(n,e=[0,0]){function r({element:i}){return Math.abs(a.distance(e,i.point)-n)<1e-9}return this.filters.push(r),this}atPoint(n){function e({element:r}){return a.sameVector(n,r.point)}return this.filters.push(e),this}inBox(n,e){const[r,i]=n,[s,o]=e,f=Math.min(r,s),l=Math.max(r,s),u=Math.min(i,o),h=Math.max(i,o);function p({element:d}){const[m,g]=d.point;return m>=f&&m<=l&&g>=u&&g<=h}return this.filters.push(p),this}ofAngle(n){function e({element:r}){const i=r.firstCurve.tangentAtLastPoint,s=r.secondCurve.tangentAtFirstPoint;return Math.abs(rt(a.angle(i,s))-rt(a.DEG2RAD*n))<1e-9}return this.filters.push(e),this}above(n=0){function e({element:r}){return r.point[1]>n}return this.filters.push(e),this}below(n=0){function e({element:r}){return r.point[1]<n}return this.filters.push(e),this}leftOf(n=0){function e({element:r}){return r.point[0]<n}return this.filters.push(e),this}rightOf(n=0){function e({element:r}){return r.point[0]>n}return this.filters.push(e),this}shouldKeep(n){return this.filters.every(r=>r({element:n}))}asFilterFun(){return this.shouldKeep.bind(this)}}function pt(t,n,e,r=()=>!0){const i=[n.segments[0]],s=(f,l)=>{r({firstCurve:f,secondCurve:l,point:f.lastPoint})?i.push(...t(f,l,e)):i.push(f,l)};if(n.segments.slice(1).forEach(f=>{const l=i.pop();if(!l)throw new Error("Bug in the stroke filletting algo");s(l,f)}),!i.at(-1))throw new Error("Bug in the stroke corner algo");if(n instanceof c.Loop){const f=i.pop(),l=i.shift();if(!f||!l)throw new Error("Bug in the filletting algo");return s(f,l),new c.Loop(i,{ignoreChecks:!0})}else return new c.Strand(i,{ignoreChecks:!0})}function v(t,n,e){const r=typeof e=="function"?e(new k):e,i=r&&r.asFilterFun();if(t instanceof c.Loop||t instanceof c.Strand)return pt(b.filletSegments,t,n,i);if(t instanceof c.Figure){const s=v(t.contour,n,r),o=t.holes.map(f=>v(f,n,r));return new c.Figure(s,o,{ignoreChecks:!0})}if(t instanceof c.Diagram){const s=t.figures.map(o=>v(o,n,r));return new c.Diagram(s,{ignoreChecks:!0})}throw new Error("invalid shape to fillet")}function F(t,n,e){const r=typeof e=="function"?e(new k):e,i=r&&r.asFilterFun();if(t instanceof c.Loop||t instanceof c.Strand)return pt(b.filletSegments,t,n,i);if(t instanceof c.Figure){const s=F(t.contour,n,r),o=t.holes.map(f=>F(f,n,r));return new c.Figure(s,o,{ignoreChecks:!0})}if(t instanceof c.Diagram){const s=t.figures.map(o=>F(o,n,r));return new c.Diagram(s,{ignoreChecks:!0})}throw new Error("invalid shape to chamfer")}class Kt extends k{constructor(n){super(),this.shape=n}fillet(n){return v(this.shape,n,this.clone())}chamfer(n){return F(this.shape,n,this.clone())}}function Jt(t){return new Kt(t)}const Xt=1*a.DEG2RAD,Qt=24;function T(t,n,e,r){const i=t.gradientAt(n);return a.length(i)<=r?e:a.normalize(i)}function _(t,n,e,r,i,s){const o=t.paramPoint(n),f=t.paramPoint(e),l=a.subtract(f,o),u=a.length(l);if(u<=t.precision||s>=r.maxDepth)return[o,f];const h=[l[0]/u,l[1]/u],p=(n+e)*.5,d=T(t,n,h,t.precision),m=T(t,p,h,t.precision),g=T(t,e,h,t.precision),S=a.dotProduct(d,m),P=a.dotProduct(m,g);if(S>=i&&P>=i)return[o,f];const M=t.paramPoint(p),L=S>=i?[o,M]:_(t,n,p,r,i,s+1),A=P>=i?[M,f]:_(t,p,e,r,i,s+1);return[...L.slice(0,-1),...A]}function Q(t,n={}){const e={maxAngle:n.maxAngle??Xt,maxDepth:n.maxDepth??Qt},r=Math.cos(e.maxAngle);return _(t,0,1,e,r,0)}function Yt(t,n){return new c.Diagram(t.figures.map(e=>dt(e,n)))}function dt(t,n){return new c.Figure(U(t.contour,n),t.holes.map(e=>U(e,n)))}function U(t,n){return new c.Loop(Y(t.segments,n))}function Zt(t,n){return new c.Strand(Y(t.segments,n))}function Y(t,n){return t.flatMap(n)}function Wt(t,n){if(t instanceof c.Diagram)return Yt(t,n);if(t instanceof c.Figure)return dt(t,n);if(t instanceof c.Loop)return U(t,n);if(t instanceof c.Strand)return Zt(t,n);if(c.isSegment(t))return Y([t],n);throw new Error("Unsupported shape type")}function jt(t){if(t.length<2)return[];const n=[];for(let e=0;e<t.length-1;e+=1)n.push(new a.Line(t[e],t[e+1]));return n}function tn(t,n){return jt(Q(t,n))}function x(t,n){const e=[];return t.forEach((r,i)=>{const s=Q(r,n);if(s.length!==0){if(i===0){e.push(...s);return}e.push(...s.slice(1))}}),e.length>1&&a.sameVector(e[0],e[e.length-1])&&e.pop(),e}function nn(t){return t.length<3?!1:t.map((e,r)=>{const i=t[(r+1)%t.length];return(i[0]-e[0])*(i[1]+e[1])}).reduce((e,r)=>e+r,0)>0}function z(t,n){return t.length<3||nn(t)===n?t:[...t].reverse()}function en(t,n={}){return Wt(t,r=>tn(r,n))}function mt(t,n={}){if(t instanceof c.Diagram)return t.figures.map(e=>mt(e,n));if(t instanceof c.Figure){const e=z(x(t.contour.segments,n),!1),r=t.holes.map(i=>z(x(i.segments,n),!0));return[e,...r]}return t instanceof c.Loop?z(x(t.segments,n),!1):t instanceof c.Strand?x(t.segments,n):Q(t,n)}function $(t){if(t instanceof c.Diagram)return D.svgDiagram(t);if(t instanceof c.Figure)return D.svgFigure(t);if(t instanceof c.Loop)return`<path d="${D.svgLoop(t)}" />`;if(t instanceof c.Strand)return`<path d="${D.svgStrand(t)}" />`;if(c.isSegment(t))return`<path d="${`M ${t.firstPoint.join(" ")}`} ${D.svgSegmentToPath(t)}" />`;throw new Error("Unknown shape type")}const it=t=>"shape"in t?t.shape:t,st=(t,n)=>{if(!("shape"in t))return n;const{color:e}=t;return e?`<g stroke="${e}">${n}</g>`:n},ot=t=>new a.BoundingBox(t.xMin,-t.yMax,t.xMax,-t.yMin);function rn(t,{margin:n=1,unit:e=null,viewBox:r}={}){if(Array.isArray(t)){const s=t.map(l=>it(l).mirror()),o=s.map((l,u)=>st(t[u],$(l))).join(`
|
|
2
|
-
`),
|
|
1
|
+
"use strict";var At=Object.defineProperty;var bt=(t,n,e)=>n in t?At(t,n,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[n]=e;var w=(t,n,e)=>bt(t,typeof n!="symbol"?n+"":n,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A=require("./draw-BmJ8wIc9.cjs"),c=require("./models-Fv9xfQBT.cjs"),a=require("./QuadraticBezier-eSR75IzC.cjs"),D=require("./svg-b6SOnbda.cjs");function b(t){if(c.Figure.isInstance(t))return[t];if(c.Loop.isInstance(t))return[new c.Figure(t)];if(c.Diagram.isInstance(t))return t.figures;throw new Error("Unknown shape")}function ct(t,n,e=1e-9){let r=[];const i=[],s=new Array(n.segments.length).fill(0).map(()=>[]);n.segments.forEach((u,l)=>{t.segments.forEach(f=>{const{intersections:h,overlaps:p}=c.findIntersectionsAndOverlaps(u,f,e);r.push(...h),s[l].push(...h),i.push(...p);const d=p.flatMap(m=>[m.firstPoint,m.lastPoint]);r.push(...d),s[l].push(...d)})}),r=a.removeDuplicatePoints(r,e);const o=a.zip([n.segments,s]).flatMap(([u,l])=>l.length?u.splitAt(l):[u]);return Array.from(c.strandsBetweenIntersections(o,r,i))}function H(t,n,e=!1){return ct(n,t).filter(i=>{const s=i.segments[0].midPoint;return n.onStroke(s)?!e:!n.contains(s)})}function K(t,n,e=!1){return ct(n,t).filter(i=>{const s=i.segments[0].midPoint;return n.onStroke(s)?!e:n.contains(s)})}function Z(t,n,e=!1){const r=H(t,n.contour,e),i=n.holes.flatMap(s=>K(t,s,e));return[...r,...i]}function W(t,n,e=!1){let r=K(t,n.contour,e);return n.holes.forEach(i=>{r=r.flatMap(s=>H(s,i,e))}),r}function at(t,n){return new c.Diagram(c.fuseFiguresLists(b(t),b(n)))}function E(t){return t.reduce((n,e)=>at(n,e),new c.Diagram)}function J(t,n){return new c.Diagram(c.cutFiguresLists(b(t),b(n)))}function Dt(t,n){return new c.Diagram(c.intersectFiguresLists(b(t),b(n)))}function vt(t,n,e=!0){if(c.Loop.isInstance(n))return H(t,n,e);if(c.Figure.isInstance(n))return Z(t,n,e);let r=[new c.Strand([...t.segments])];return n.figures.forEach(i=>{r=r.flatMap(s=>Z(s,i,e))}),r}function Ft(t,n,e=!1){if(c.Loop.isInstance(n))return K(t,n,e);if(c.Figure.isInstance(n))return W(t,n,e);let r=[new c.Strand([...t.segments])];return n.figures.forEach(i=>{r=r.flatMap(s=>W(s,i,e))}),r}function j(t){return t<0?"before":t>1?"after":"between"}const tt=(t,n,e)=>{if(e==="before")return t.distanceFrom(n.firstPoint);if(e==="after")return t.distanceFrom(n.lastPoint);throw new Error("Invalid position")};function Et(t,n){const e=a.lineLineParams(t,n);if(e==="parallel")return Math.min(t.distanceFrom(n.firstPoint),t.distanceFrom(n.lastPoint));const{intersectionParam1:r,intersectionParam2:i}=e,s=j(r),o=j(i);if(s==="between"&&o==="between")return 0;if(s==="between"&&o!=="between")return tt(t,n,o);if(o==="between"&&s!=="between")return tt(n,t,s);if(s==="before"&&o==="before")return a.distance(t.firstPoint,n.firstPoint);if(s==="after"&&o==="after")return a.distance(t.lastPoint,n.lastPoint);if(s==="before"&&o==="after")return a.distance(t.firstPoint,n.lastPoint);if(s==="after"&&o==="before")return a.distance(t.lastPoint,n.firstPoint);throw new Error("Invalid position")}function nt(t,n){if(c.lineArcIntersection(t,n).length>0)return 0;const e=c.projectPointOnLine(t,n.center);if(t.isOnSegment(e)){const r=a.distance(e,n.center);if(Math.abs(r-n.radius)<t.precision&&n.isOnSegment(e))return 0;if(r-n.radius>t.precision){const i=a.normalize(a.subtract(e,n.center)),s=a.add(n.center,a.scalarMultiply(i,n.radius));if(n.isOnSegment(s))return a.distance(s,e)}}return Math.min(n.distanceFrom(t.firstPoint),n.distanceFrom(t.lastPoint),t.distanceFrom(n.firstPoint),t.distanceFrom(n.lastPoint))}const kt=(t,n)=>{const e=t.angleToParam(n.firstAngle);if(t.isValidParameter(e))return!0;const r=t.angleToParam(n.lastAngle);return!!t.isValidParameter(r)};function Ct(t,n){if(c.arcArcIntersection(t,n,!0).length>0)return 0;const e=a.distance(t.center,n.center);if(e<t.precision&&kt(t,n))return Math.abs(t.radius-n.radius);const r=a.normalize(a.subtract(n.center,t.center)),i=e-Math.abs(t.radius-n.radius)<t.precision;let s=a.polarAngle(r);i&&n.radius>t.radius&&(s+=Math.PI);const o=i?s:s+Math.PI,u=t.angleToParam(s),l=n.angleToParam(o);return t.isValidParameter(u)&&n.isValidParameter(l)?a.distance(t.paramPoint(u),n.paramPoint(l)):Math.min(t.distanceFrom(n.firstPoint),t.distanceFrom(n.lastPoint),n.distanceFrom(t.firstPoint),n.distanceFrom(t.lastPoint))}function yt(t,n,e){let r=0,i=t.length-1;for(;r<=i;){const s=Math.floor((r+i)/2),o=e(t[s],n);if(o<0)r=s+1;else if(o>0)i=s-1;else return s}return-(r+1)}function xt(t,n,e){const r=yt(t,n,e);r<0?t.splice(-(r+1),0,n):t.splice(r,0,n)}class Ot{constructor(){w(this,"buckets");this.buckets=[]}addInterval(n){const e=this.buckets[n.rectangle.diagonalBucketIndex];e===void 0?this.buckets[n.rectangle.diagonalBucketIndex]=[n]:xt(e,n,(r,i)=>r.value-i.value)}removeInterval(n){const e=this.buckets[n.rectangle.diagonalBucketIndex];if(e===void 0)throw new Error("Interval not found");e.shift()}getBottomRightHullIntervals(){const n=[];for(let e=this.buckets.length-1;e>=0;e--){const r=this.buckets[e];if(!r===void 0)continue;const i=r[0];if(i!==void 0){if(!n.length){n.push(i);continue}for(;n.length&&n[n.length-1].value>=i.value;)n.pop();for(;n.length>=2;){const s=n[n.length-1],o=n[n.length-2],u=(i.value-o.value)/((i.rectangle.diagonal-o.rectangle.diagonal)*2);if(o.value+(s.rectangle.diagonal-o.rectangle.diagonal)/2*u<s.value)n.pop();else break}n.push(i)}}return n}}class Bt{constructor(n,e){w(this,"diagonal");w(this,"diagonalBucketIndex");w(this,"xLength");w(this,"yLength");w(this,"index");this.x=n,this.y=e,this.xLength=Math.pow(3,-n),this.yLength=Math.pow(3,-e),this.diagonal=Math.sqrt(this.xLength*this.xLength+this.yLength*this.yLength),this.diagonalBucketIndex=n+e,this.index=`${n},${e}`}}class Rt{constructor(n,e=1e-8,r=1e3,i=1e-6){w(this,"rectangles");w(this,"buckets");w(this,"fMin");w(this,"argMin");w(this,"tol");this.fcn=n,this.endTolerance=e,this.maxIterations=r,this.epsilon=i,this.fcn=n,this.epsilon=i,this.endTolerance=e,this.maxIterations=r,this.rectangles=new Map,this.buckets=new Ot;const s=[.5,.5],o=this.rect(0,0),u=this.fcn(s);this.buckets.addInterval(new y(s,u,o)),this.fMin=u,this.argMin=s,this.tol=o.diagonal}registerInterval(n){this.buckets.addInterval(n),n.value<=this.fMin&&(this.fMin=n.value,this.argMin=n.center,this.tol=n.rectangle.diagonal)}rect(n,e){const r=`${n},${e}`;return this.rectangles.has(r)||this.rectangles.set(r,new Bt(n,e)),this.rectangles.get(r)}splitInterval(n){let e,r,i;const[s,o]=n.center;return n.rectangle.x<=n.rectangle.y?(e=this.rect(n.rectangle.x+1,n.rectangle.y),r=[s-e.xLength,o],i=[s+e.xLength,o]):(e=this.rect(n.rectangle.x,n.rectangle.y+1),r=[s,o-e.yLength],i=[s,o+e.yLength]),[new y(r,this.fcn(r),e),new y(n.center,n.value,e),new y(i,this.fcn(i),e)]}single_iteration(){const n=this.buckets.getBottomRightHullIntervals();for(;n.length>=2;){const e=n[0],r=n[1],i=(r.value-e.value)/((r.rectangle.diagonal-e.rectangle.diagonal)/2),s=e.value-i*r.value/2;if((this.fMin-s)/Math.abs(this.fMin)<this.epsilon)n.shift();else break}n.forEach(e=>{this.buckets.removeInterval(e)});for(const e of n){const[r,i,s]=this.splitInterval(e);this.registerInterval(r),this.registerInterval(i),this.registerInterval(s)}}run(){let n=0;for(;this.tol>this.endTolerance/2&&(this.single_iteration(),n++,!(n>this.maxIterations)););return{fMin:this.fMin,argMin:this.argMin,tol:this.tol,iterations:n}}}class y{constructor(n,e,r){this.center=n,this.value=e,this.rectangle=r}}function Tt(t,n=1e-8,e=1e3,r=1e-6){return new Rt(t,n,e,r).run()}function zt(t,n,e=1e-9){const r=Tt(i=>{const s=t.paramPoint(i[0]),o=n.paramPoint(i[1]);return a.squareDistance(s,o)},e);return Math.sqrt(r.fMin)}function Vt(t,n){return a.Line.isInstance(t)&&a.Line.isInstance(n)?Et(t,n):a.Line.isInstance(t)&&a.Arc.isInstance(n)?nt(t,n):a.Arc.isInstance(t)&&a.Line.isInstance(n)?nt(n,t):a.Arc.isInstance(t)&&a.Arc.isInstance(n)?Ct(t,n):zt(t,n)}function ut(t){if(a.CubicBezier.isInstance(t)){const i=a.subtract(t.lastPoint,t.firstPoint),s=a.subtract(t.firstControlPoint,t.firstPoint),o=a.subtract(t.lastControlPoint,t.firstPoint),u=a.angle(i,s),l=a.angle(i,o);if(u>0&&l<0||u<0&&l>0)return!1}const n=a.perpendicular(t.tangentAtFirstPoint),e=a.perpendicular(t.tangentAtLastPoint);let r=a.dotProduct(n,e);return Math.abs(Math.acos(r))<Math.PI/3}function V(t){if(ut(t))return[t];const[n,e]=t.splitAtParameters([.5]);return[...V(n),...V(e)]}function et(t){return ut(t)?[t]:t.splitAtParameters(t.getParametersOfExtrema()).flatMap(V)}function Gt(t){const n=t.deltaAngle,e=Math.ceil(Math.abs(n)/(Math.PI/2)),r=n/e,i=4*Math.tan(r/4)/3,s=t.clockwise?-1:1,o=Array.from({length:e+1},(l,f)=>t.firstAngle+f*r*s);return o.slice(0,-1).map((l,f)=>[l,o[f+1]]).map(([l,f])=>{const h=Math.cos(l),p=Math.sin(l),d=Math.cos(f),m=Math.sin(f),[g,I]=t.center,P=t.minorRadius,S=t.majorRadius,M=new a.TransformationMatrix().rotate(t.tiltAngle,t.center),L=Lt=>M.transform(Lt),C=L([g+S*h,I+P*p]),B=L([g+S*(h-i*p*s),I+P*(p+i*h*s)]),St=L([g+S*(d+i*m*s),I+P*(m-i*d*s)]),Mt=L([g+S*d,I+P*m]);return new a.CubicBezier(C,Mt,B,St)})}const X=1e-8,ft=t=>t.flatMap(n=>a.Line.isInstance(n)?[n]:a.Arc.isInstance(n)?[n]:a.EllipseArc.isInstance(n)?Gt(n).flatMap(e=>et(e)):a.QuadraticBezier.isInstance(n)||a.CubicBezier.isInstance(n)?et(n):[new a.Line(n.firstPoint,n.lastPoint)]);function G(t,n,e=!0){const r=t.map(f=>({offset:A.offsetSegment(f,n),original:f})),i=[];let s=e?null:r.at(-1),o=e?r.at(-1):null;if(i.length===1)return i;const u=f=>{s?A.DegenerateSegment.isInstance(f.offset)?a.sameVector(f.offset.firstPoint,f.offset.lastPoint)||i.push(new a.Line(f.offset.firstPoint,f.offset.lastPoint)):i.push(f.offset):s=f},l=function*(){for(const f of r.slice(0,-1))yield f;if(!s)throw new Error("Bug in the offset algorithm");yield s};for(const f of l()){if(!o){o=f;continue}const h=o.offset.lastPoint,p=f.offset.firstPoint;if(a.sameVector(h,p)){u(o),o=f;continue}let d=[];if(!A.DegenerateSegment.isInstance(o.offset)&&!A.DegenerateSegment.isInstance(f.offset)){const{intersections:P,overlaps:S}=c.findIntersectionsAndOverlaps(o.offset,f.offset,X/100);d=[...P,...S.flatMap(M=>[M.firstPoint,M.lastPoint])]}if(d.length>0){let P=d[0];if(d.length>1){const L=o==null?void 0:o.original.lastPoint,C=d.map(B=>a.squareDistance(B,L));P=d[C.indexOf(Math.min(...C))]}const S=o.offset.splitAt([P])[0],M=f.offset.splitAt([P]).at(-1);if(!M)throw new Error("Bug in the splitting algo in offset");u({offset:S,original:o.original}),o={offset:M,original:f.original};continue}const m=o.original.lastPoint,g=a.crossProduct(a.subtract(p,m),a.subtract(h,m))>0,I=new a.Arc(h,p,m,g);u(o),i.push(I),o=f}return o&&u(o),i}function lt(t){const n=new Map,e=(r,i)=>{const s=n.get(r)||[];n.set(r,[...s,...i])};return t.forEach((r,i)=>{t.slice(i+1).forEach((s,o)=>{const{intersections:u,overlaps:l}=c.findIntersectionsAndOverlaps(r,s,X),f=[...u,...l.flatMap(h=>[h.firstPoint,h.lastPoint])].filter(h=>{const p=a.sameVector(h,r.firstPoint)||a.sameVector(h,r.lastPoint),d=a.sameVector(h,s.firstPoint)||a.sameVector(h,s.lastPoint);return!(p&&d)});f.length&&(e(i,f),e(o+i+1,f))})}),n}function ht(t,n){return n.flatMap((e,r)=>{if(!t.has(r))return e;const i=t.get(r);return i?e.splitAt(i):e})}function gt(t,n,e){return t.filter(r=>!n.segments.some(s=>Vt(s,r)<Math.abs(e)-X))}function O(t,n){const e=t.clockwise?n:-n,r=ft(t.segments),i=G(r,e);if(i.length<2)return new c.Diagram;const s=lt(i);if(!s.size){const h=new c.Loop(i);return new c.Diagram([new c.Figure(h)])}const o=ht(s,i),u=gt(o,t,n);if(!u.length)return new c.Diagram;const f=c.stitchSegments(u).filter(h=>h.length>1).filter(h=>a.sameVector(h[0].firstPoint,h.at(-1).lastPoint)).map(h=>new c.Loop(h));return f.length?new c.Diagram(f.map(h=>new c.Figure(h))):new c.Diagram}function _t(t,n,e="round"){const r=n/2,i=ft(t.segments),s=G(i,r,!1),o=G(i,-r,!1).map(g=>g.reverse());o.reverse();const u=(g,I)=>e==="round"?a.tangentArc(g.lastPoint,I.firstPoint,g.tangentAtLastPoint):new a.Line(g.lastPoint,I.firstPoint),l=[...s,u(s[s.length-1],o[0]),...o,u(o[o.length-1],s[0])],f=lt(l);if(!f.size){const g=new c.Loop(l);return new c.Diagram([new c.Figure(g)])}const h=ht(f,l),p=gt(h,t,r);if(!p.length)return new c.Diagram;const m=c.stitchSegments(p).filter(g=>g.length>1).filter(g=>a.sameVector(g[0].firstPoint,g.at(-1).lastPoint)).map(g=>new c.Loop(g));return m.length?new c.Diagram(m.map(g=>new c.Figure(g))):new c.Diagram}function Ut(t,n){const e=t.map(r=>{const i=E(r.holes.map(s=>O(s,n)));return J(O(r.contour,n),i)});return E(e)}function $t(t,n){const e=Math.abs(n/2),r=t.map(i=>E(i.allLoops.map(s=>J(O(s,e),O(s,-e)))));return E(r)}function qt(t,n){return Ut(b(t),n)}function Nt(t,n,{endCap:e="round"}={}){return c.Strand.isInstance(t)?_t(t,n,e):$t(b(t),n)}class Ht{constructor(){w(this,"filters");this.filters=[]}delete(){this.filters=[]}and(n){return n.forEach(e=>e(this)),this}not(n){const e=new this.constructor;n(e);const r=({element:i})=>!e.shouldKeep(i);return this.filters.push(r),this}either(n){const e=n.map(i=>{const s=new this.constructor;return i(s),s}),r=({element:i})=>e.some(s=>s.shouldKeep(i));return this.filters.push(r),this}}const R=2*Math.PI,rt=t=>{const n=t%R,e=n<0?n+R:n;return e<Math.PI?e:e===Math.PI?0:Math.abs(e-R)};class k extends Ht{clone(){const n=new k;return n.filters=[...this.filters],n}inList(n){const e=({element:r})=>!!n.find(i=>a.sameVector(i,r.point));return this.filters.push(e),this}atDistance(n,e=[0,0]){function r({element:i}){return Math.abs(a.distance(e,i.point)-n)<1e-9}return this.filters.push(r),this}atPoint(n){function e({element:r}){return a.sameVector(n,r.point)}return this.filters.push(e),this}inBox(n,e){const[r,i]=n,[s,o]=e,u=Math.min(r,s),l=Math.max(r,s),f=Math.min(i,o),h=Math.max(i,o);function p({element:d}){const[m,g]=d.point;return m>=u&&m<=l&&g>=f&&g<=h}return this.filters.push(p),this}ofAngle(n){function e({element:r}){const i=r.firstCurve.tangentAtLastPoint,s=r.secondCurve.tangentAtFirstPoint;return Math.abs(rt(a.angle(i,s))-rt(a.DEG2RAD*n))<1e-9}return this.filters.push(e),this}above(n=0){function e({element:r}){return r.point[1]>n}return this.filters.push(e),this}below(n=0){function e({element:r}){return r.point[1]<n}return this.filters.push(e),this}leftOf(n=0){function e({element:r}){return r.point[0]<n}return this.filters.push(e),this}rightOf(n=0){function e({element:r}){return r.point[0]>n}return this.filters.push(e),this}shouldKeep(n){return this.filters.every(r=>r({element:n}))}asFilterFun(){return this.shouldKeep.bind(this)}}function pt(t,n,e,r=()=>!0){const i=[n.segments[0]],s=(u,l)=>{r({firstCurve:u,secondCurve:l,point:u.lastPoint})?i.push(...t(u,l,e)):i.push(u,l)};if(n.segments.slice(1).forEach(u=>{const l=i.pop();if(!l)throw new Error("Bug in the stroke filletting algo");s(l,u)}),!i.at(-1))throw new Error("Bug in the stroke corner algo");if(c.Loop.isInstance(n)){const u=i.pop(),l=i.shift();if(!u||!l)throw new Error("Bug in the filletting algo");return s(u,l),new c.Loop(i,{ignoreChecks:!0})}else return new c.Strand(i,{ignoreChecks:!0})}function v(t,n,e){const r=typeof e=="function"?e(new k):e,i=r&&r.asFilterFun();if(c.Loop.isInstance(t)||c.Strand.isInstance(t))return pt(A.filletSegments,t,n,i);if(c.Figure.isInstance(t)){const s=v(t.contour,n,r),o=t.holes.map(u=>v(u,n,r));return new c.Figure(s,o,{ignoreChecks:!0})}if(c.Diagram.isInstance(t)){const s=t.figures.map(o=>v(o,n,r));return new c.Diagram(s,{ignoreChecks:!0})}throw new Error("invalid shape to fillet")}function F(t,n,e){const r=typeof e=="function"?e(new k):e,i=r&&r.asFilterFun();if(c.Loop.isInstance(t)||c.Strand.isInstance(t))return pt(A.filletSegments,t,n,i);if(c.Figure.isInstance(t)){const s=F(t.contour,n,r),o=t.holes.map(u=>F(u,n,r));return new c.Figure(s,o,{ignoreChecks:!0})}if(c.Diagram.isInstance(t)){const s=t.figures.map(o=>F(o,n,r));return new c.Diagram(s,{ignoreChecks:!0})}throw new Error("invalid shape to chamfer")}class Kt extends k{constructor(n){super(),this.shape=n}fillet(n){return v(this.shape,n,this.clone())}chamfer(n){return F(this.shape,n,this.clone())}}function Jt(t){return new Kt(t)}const Xt=1*a.DEG2RAD,Qt=24;function T(t,n,e,r){const i=t.gradientAt(n);return a.length(i)<=r?e:a.normalize(i)}function _(t,n,e,r,i,s){const o=t.paramPoint(n),u=t.paramPoint(e),l=a.subtract(u,o),f=a.length(l);if(f<=t.precision||s>=r.maxDepth)return[o,u];const h=[l[0]/f,l[1]/f],p=(n+e)*.5,d=T(t,n,h,t.precision),m=T(t,p,h,t.precision),g=T(t,e,h,t.precision),I=a.dotProduct(d,m),P=a.dotProduct(m,g);if(I>=i&&P>=i)return[o,u];const S=t.paramPoint(p),M=I>=i?[o,S]:_(t,n,p,r,i,s+1),L=P>=i?[S,u]:_(t,p,e,r,i,s+1);return[...M.slice(0,-1),...L]}function Q(t,n={}){const e={maxAngle:n.maxAngle??Xt,maxDepth:n.maxDepth??Qt},r=Math.cos(e.maxAngle);return _(t,0,1,e,r,0)}function Yt(t,n){return new c.Diagram(t.figures.map(e=>dt(e,n)))}function dt(t,n){return new c.Figure(U(t.contour,n),t.holes.map(e=>U(e,n)))}function U(t,n){return new c.Loop(Y(t.segments,n))}function Zt(t,n){return new c.Strand(Y(t.segments,n))}function Y(t,n){return t.flatMap(n)}function Wt(t,n){if(c.Diagram.isInstance(t))return Yt(t,n);if(c.Figure.isInstance(t))return dt(t,n);if(c.Loop.isInstance(t))return U(t,n);if(c.Strand.isInstance(t))return Zt(t,n);if(c.isSegment(t))return Y([t],n);throw new Error("Unsupported shape type")}function jt(t){if(t.length<2)return[];const n=[];for(let e=0;e<t.length-1;e+=1)n.push(new a.Line(t[e],t[e+1]));return n}function tn(t,n){return jt(Q(t,n))}function x(t,n){const e=[];return t.forEach((r,i)=>{const s=Q(r,n);if(s.length!==0){if(i===0){e.push(...s);return}e.push(...s.slice(1))}}),e.length>1&&a.sameVector(e[0],e[e.length-1])&&e.pop(),e}function nn(t){return t.length<3?!1:t.map((e,r)=>{const i=t[(r+1)%t.length];return(i[0]-e[0])*(i[1]+e[1])}).reduce((e,r)=>e+r,0)>0}function z(t,n){return t.length<3||nn(t)===n?t:[...t].reverse()}function en(t,n={}){return Wt(t,r=>tn(r,n))}function mt(t,n={}){if(c.Diagram.isInstance(t))return t.figures.flatMap(e=>mt(e,n));if(c.Figure.isInstance(t)){const e=z(x(t.contour.segments,n),!1),r=t.holes.map(i=>z(x(i.segments,n),!0));return[e,...r]}return c.Loop.isInstance(t)?[z(x(t.segments,n),!1)]:c.Strand.isInstance(t)?[x(t.segments,n)]:[Q(t,n)]}function $(t){if(c.Diagram.isInstance(t))return D.svgDiagram(t);if(c.Figure.isInstance(t))return D.svgFigure(t);if(c.Loop.isInstance(t))return`<path d="${D.svgLoop(t)}" />`;if(c.Strand.isInstance(t))return`<path d="${D.svgStrand(t)}" />`;if(c.isSegment(t))return`<path d="${`M ${t.firstPoint.join(" ")}`} ${D.svgSegmentToPath(t)}" />`;throw new Error("Unknown shape type")}const it=t=>"shape"in t?t.shape:t,st=(t,n)=>{if(!("shape"in t))return n;const{color:e}=t;return e?`<g stroke="${e}">${n}</g>`:n},ot=t=>new a.BoundingBox(t.xMin,-t.yMax,t.xMax,-t.yMin);function rn(t,{margin:n=1,unit:e=null,viewBox:r}={}){if(Array.isArray(t)){const s=t.map(l=>it(l).mirror()),o=s.map((l,f)=>st(t[f],$(l))).join(`
|
|
2
|
+
`),u=s.slice(1).reduce((l,f)=>l.merge(f.boundingBox),s[0].boundingBox);return D.wrapSVG(o,r?ot(r):u,n,e)}const i=it(t).mirror();return D.wrapSVG(st(t,$(i)),r?ot(r):i.boundingBox,n,e)}const Pt=t=>{if(t.type==="LINE")return new a.Line(t.firstPoint,t.lastPoint);if(t.type==="ARC")return new a.Arc(t.firstPoint,t.lastPoint,t.center,t.clockwise);if(t.type==="ELLIPSE_ARC")return new a.EllipseArc(t.firstPoint,t.lastPoint,t.center,t.majorRadius,t.minorRadius,t.tiltAngle,t.clockwise,{angleUnits:"rad"});if(t.type==="QUADRATIC_BEZIER")return new a.QuadraticBezier(t.firstPoint,t.lastPoint,t.controlPoint);if(t.type==="CUBIC_BEZIER")return new a.CubicBezier(t.firstPoint,t.lastPoint,t.firstControlPoint,t.lastControlPoint);throw new Error("Unknown segment type")},q=t=>{const n=t.segments.map(Pt);return new c.Loop(n)},wt=t=>{const n=q(t.contour),e=t.holes.map(q);return new c.Figure(n,e)},sn=t=>{const n=t.figures.map(wt);return new c.Diagram(n)};function on(t){if(t.type==="DIAGRAM")return sn(t);if(t.type==="FIGURE")return wt(t);if(t.type==="LOOP")return q(t);if(t.type==="LINE"||t.type==="ARC"||t.type==="ELLIPSE_ARC"||t.type==="CUBIC_BEZIER")return Pt(t);throw new Error("Unknown shape type")}const N=Math.PI/180,It=180/Math.PI;function cn(t,n){const e=Math.cos(n*N)*t,r=Math.sin(n*N)*t;return[e,r]}function an([t,n]){const e=Math.sqrt(t*t+n*n),r=Math.atan2(n,t)*It;return[e,r]}exports.DrawingPen=A.DrawingPen;exports.draw=A.draw;exports.exportJSON=c.exportJSON;exports.DEG2RAD=N;exports.RAD2DEG=It;exports.cartesianToPolar=an;exports.chamfer=F;exports.confineStrand=Ft;exports.cut=J;exports.eraseStrand=vt;exports.exportSVG=rn;exports.fillet=v;exports.fuse=at;exports.fuseAll=E;exports.importJSON=on;exports.intersect=Dt;exports.offset=qt;exports.outlineStroke=Nt;exports.polarToCartesian=cn;exports.selectCorners=Jt;exports.svgBody=$;exports.tesselate=en;exports.tesselatePoints=mt;
|
|
3
3
|
//# sourceMappingURL=pantograph.cjs.map
|