flatten-js
Version:
Javascript library for 2d geometry
1 lines • 52.2 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.flatten=e():t.flatten=e()}(window,function(){return function(t){var e={};function n(i){if(e[i])return e[i].exports;var r=e[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(i,r,function(e){return t[e]}.bind(null,r));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=2)}([function(t,e,n){"use strict";let i=n(7),{RB_TREE_COLOR_RED:r,RB_TREE_COLOR_BLACK:s}=n(1),o=new i;t.exports=class t{constructor(){this.root=null}get size(){let t=0;return this.tree_walk(this.root,()=>t++),t}get keys(){let t=[];return this.tree_walk(this.root,e=>t.push(e.item.key.output())),t}get values(){let t=[];return this.tree_walk(this.root,e=>t.push(e.item.value)),t}get items(){let t=[];return this.tree_walk(this.root,e=>t.push({key:e.item.key.output(),value:e.item.value})),t}get isEmpty(){return null==this.root||this.root==o}insert(t,e=t){if(void 0===t)return;let n=new i(t,e,o,o,null,r);return this.tree_insert(n),this.recalc_max(n),n}exist(t,e){let n=new i(t,e);return!!this.tree_search(this.root,n)}remove(t,e){let n=new i(t,e),r=this.tree_search(this.root,n);return r&&this.tree_delete(r),r}search(t){let e=new i(t),n=[];this.tree_search_interval(this.root,e,n);let r=[];return n.forEach(t=>{t.item.value?r.push(t.item.value):r.push(t.item.key.output())},[]),r}forEach(t){this.tree_walk(this.root,e=>t(e.item.key,e.item.value))}map(e){const n=new t;return this.tree_walk(this.root,t=>n.insert(t.item.key,e(t.item.value,t.item.key))),n}recalc_max(t){let e=t;for(;null!=e.parent;)e.parent.update_max(),e=e.parent}tree_insert(t){let e=this.root,n=null;if(null==this.root||this.root==o)this.root=t;else{for(;e!=o;)n=e,e=t.less_than(e)?e.left:e.right;t.parent=n,t.less_than(n)?n.left=t:n.right=t}this.insert_fixup(t)}insert_fixup(t){let e,n;for(e=t;e!=this.root&&e.parent.color==r;)e.parent==e.parent.parent.left?(n=e.parent.parent.right).color==r?(e.parent.color=s,n.color=s,e.parent.parent.color=r,e=e.parent.parent):(e==e.parent.right&&(e=e.parent,this.rotate_left(e)),e.parent.color=s,e.parent.parent.color=r,this.rotate_right(e.parent.parent)):(n=e.parent.parent.left).color==r?(e.parent.color=s,n.color=s,e.parent.parent.color=r,e=e.parent.parent):(e==e.parent.left&&(e=e.parent,this.rotate_right(e)),e.parent.color=s,e.parent.parent.color=r,this.rotate_left(e.parent.parent));this.root.color=s}tree_delete(t){let e,n;(n=(e=t.left==o||t.right==o?t:this.tree_successor(t)).left!=o?e.left:e.right).parent=e.parent,e==this.root?this.root=n:(e==e.parent.left?e.parent.left=n:e.parent.right=n,e.parent.update_max()),this.recalc_max(n),e!=t&&(t.copy_data(e),t.update_max(),this.recalc_max(t)),e.color==s&&this.delete_fixup(n)}delete_fixup(t){let e,n=t;for(;n!=this.root&&null!=n.parent&&n.color==s;)n==n.parent.left?((e=n.parent.right).color==r&&(e.color=s,n.parent.color=r,this.rotate_left(n.parent),e=n.parent.right),e.left.color==s&&e.right.color==s?(e.color=r,n=n.parent):(e.right.color==s&&(e.color=r,e.left.color=s,this.rotate_right(e),e=n.parent.right),e.color=n.parent.color,n.parent.color=s,e.right.color=s,this.rotate_left(n.parent),n=this.root)):((e=n.parent.left).color==r&&(e.color=s,n.parent.color=r,this.rotate_right(n.parent),e=n.parent.left),e.left.color==s&&e.right.color==s?(e.color=r,n=n.parent):(e.left.color==s&&(e.color=r,e.right.color=s,this.rotate_left(e),e=n.parent.left),e.color=n.parent.color,n.parent.color=s,e.left.color=s,this.rotate_right(n.parent),n=this.root));n.color=s}tree_search(t,e){if(null!=t&&t!=o)return e.equal_to(t)?t:e.less_than(t)?this.tree_search(t.left,e):this.tree_search(t.right,e)}tree_search_interval(t,e,n){null!=t&&t!=o&&(t.left==o||t.not_intersect_left_subtree(e)||this.tree_search_interval(t.left,e,n),t.intersect(e)&&n.push(t),t.right==o||t.not_intersect_right_subtree(e)||this.tree_search_interval(t.right,e,n))}local_minimum(t){let e=t;for(;null!=e.left&&e.left!=o;)e=e.left;return e}local_maximum(t){let e=t;for(;null!=e.right&&e.right!=o;)e=e.right;return e}tree_successor(t){let e,n,i;if(t.right!=o)e=this.local_minimum(t.right);else{for(n=t,i=t.parent;null!=i&&i.right==n;)n=i,i=i.parent;e=i}return e}rotate_left(t){let e=t.right;t.right=e.left,e.left!=o&&(e.left.parent=t),e.parent=t.parent,t==this.root?this.root=e:t==t.parent.left?t.parent.left=e:t.parent.right=e,e.left=t,t.parent=e,null!=t&&t!=o&&t.update_max(),null!=(e=t.parent)&&e!=o&&e.update_max()}rotate_right(t){let e=t.left;t.left=e.right,e.right!=o&&(e.right.parent=t),e.parent=t.parent,t==this.root?this.root=e:t==t.parent.left?t.parent.left=e:t.parent.right=e,e.right=t,t.parent=e,null!=t&&t!=o&&t.update_max(),null!=(e=t.parent)&&e!=o&&e.update_max()}tree_walk(t,e){null!=t&&t!=o&&(this.tree_walk(t.left,e),e(t),this.tree_walk(t.right,e))}testRedBlackProperty(){let t=!0;return this.tree_walk(this.root,function(e){e.color==r&&(e.left.color==s&&e.right.color==s||(t=!1))}),t}testBlackHeightProperty(t){let e=0,n=0,i=0;if(t.color==s&&e++,(n=t.left!=o?this.testBlackHeightProperty(t.left):1)!=(i=t.right!=o?this.testBlackHeightProperty(t.right):1))throw new Error("Red-black height property violated");return e+=n}}},function(t,e,n){"use strict";t.exports={RB_TREE_COLOR_RED:0,RB_TREE_COLOR_BLACK:1}},function(t,e,n){"use strict";let i=n(3),r=n(4),s=new class{constructor(){this.DP_TOL=i.DP_TOL,this.CCW=!0,this.CW=!1,this.ORIENTATION={CCW:-1,CW:1,NOT_ORIENTABLE:0},this.PIx2=2*Math.PI,this.INSIDE=1,this.OUTSIDE=0,this.BOUNDARY=2,this.CONTAINS=3,this.INTERLACE=4,this.OVERLAP_SAME=1,this.OVERLAP_OPPOSITE=2,this.Utils=i,this.Errors=r}};n(5)(s),n(6)(s),n(9)(s),n(10)(s),n(11)(s),n(12)(s),n(13)(s),n(14)(s),n(15)(s),n(16)(s),n(17)(s),n(18)(s),n(19)(s),n(20)(s),n(21)(s),t.exports=s},function(t,e){t.exports={DP_TOL:1e-6,EQ_0:function(t){return t<1e-6&&t>-1e-6},EQ:function(t,e){return t-e<1e-6&&t-e>-1e-6},GT:(t,e)=>t-e>1e-6,GE:(t,e)=>t-e>-1e-6,LT:(t,e)=>t-e<-1e-6,LE:(t,e)=>t-e<1e-6}},function(t,e){t.exports={ILLEGAL_PARAMETERS:new ReferenceError("Illegal Parameters"),ZERO_DIVISION:new Error("Zero division")}},function(t,e,n){"use strict";t.exports=function(t){t.Matrix=class e{constructor(t=1,e=0,n=0,i=1,r=0,s=0){this.a=t,this.b=e,this.c=n,this.d=i,this.tx=r,this.ty=s}clone(){return new e(this.a,this.b,this.c,this.d,this.tx,this.ty)}transform(t){return[t[0]*this.a+t[1]*this.c+this.tx,t[0]*this.b+t[1]*this.d+this.ty]}multiply(t){return new e(this.a*t.a+this.c*t.b,this.b*t.a+this.d*t.b,this.a*t.c+this.c*t.d,this.b*t.c+this.d*t.d,this.a*t.tx+this.c*t.ty+this.tx,this.b*t.tx+this.d*t.ty+this.ty)}translate(...n){let i,r;if(1==n.length&&n[0]instanceof t.Vector)i=n[0].x,r=n[0].y;else{if(2!=n.length||"number"!=typeof n[0]||"number"!=typeof n[1])throw t.Errors.ILLEGAL_PARAMETERS;i=n[0],r=n[1]}return this.multiply(new e(1,0,0,1,i,r))}rotate(t){let n=Math.cos(t),i=Math.sin(t);return this.multiply(new e(n,i,-i,n,0,0))}scale(t,n){return this.multiply(new e(t,0,0,n,0,0))}equalTo(e){return!!t.Utils.EQ(this.tx,e.tx)&&(!!t.Utils.EQ(this.ty,e.ty)&&(!!t.Utils.EQ(this.a,e.a)&&(!!t.Utils.EQ(this.b,e.b)&&(!!t.Utils.EQ(this.c,e.c)&&!!t.Utils.EQ(this.d,e.d)))))}},t.matrix=((...e)=>new t.Matrix(...e))}},function(t,e,n){"use strict";let i=n(0);t.exports=function(t){t.PlanarSet=class extends Set{constructor(){super(),this.index=new i}add(t){let e=this.size;if(super.add(t),this.size>e){this.index.insert(t.box,t)}return this}delete(t){let e=super.delete(t);return e&&this.index.remove(t.box,t),e}clear(){super.clear(),this.index=new i}search(t){return this.index.search(t)}hit(e){let n=new t.Box(e.x-1,e.y-1,e.x+1,e.y+1);return this.index.search(n).filter(t=>e.on(t))}svg(){return[...this].reduce((t,e)=>t+e.svg(),"")}}}},function(t,e,n){"use strict";let i=n(8),{RB_TREE_COLOR_RED:r,RB_TREE_COLOR_BLACK:s}=n(1);t.exports=class{constructor(t,e,n=null,r=null,o=null,l=s){this.left=n,this.right=r,this.parent=o,this.color=l,this.item={key:t,value:e},t&&t instanceof Array&&2==t.length&&(Number.isNaN(t[0])||Number.isNaN(t[1])||(this.item.key=new i(Math.min(t[0],t[1]),Math.max(t[0],t[1])))),this.max=this.item.key?this.item.key.max:void 0}isNil(){return void 0===this.item.key&&void 0===this.item.value&&null===this.left&&null===this.right&&this.color===s}less_than(t){return this.item.key.less_than(t.item.key)}equal_to(t){let e=!0;return this.item.value&&t.item.value&&(e=this.item.value.equal_to?this.item.value.equal_to(t.item.value):this.item.value==t.item.value),this.item.key.equal_to(t.item.key)&&e}intersect(t){return this.item.key.intersect(t.item.key)}copy_data(t){this.item.key=t.item.key.clone(),this.item.value=t.item.value}update_max(){if(this.max=this.item.key?this.item.key.max:void 0,this.right&&this.right.max){let t=this.item.key.maximal_val;this.max=t(this.max,this.right.max)}if(this.left&&this.left.max){let t=this.item.key.maximal_val;this.max=t(this.max,this.left.max)}}not_intersect_left_subtree(t){return(0,this.item.key.val_less_than)(this.left.max.high?this.left.max.high:this.left.max,t.item.key.low)}not_intersect_right_subtree(t){let e=this.item.key.val_less_than,n=this.right.max.low?this.right.max.low:this.right.item.key.low;return e(t.item.key.high,n)}}},function(t,e){t.exports=class t{constructor(t,e){this.low=t,this.high=e}get max(){return this.high}interval(e,n){return new t(e,n)}clone(){return new t(this.low,this.high)}less_than(t){return this.low<t.low||this.low==t.low&&this.high<t.high}equal_to(t){return this.low==t.low&&this.high==t.high}intersect(t){return!this.not_intersect(t)}not_intersect(t){return this.high<t.low||t.high<this.low}output(){return[this.low,this.high]}maximal_val(t,e){return Math.max(t,e)}val_less_than(t,e){return t<e}}},function(t,e){t.exports=function(t){t.Point=class e{constructor(...e){if(this.x=0,this.y=0,0!==e.length){if(1===e.length&&e[0]instanceof Array&&2===e[0].length){let t=e[0];if("number"==typeof t[0]&&"number"==typeof t[1])return this.x=t[0],void(this.y=t[1])}if(1===e.length&&e[0]instanceof Object&&"point"===e[0].name){let{x:t,y:n}=e[0];return this.x=t,void(this.y=n)}if(2===e.length&&"number"==typeof e[0]&&"number"==typeof e[1])return this.x=e[0],void(this.y=e[1]);throw t.Errors.ILLEGAL_PARAMETERS}}get box(){return new t.Box(this.x,this.y,this.x,this.y)}clone(){return new t.Point(this.x,this.y)}get vertices(){return[this.clone()]}equalTo(e){return t.Utils.EQ(this.x,e.x)&&t.Utils.EQ(this.y,e.y)}lessThan(e){return!!t.Utils.LT(this.y,e.y)||!(!t.Utils.EQ(this.y,e.y)||!t.Utils.LT(this.x,e.x))}rotate(e,n={x:0,y:0}){var i=n.x+(this.x-n.x)*Math.cos(e)-(this.y-n.y)*Math.sin(e),r=n.y+(this.x-n.x)*Math.sin(e)+(this.y-n.y)*Math.cos(e);return new t.Point(i,r)}translate(...e){if(1==e.length&&e[0]instanceof t.Vector)return new t.Point(this.x+e[0].x,this.y+e[0].y);if(2==e.length&&"number"==typeof e[0]&&"number"==typeof e[1])return new t.Point(this.x+e[0],this.y+e[1]);throw t.Errors.ILLEGAL_PARAMETERS}transform(e){return new t.Point(e.transform([this.x,this.y]))}projectionOn(e){if(this.equalTo(e.pt))return this.clone();let n=new t.Vector(this,e.pt);if(t.Utils.EQ_0(n.cross(e.norm)))return e.pt.clone();let i=n.dot(e.norm),r=e.norm.multiply(i);return this.translate(r)}leftTo(e){let n=new t.Vector(e.pt,this);return t.Utils.GT(n.dot(e.norm),0)}distanceTo(n){let{Distance:i}=t;if(n instanceof e){let e=n.x-this.x,i=n.y-this.y;return[Math.sqrt(e*e+i*i),new t.Segment(this,n)]}return n instanceof t.Line?i.point2line(this,n):n instanceof t.Circle?i.point2circle(this,n):n instanceof t.Segment?i.point2segment(this,n):n instanceof t.Arc?i.point2arc(this,n):n instanceof t.Polygon?i.point2polygon(this,n):n instanceof t.PlanarSet?i.shape2planarSet(this,n):void 0}on(e){return e instanceof t.Point?this.equalTo(e):e instanceof t.Line?e.contains(this):e instanceof t.Circle?e.contains(this):e instanceof t.Segment?e.contains(this):e instanceof t.Arc?e.contains(this):e instanceof t.Polygon?e.contains(this):void 0}svg(t={}){let{r:e,stroke:n,strokeWidth:i,fill:r,id:s,className:o}=t,l=s&&s.length>0?`id="${s}"`:"",h=o&&o.length>0?`class="${o}"`:"";return`\n<circle cx="${this.x}" cy="${this.y}" r="${e||3}" stroke="${n||"black"}" stroke-width="${i||1}" fill="${r||"red"}" ${l} ${h} />`}toJSON(){return Object.assign({},this,{name:"point"})}},t.point=((...e)=>new t.Point(...e))}},function(t,e,n){"use strict";t.exports=function(t){t.Vector=class e{constructor(...e){if(this.x=0,this.y=0,0!==e.length){if(1===e.length&&e[0]instanceof Array&&2===e[0].length){let t=e[0];if("number"==typeof t[0]&&"number"==typeof t[1])return this.x=t[0],void(this.y=t[1])}if(1===e.length&&e[0]instanceof Object&&"vector"===e[0].name){let{x:t,y:n}=e[0];return this.x=t,void(this.y=n)}if(2===e.length){let n=e[0],i=e[1];if("number"==typeof n&&"number"==typeof i)return this.x=n,void(this.y=i);if(n instanceof t.Point&&i instanceof t.Point)return this.x=i.x-n.x,void(this.y=i.y-n.y)}throw t.Errors.ILLEGAL_PARAMETERS}}clone(){return new e(this.x,this.y)}get slope(){let t=Math.atan2(this.y,this.x);return t<0&&(t=2*Math.PI+t),t}get length(){return Math.sqrt(this.dot(this))}equalTo(e){return t.Utils.EQ(this.x,e.x)&&t.Utils.EQ(this.y,e.y)}multiply(t){return new e(t*this.x,t*this.y)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}normalize(){if(!t.Utils.EQ_0(this.length))return new e(this.x/this.length,this.y/this.length);throw t.Errors.ZERO_DIVISION}rotate(e){let n=new t.Point(this.x,this.y).rotate(e);return new t.Vector(n.x,n.y)}rotate90CCW(){return new t.Vector(-this.y,this.x)}rotate90CW(){return new t.Vector(this.y,-this.x)}invert(){return new t.Vector(-this.x,-this.y)}add(e){return new t.Vector(this.x+e.x,this.y+e.y)}subtract(e){return new t.Vector(this.x-e.x,this.y-e.y)}angleTo(t){let e=this.normalize(),n=t.normalize(),i=Math.atan2(e.cross(n),e.dot(n));return i<0&&(i+=2*Math.PI),i}projectionOn(t){let e=t.normalize(),n=this.dot(e);return e.multiply(n)}toJSON(){return Object.assign({},this,{name:"vector"})}},t.vector=((...e)=>new t.Vector(...e))}},function(t,e,n){"use strict";t.exports=function(t){t.Line=class e{constructor(...n){if(this.pt=new t.Point,this.norm=new t.Vector(0,1),0!=n.length){if(1==n.length&&n[0]instanceof Object&&"line"===n[0].name){let{pt:e,norm:i}=n[0];return this.pt=new t.Point(e),void(this.norm=new t.Vector(i))}if(2==n.length){let i=n[0],r=n[1];if(i instanceof t.Point&&r instanceof t.Point)return this.pt=i,void(this.norm=e.points2norm(i,r));if(i instanceof t.Point&&r instanceof t.Vector){if(t.Utils.EQ_0(r.x)&&t.Utils.EQ_0(r.y))throw t.Errors.ILLEGAL_PARAMETERS;return this.pt=i.clone(),this.norm=r.clone(),void(this.norm=this.norm.normalize())}if(i instanceof t.Vector&&r instanceof t.Point){if(t.Utils.EQ_0(i.x)&&t.Utils.EQ_0(i.y))throw t.Errors.ILLEGAL_PARAMETERS;return this.pt=r.clone(),this.norm=i.clone(),void(this.norm=this.norm.normalize())}}throw t.Errors.ILLEGAL_PARAMETERS}}clone(){return new t.Line(this.pt,this.norm)}get slope(){return new t.Vector(this.norm.y,-this.norm.x).slope}get standard(){return[this.norm.x,this.norm.y,this.norm.dot(this.pt)]}parallelTo(e){return t.Utils.EQ_0(this.norm.cross(e.norm))}incidentTo(t){return this.parallelTo(t)&&this.pt.on(t)}contains(e){if(this.pt.equalTo(e))return!0;let n=new t.Vector(this.pt,e);return t.Utils.EQ_0(this.norm.dot(n))}intersect(n){return n instanceof t.Point?this.contains(n)?[n]:[]:n instanceof t.Line?e.intersectLine2Line(this,n):n instanceof t.Circle?e.intersectLine2Circle(this,n):n instanceof t.Segment?n.intersect(this):n instanceof t.Arc?e.intersectLine2Arc(this,n):n instanceof t.Polygon?t.Polygon.intersectLine2Polygon(this,n):void 0}distanceTo(e){let{Distance:n}=t;if(e instanceof t.Point){let[t,i]=n.point2line(e,this);return[t,i=i.reverse()]}if(e instanceof t.Circle){let[t,i]=n.circle2line(e,this);return[t,i=i.reverse()]}if(e instanceof t.Segment){let[t,i]=n.segment2line(e,this);return[t,i.reverse()]}if(e instanceof t.Arc){let[t,i]=n.arc2line(e,this);return[t,i.reverse()]}if(e instanceof t.Polygon){let[t,i]=n.shape2polygon(this,e);return[t,i]}}svg(n,i={}){let r=e.intersectLine2Box(this,n);if(0===r.length)return"";let s=r[0],o=2==r.length?r[1]:r.find(t=>!t.equalTo(s));return void 0===o&&(o=s),new t.Segment(s,o).svg(i)}toJSON(){return Object.assign({},this,{name:"line"})}static points2norm(e,n){if(e.equalTo(n))throw t.Errors.ILLEGAL_PARAMETERS;return new t.Vector(e,n).normalize().rotate90CCW()}static intersectLine2Line(e,n){let i=[],[r,s,o]=e.standard,[l,h,a]=n.standard,c=r*h-s*l,u=o*h-s*a,p=r*a-o*l;if(!t.Utils.EQ_0(c)){let e=new t.Point(u/c,p/c);i.push(e)}return i}static intersectLine2Circle(e,n){let i=[],r=n.pc.projectionOn(e),s=n.pc.distanceTo(r)[0];if(t.Utils.EQ(s,n.r))i.push(r);else if(t.Utils.LT(s,n.r)){let t,o,l=Math.sqrt(n.r*n.r-s*s);t=e.norm.rotate90CCW().multiply(l),o=r.translate(t),i.push(o),t=e.norm.rotate90CW().multiply(l),o=r.translate(t),i.push(o)}return i}static intersectLine2Box(e,n){let i=[new t.Point(n.xmin,n.ymin),new t.Point(n.xmax,n.ymin),new t.Point(n.xmax,n.ymax),new t.Point(n.xmin,n.ymax)],r=[new t.Segment(i[0],i[1]),new t.Segment(i[1],i[2]),new t.Segment(i[2],i[3]),new t.Segment(i[3],i[0])],s=[];for(let t of r){let n=t.intersect(e);for(let t of n)s.push(t)}return s}static intersectLine2Arc(n,i){let r=[];if(0==e.intersectLine2Box(n,i.box).length)return r;let s=new t.Circle(i.pc,i.r),o=n.intersect(s);for(let t of o)t.on(i)&&r.push(t);return r}},t.line=((...e)=>new t.Line(...e))}},function(t,e,n){"use strict";t.exports=function(t){let{Arc:e,vector:n}=t;t.Circle=class e{constructor(...e){if(this.pc=new t.Point,this.r=1,1==e.length&&e[0]instanceof Object&&"circle"===e[0].name){let{pc:n,r:i}=e[0];return this.pc=new t.Point(n),void(this.r=i)}{let[n,i]=[...e];return n&&n instanceof t.Point&&(this.pc=n.clone()),void(void 0!==i&&(this.r=i))}}clone(){return new t.Circle(this.pc.clone(),this.r)}get center(){return this.pc}get box(){return new t.Box(this.pc.x-this.r,this.pc.y-this.r,this.pc.x+this.r,this.pc.y+this.r)}contains(e){return t.Utils.LE(e.distanceTo(this.center)[0],this.r)}toArc(e=!0){return new t.Arc(this.center,this.r,Math.PI,-Math.PI,e)}intersect(n){return n instanceof t.Point?this.contains(n)?[n]:[]:n instanceof t.Line?n.intersect(this):n instanceof t.Segment?n.intersect(this):n instanceof t.Circle?e.intersectCirle2Circle(this,n):n instanceof t.Arc?n.intersect(this):n instanceof t.Polygon?t.Polygon.intersectShape2Polygon(this,n):void 0}distanceTo(e){let{Distance:n}=t,{point2circle:i,circle2circle:r,circle2line:s,segment2circle:o,arc2circle:l}=n;if(e instanceof t.Point){let[t,n]=i(e,this);return[t,n=n.reverse()]}if(e instanceof t.Circle){let[t,n]=r(this,e);return[t,n]}if(e instanceof t.Line){let[t,n]=s(this,e);return[t,n]}if(e instanceof t.Segment){let[t,n]=o(e,this);return[t,n=n.reverse()]}if(e instanceof t.Arc){let[t,n]=l(e,this);return[t,n=n.reverse()]}if(e instanceof t.Polygon){let[t,i]=n.shape2polygon(this,e);return[t,i]}if(e instanceof t.PlanarSet){let[t,i]=n.shape2planarSet(this,e);return[t,i]}}static intersectCirle2Circle(e,n){let i=[];if(e.box.not_intersect(n.box))return i;let r=new t.Vector(e.pc,n.pc),s=e.r,o=n.r;if(t.Utils.EQ_0(s)||t.Utils.EQ_0(o))return i;if(t.Utils.EQ_0(r.x)&&t.Utils.EQ_0(r.y)&&t.Utils.EQ(s,o))return i.push(e.pc.translate(-s,0)),i;let l,h=e.pc.distanceTo(n.pc)[0];if(t.Utils.GT(h,s+o))return i;if(t.Utils.LT(h,Math.abs(s-o)))return i;if(r.x/=h,r.y/=h,t.Utils.EQ(h,s+o)||t.Utils.EQ(h,Math.abs(s-o)))return l=e.pc.translate(s*r.x,s*r.y),i.push(l),i;let a=s*s/(2*h)-o*o/(2*h)+h/2,c=e.pc.translate(a*r.x,a*r.y),u=Math.sqrt(s*s-a*a);return l=c.translate(r.rotate90CCW().multiply(u)),i.push(l),l=c.translate(r.rotate90CW().multiply(u)),i.push(l),i}svg(t={}){let{stroke:e,strokeWidth:n,fill:i,fillOpacity:r,id:s,className:o}=t,l=s&&s.length>0?`id="${s}"`:"",h=o&&o.length>0?`class="${o}"`:"";return`\n<circle cx="${this.pc.x}" cy="${this.pc.y}" r="${this.r}" stroke="${e||"black"}" stroke-width="${n||1}" fill="${i||"none"}" fill-opacity="${r||1}" ${l} ${h} />`}toJSON(){return Object.assign({},this,{name:"circle"})}},t.circle=((...e)=>new t.Circle(...e))}},function(t,e,n){"use strict";t.exports=function(t){t.Segment=class e{constructor(...e){if(this.ps=new t.Point,this.pe=new t.Point,0!=e.length){if(1==e.length&&e[0]instanceof Array&&4==e[0].length){let n=e[0];return this.ps=new t.Point(n[0],n[1]),void(this.pe=new t.Point(n[2],n[3]))}if(1==e.length&&e[0]instanceof Object&&"segment"===e[0].name){let{ps:n,pe:i}=e[0];return this.ps=new t.Point(n.x,n.y),void(this.pe=new t.Point(i.x,i.y))}if(2==e.length&&e[0]instanceof t.Point&&e[1]instanceof t.Point)return this.ps=e[0].clone(),void(this.pe=e[1].clone());if(4==e.length)return this.ps=new t.Point(e[0],e[1]),void(this.pe=new t.Point(e[2],e[3]));throw t.Errors.ILLEGAL_PARAMETERS}}clone(){return new t.Segment(this.start,this.end)}get start(){return this.ps}get end(){return this.pe}get vertices(){return[this.ps.clone(),this.pe.clone()]}get length(){return this.start.distanceTo(this.end)[0]}get slope(){return new t.Vector(this.start,this.end).slope}get box(){return new t.Box(Math.min(this.start.x,this.end.x),Math.min(this.start.y,this.end.y),Math.max(this.start.x,this.end.x),Math.max(this.start.y,this.end.y))}equalTo(t){return this.ps.equalTo(t.ps)&&this.pe.equalTo(t.pe)}contains(e){return t.Utils.EQ_0(this.distanceToPoint(e))}intersect(n){return n instanceof t.Point?this.contains(n)?[n]:[]:n instanceof t.Line?e.intersectSegment2Line(this,n):n instanceof t.Segment?e.intersectSegment2Segment(this,n):n instanceof t.Circle?e.intersectSegment2Circle(this,n):n instanceof t.Arc?e.intersectSegment2Arc(this,n):n instanceof t.Polygon?t.Polygon.intersectShape2Polygon(this,n):void 0}distanceTo(e){let{Distance:n}=t;if(e instanceof t.Point){let[t,i]=n.point2segment(e,this);return[t,i=i.reverse()]}if(e instanceof t.Circle){let[t,i]=n.segment2circle(this,e);return[t,i]}if(e instanceof t.Line){let[t,i]=n.segment2line(this,e);return[t,i]}if(e instanceof t.Segment){let[t,i]=n.segment2segment(this,e);return[t,i]}if(e instanceof t.Arc){let[t,i]=n.segment2arc(this,e);return[t,i]}if(e instanceof t.Polygon){let[t,i]=n.shape2polygon(this,e);return[t,i]}if(e instanceof t.PlanarSet){let[t,i]=n.shape2planarSet(this,e);return[t,i]}}tangentInStart(){return new t.Vector(this.start,this.end).normalize()}tangentInEnd(){return new t.Vector(this.end,this.start).normalize()}reverse(){return new e(this.end,this.start)}split(e){return this.contains(e)?this.start.equalTo(this.end)?[this.clone()]:this.start.equalTo(e)||this.end.equalTo(e)?[this]:[new t.Segment(this.start,e),new t.Segment(e,this.end)]:[]}middle(){return new t.Point((this.start.x+this.end.x)/2,(this.start.y+this.end.y)/2)}distanceToPoint(e){let[n,...i]=t.Distance.point2segment(e,this);return n}definiteIntegral(t=0){return(this.end.x-this.start.x)*(this.start.y-t+(this.end.y-t))/2}translate(...t){return new e(this.ps.translate(...t),this.pe.translate(...t))}rotate(e=0,n=new t.Point){let i=new t.Matrix;return i=i.translate(n.x,n.y).rotate(e).translate(-n.x,-n.y),this.transform(i)}transform(n=new t.Matrix){return new e(this.ps.transform(n),this.pe.transform(n))}isZeroLength(){return this.ps.equalTo(this.pe)}static intersectSegment2Line(e,n){let i=[];return e.ps.on(n)&&i.push(e.ps),e.pe.on(n)&&!e.isZeroLength()&&i.push(e.pe),i.length>0?i:e.isZeroLength()?i:e.ps.leftTo(n)&&e.pe.leftTo(n)||!e.ps.leftTo(n)&&!e.pe.leftTo(n)?i:new t.Line(e.ps,e.pe).intersect(n)}static intersectSegment2Segment(e,n){let i=[];if(e.box.not_intersect(n.box))return i;if(e.isZeroLength())return e.ps.on(n)&&i.push(e.ps),i;if(n.isZeroLength())return n.ps.on(e)&&i.push(n.ps),i;let r=new t.Line(e.ps,e.pe),s=new t.Line(n.ps,n.pe);if(r.incidentTo(s))e.ps.on(n)&&i.push(e.ps),e.pe.on(n)&&i.push(e.pe),!n.ps.on(e)||n.ps.equalTo(e.ps)||n.ps.equalTo(e.pe)||i.push(n.ps),!n.pe.on(e)||n.pe.equalTo(e.ps)||n.pe.equalTo(e.pe)||i.push(n.pe);else{let t=r.intersect(s);t.length>0&&t[0].on(e)&&t[0].on(n)&&i.push(t[0])}return i}static intersectSegment2Circle(e,n){let i=[];if(e.box.not_intersect(n.box))return i;if(e.isZeroLength()){let[r,s]=e.ps.distanceTo(n.pc);return t.Utils.EQ(r,n.r)&&i.push(e.ps),i}let r=new t.Line(e.ps,e.pe).intersect(n);for(let t of r)t.on(e)&&i.push(t);return i}static intersectSegment2Arc(e,n){let i=[];if(e.box.not_intersect(n.box))return i;if(e.isZeroLength())return e.ps.on(n)&&i.push(e.ps),i;let r=new t.Line(e.ps,e.pe),s=new t.Circle(n.pc,n.r),o=r.intersect(s);for(let t of o)t.on(e)&&t.on(n)&&i.push(t);return i}svg(t={}){let{stroke:e,strokeWidth:n,id:i,className:r}=t,s=i&&i.length>0?`id="${i}"`:"",o=r&&r.length>0?`class="${r}"`:"";return`\n<line x1="${this.start.x}" y1="${this.start.y}" x2="${this.end.x}" y2="${this.end.y}" stroke="${e||"black"}" stroke-width="${n||1}" ${s} ${o} />`}toJSON(){return Object.assign({},this,{name:"segment"})}},t.segment=((...e)=>new t.Segment(...e))}},function(t,e,n){"use strict";t.exports=function(t){t.Arc=class e{constructor(...e){if(this.pc=new t.Point,this.r=1,this.startAngle=0,this.endAngle=2*Math.PI,this.counterClockwise=t.CCW,0!=e.length){if(1==e.length&&e[0]instanceof Object&&"arc"===e[0].name){let{pc:n,r:i,startAngle:r,endAngle:s,counterClockwise:o}=e[0];return this.pc=new t.Point(n.x,n.y),this.r=i,this.startAngle=r,this.endAngle=s,void(this.counterClockwise=o)}{let[n,i,r,s,o]=[...e];return n&&n instanceof t.Point&&(this.pc=n.clone()),void 0!==i&&(this.r=i),void 0!==r&&(this.startAngle=r),void 0!==s&&(this.endAngle=s),void(void 0!==o&&(this.counterClockwise=o))}}}clone(){return new t.Arc(this.pc.clone(),this.r,this.startAngle,this.endAngle,this.counterClockwise)}get sweep(){if(t.Utils.EQ(this.startAngle,this.endAngle))return 0;if(t.Utils.EQ(Math.abs(this.startAngle-this.endAngle),t.PIx2))return t.PIx2;let e;return e=this.counterClockwise?t.Utils.GT(this.endAngle,this.startAngle)?this.endAngle-this.startAngle:this.endAngle-this.startAngle+t.PIx2:t.Utils.GT(this.startAngle,this.endAngle)?this.startAngle-this.endAngle:this.startAngle-this.endAngle+t.PIx2,t.Utils.GT(e,t.PIx2)&&(e-=t.PIx2),t.Utils.LT(e,0)&&(e+=t.PIx2),e}get start(){return new t.Point(this.pc.x+this.r,this.pc.y).rotate(this.startAngle,this.pc)}get end(){return new t.Point(this.pc.x+this.r,this.pc.y).rotate(this.endAngle,this.pc)}get center(){return this.pc.clone()}get vertices(){return[this.start.clone(),this.end.clone()]}get length(){return Math.abs(this.sweep*this.r)}get box(){let e=this.breakToFunctional().reduce((t,e)=>t.merge(e.start.box),new t.Box);return e=e.merge(this.end.box)}contains(e){if(!t.Utils.EQ(this.pc.distanceTo(e)[0],this.r))return!1;if(e.equalTo(this.start))return!0;let n=new t.Vector(this.pc,e).slope,i=new t.Arc(this.pc,this.r,this.startAngle,n,this.counterClockwise);return t.Utils.LE(i.length,this.length)}split(e){if(!this.contains(e))return[];if(t.Utils.EQ_0(this.sweep))return[this.clone()];if(this.start.equalTo(e)||this.end.equalTo(e))return[this.clone()];let n=new t.Vector(this.pc,e).slope;return[new t.Arc(this.pc,this.r,this.startAngle,n,this.counterClockwise),new t.Arc(this.pc,this.r,n,this.endAngle,this.counterClockwise)]}middle(){let e=this.counterClockwise?this.startAngle+this.sweep/2:this.startAngle-this.sweep/2;return new t.Arc(this.pc,this.r,this.startAngle,e,this.counterClockwise).end}chordHeight(){return(1-Math.cos(Math.abs(this.sweep/2)))*this.r}intersect(n){return n instanceof t.Point?this.contains(n)?[n]:[]:n instanceof t.Line?n.intersect(this):n instanceof t.Circle?e.intersectArc2Circle(this,n):n instanceof t.Segment?n.intersect(this):n instanceof t.Arc?e.intersectArc2Arc(this,n):n instanceof t.Polygon?t.Polygon.intersectShape2Polygon(this,n):void 0}distanceTo(e){let{Distance:n}=t;if(e instanceof t.Point){let[t,i]=n.point2arc(e,this);return[t,i=i.reverse()]}if(e instanceof t.Circle){let[t,i]=n.arc2circle(this,e);return[t,i]}if(e instanceof t.Line){let[t,i]=n.arc2line(this,e);return[t,i]}if(e instanceof t.Segment){let[t,i]=n.segment2arc(e,this);return[t,i=i.reverse()]}if(e instanceof t.Arc){let[t,i]=n.arc2arc(this,e);return[t,i]}if(e instanceof t.Polygon){let[t,i]=n.shape2polygon(this,e);return[t,i]}if(e instanceof t.PlanarSet){let[t,i]=n.shape2planarSet(this,e);return[t,i]}}breakToFunctional(){let e=[],n=[0,Math.PI/2,2*Math.PI/2,3*Math.PI/2],i=[this.pc.translate(this.r,0),this.pc.translate(0,this.r),this.pc.translate(-this.r,0),this.pc.translate(0,-this.r)],r=[];for(let e=0;e<4;e++)i[e].on(this)&&r.push(new t.Arc(this.pc,this.r,this.startAngle,n[e],this.counterClockwise));if(0==r.length)e.push(this.clone());else{r.sort((t,e)=>t.length-e.length);for(let n=0;n<r.length;n++){let i,s=e.length>0?e[e.length-1]:void 0;i=s?new t.Arc(this.pc,this.r,s.endAngle,r[n].endAngle,this.counterClockwise):new t.Arc(this.pc,this.r,this.startAngle,r[n].endAngle,this.counterClockwise),t.Utils.EQ_0(i.length)||e.push(i.clone())}let n,i=e.length>0?e[e.length-1]:void 0;n=i?new t.Arc(this.pc,this.r,i.endAngle,this.endAngle,this.counterClockwise):new t.Arc(this.pc,this.r,this.startAngle,this.endAngle,this.counterClockwise),t.Utils.EQ_0(n.length)||e.push(n.clone())}return e}tangentInStart(){let e=new t.Vector(this.pc,this.start),n=this.counterClockwise?Math.PI/2:-Math.PI/2;return e.rotate(n).normalize()}tangentInEnd(){let e=new t.Vector(this.pc,this.end),n=this.counterClockwise?-Math.PI/2:Math.PI/2;return e.rotate(n).normalize()}reverse(){return new e(this.pc,this.r,this.endAngle,this.startAngle,!this.counterClockwise)}translate(...t){let e=this.clone();return e.pc=this.pc.translate(...t),e}rotate(e=0,n=new t.Point){let i=new t.Matrix;return i=i.translate(n.x,n.y).rotate(e).translate(-n.x,-n.y),this.transform(i)}transform(n=new t.Matrix){let i=this.start.transform(n),r=this.end.transform(n),s=this.pc.transform(n);return e.arcSE(s,i,r,this.counterClockwise)}static arcSE(n,i,r,s){let{vector:o}=t,l=o(n,i).slope,h=o(n,r).slope;t.Utils.EQ(l,h)&&(h+=2*Math.PI,s=!0);let a=o(n,i).length;return new e(n,a,l,h,s)}static intersectArc2Arc(e,n){var i=[];if(e.box.not_intersect(n.box))return i;if(e.pc.equalTo(n.pc)&&t.Utils.EQ(e.r,n.r)){let t;return(t=e.start).on(n)&&i.push(t),(t=e.end).on(n)&&i.push(t),(t=n.start).on(e)&&i.push(t),(t=n.end).on(e)&&i.push(t),i}let r=new t.Circle(e.pc,e.r),s=new t.Circle(n.pc,n.r),o=r.intersect(s);for(let t of o)t.on(e)&&t.on(n)&&i.push(t);return i}static intersectArc2Circle(e,n){let i=[];if(e.box.not_intersect(n.box))return i;if(n.pc.equalTo(e.pc)&&t.Utils.EQ(n.r,e.r))return i.push(e.start),i.push(e.end),i;let r=n,s=new t.Circle(e.pc,e.r),o=r.intersect(s);for(let t of o)t.on(e)&&i.push(t);return i}definiteIntegral(t=0){return this.breakToFunctional().reduce((e,n)=>e+n.circularSegmentDefiniteIntegral(t),0)}circularSegmentDefiniteIntegral(e){let n=new t.Line(this.start,this.end),i=this.pc.leftTo(n),r=new t.Segment(this.start,this.end).definiteIntegral(e),s=this.circularSegmentArea();return i?r-s:r+s}circularSegmentArea(){return.5*this.r*this.r*(this.sweep-Math.sin(this.sweep))}svg(e={}){let n=this.sweep<=Math.PI?"0":"1",i=this.counterClockwise?"1":"0",{stroke:r,strokeWidth:s,fill:o,id:l,className:h}=e,a=l&&l.length>0?`id="${l}"`:"",c=h&&h.length>0?`class="${h}"`:"";if(t.Utils.EQ(this.sweep,2*Math.PI)){return new t.Circle(this.pc,this.r).svg(e)}return`\n<path d="M${this.start.x},${this.start.y}\n A${this.r},${this.r} 0 ${n},${i} ${this.end.x},${this.end.y}"\n stroke="${r||"black"}" stroke-width="${s||1}" fill="${o||"none"}" ${a} ${c} />`}toJSON(){return Object.assign({},this,{name:"arc"})}},t.arc=((...e)=>new t.Arc(...e))}},function(t,e,n){"use strict";t.exports=function(t){t.Box=class e{constructor(t,e,n,i){this.xmin=t,this.ymin=e,this.xmax=n,this.ymax=i}clone(){return new e(this.xmin,this.ymin,this.xmax,this.ymax)}get low(){return new t.Point(this.xmin,this.ymin)}get high(){return new t.Point(this.xmax,this.ymax)}get max(){return this.clone()}get center(){return new t.Point((this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2)}not_intersect(t){return this.xmax<t.xmin||this.xmin>t.xmax||this.ymax<t.ymin||this.ymin>t.ymax}intersect(t){return!this.not_intersect(t)}merge(t){return new e(void 0===this.xmin?t.xmin:Math.min(this.xmin,t.xmin),void 0===this.ymin?t.ymin:Math.min(this.ymin,t.ymin),void 0===this.xmax?t.xmax:Math.max(this.xmax,t.xmax),void 0===this.ymax?t.ymax:Math.max(this.ymax,t.ymax))}less_than(t){return!!this.low.lessThan(t.low)||!(!this.low.equalTo(t.low)||!this.high.lessThan(t.high))}equal_to(t){return this.low.equalTo(t.low)&&this.high.equalTo(t.high)}output(){return this.clone()}maximal_val(t,e){return t.merge(e)}val_less_than(t,e){return t.lessThan(e)}set(t,e,n,i){this.xmin=t,this.ymin=e,this.xmax=n,this.ymax=i}svg(t={}){let{stroke:e,strokeWidth:n,fill:i,id:r,className:s}=t,o=r&&r.length>0?`id="${r}"`:"",l=s&&s.length>0?`class="${s}"`:"",h=this.xmax-this.xmin,a=this.ymax-this.ymin;return`\n<rect x="${this.xmin}" y="${this.ymin}" width=${h} height=${a} stroke="${e||"black"}" stroke-width="${n||1}" fill="${i||"none"}" ${o} ${l} />`}},t.box=((...e)=>new t.Box(...e))}},function(t,e){t.exports=function(t){t.Edge=class{constructor(t){this.shape=t,this.next,this.prev,this.face,this.arc_length=0,this.bvStart=void 0,this.bvEnd=void 0,this.bv=void 0,this.overlap=void 0}get start(){return this.shape.start}get end(){return this.shape.end}get length(){return this.shape.length}get box(){return this.shape.box}isSegment(){return this.shape instanceof t.Segment}isArc(){return this.shape instanceof t.Arc}middle(){return this.shape.middle()}contains(t){return this.shape.contains(t)}setInclusion(e){if(void 0!==this.bv)return this.bv;if(void 0===this.bvStart&&(this.bvStart=t.ray_shoot(e,this.start)),void 0===this.bvEnd&&(this.bvEnd=t.ray_shoot(e,this.end)),this.bvStart===t.OUTSIDE||this.bvEnd==t.OUTSIDE)this.bv=t.OUTSIDE;else if(this.bvStart===t.INSIDE||this.bvEnd==t.INSIDE)this.bv=t.INSIDE;else{let n=t.ray_shoot(e,this.middle());this.bv=n}return this.bv}setOverlap(e){let n=void 0,i=this.shape,r=e.shape;i instanceof t.Segment&&r instanceof t.Segment?i.start.equalTo(r.start)&&i.end.equalTo(r.end)?n=t.OVERLAP_SAME:i.start.equalTo(r.end)&&i.end.equalTo(r.start)&&(n=t.OVERLAP_OPPOSITE):i instanceof t.Arc&&r instanceof t.Arc?i.start.equalTo(r.start)&&i.end.equalTo(r.end)&&i.middle().equalTo(r.middle())?n=t.OVERLAP_SAME:i.start.equalTo(r.end)&&i.end.equalTo(r.start)&&i.middle().equalTo(r.middle())&&(n=t.OVERLAP_OPPOSITE):(i instanceof t.Segment&&r instanceof t.Arc||i instanceof t.Arc&&r instanceof t.Segment)&&(i.start.equalTo(r.start)&&i.end.equalTo(r.end)&&i.middle().equalTo(r.middle())?n=t.OVERLAP_SAME:i.start.equalTo(r.end)&&i.end.equalTo(r.start)&&i.middle().equalTo(r.middle())&&(n=t.OVERLAP_OPPOSITE)),void 0===this.overlap&&(this.overlap=n),void 0===e.overlap&&(e.overlap=n)}svg(){if(this.shape instanceof t.Segment)return` L${this.shape.end.x},${this.shape.end.y}`;if(this.shape instanceof t.Arc){let e,n=this.shape,i=n.counterClockwise?"1":"0";if(t.Utils.EQ(n.sweep,2*Math.PI)){let r=n.counterClockwise?1:-1,s=new t.Arc(n.pc,n.r,n.startAngle,n.startAngle+r*Math.PI,n.counterClockwise),o=new t.Arc(n.pc,n.r,n.startAngle+r*Math.PI,n.endAngle,n.counterClockwise);return e="0",` A${s.r},${s.r} 0 ${e},${i} ${s.end.x},${s.end.y}\n A${o.r},${o.r} 0 ${e},${i} ${o.end.x},${o.end.y}`}return e=n.sweep<=Math.PI?"0":"1",` A${n.r},${n.r} 0 ${e},${i} ${n.end.x},${n.end.y}`}}toJSON(){return this.shape.toJSON()}}}},function(t,e,n){"use strict";t.exports=function(t){let{Point:e,point:n,Segment:i,segment:r,Arc:s,Box:o,Edge:l,Circle:h}=t;t.Face=class a{constructor(c,...u){if(this.first,this.last,this._box=void 0,this._orientation=void 0,0!=u.length){if(1==u.length)if(u[0]instanceof Array){let t=u[0];if(0==t.length)return;if(t.every(t=>t instanceof e)){let e=a.points2segments(t);this.shapes2face(c.edges,e)}else if(t.every(t=>t instanceof i||t instanceof s))this.shapes2face(c.edges,t);else if(t.every(t=>"segment"===t.name||"arc"===t.name)){let e=[];for(let n of t){let t;t="segment"===n.name?new i(n):new s(n),e.push(t)}this.shapes2face(c.edges,e)}}else if(u[0]instanceof a){let t=u[0];this.first=t.first,this.last=t.last;for(let e of t)c.edges.add(e)}else if(u[0]instanceof h)this.shapes2face(c.edges,[u[0].toArc(t.CCW)]);else if(u[0]instanceof o){let t=u[0];this.shapes2face(c.edges,[r(n(t.xmin,t.ymin),n(t.xmax,t.ymin)),r(n(t.xmax,t.ymin),n(t.xmax,t.ymax)),r(n(t.xmax,t.ymax),n(t.xmin,t.ymax)),r(n(t.xmin,t.ymax),n(t.xmin,t.ymin))])}2==u.length&&u[0]instanceof l&&u[1]instanceof l&&(this.first=u[0],this.last=u[1],this.last.next=this.first,this.first.prev=this.last,this.setArcLength())}}[Symbol.iterator](){let t=void 0;return{next:()=>{let e=t||this.first,n=!this.first||!!t&&t===this.first;return t=e?e.next:void 0,{value:e,done:n}}}}get edges(){let t=[];for(let e of this)t.push(e);return t}get size(){let t=0;for(let e of this)t++;return t}get box(){if(void 0===this._box){let e=new t.Box;for(let t of this)e=e.merge(t.box);this._box=e}return this._box}static points2segments(t){let e=[];for(let n=0;n<t.length;n++)e.push(new i(t[n],t[(n+1)%t.length]));return e}shapes2face(t,e){for(let n of e){let e=new l(n);this.append(t,e)}}isEmpty(){return void 0===this.first&&void 0===this.last}append(t,e){void 0===this.first?(e.prev=e,e.next=e,this.first=e,this.last=e,e.arc_length=0):(e.prev=this.last,this.last.next=e,this.last=e,this.last.next=this.first,this.first.prev=this.last,e.arc_length=e.prev.arc_length+e.prev.length),e.face=this,t.add(e)}insert(t,e,n){if(void 0===this.first)e.prev=e,e.next=e,this.first=e,this.last=e;else{let t=n.next;n.next=e,t.prev=e,e.prev=n,e.next=t,this.last===n&&(this.first=e)}e.face=this,e.prev===this.last?e.arc_length=0:e.arc_length=e.prev.arc_length+e.prev.length,t.add(e)}remove(t,e){e===this.first&&e===this.last?(this.first=void 0,this.last=void 0):(e.prev.next=e.next,e.next.prev=e.prev,e===this.first&&(this.first=e.next),e===this.last&&(this.last=e.prev)),t.delete(e)}reverse(){let t=[],e=this.last;do{e.shape=e.shape.reverse(),t.push(e),e=e.prev}while(e!==this.last);this.first=void 0,this.last=void 0;for(let e of t)void 0===this.first?(e.prev=e,e.next=e,this.first=e,this.last=e,e.arc_length=0):(e.prev=this.last,this.last.next=e,this.last=e,this.last.next=this.first,this.first.prev=this.last,e.arc_length=e.prev.arc_length+e.prev.length);void 0!==this._orientation&&(this._orientation=void 0,this._orientation=this.orientation())}setArcLength(){for(let t of this)t===this.first?t.arc_length=0:t.arc_length=t.prev.arc_length+t.prev.length,t.face=this}area(){return Math.abs(this.signedArea())}signedArea(){let t=0,e=this.box.ymin;for(let n of this)t+=n.shape.definiteIntegral(e);return t}orientation(){if(void 0===this._orientation){let e=this.signedArea();t.Utils.EQ_0(e)?this._orientation=t.ORIENTATION.NOT_ORIENTABLE:t.Utils.LT(e,0)?this._orientation=t.ORIENTATION.CCW:this._orientation=t.ORIENTATION.CW}return this._orientation}isSimple(t){return 0==a.getSelfIntersections(this,t,!0).length}static getSelfIntersections(e,n,i=!1){let r=[];for(let s of e){let e=n.search(s.box);for(let n of e){if(s===n)continue;if(s.shape instanceof t.Segment&&n.shape instanceof t.Segment&&(s.next===n||s.prev===n))continue;let e=s.shape.intersect(n.shape);for(let t of e)if((!t.equalTo(s.start)||!t.equalTo(n.end)||n!==s.prev)&&(!t.equalTo(s.end)||!t.equalTo(n.start)||n!==s.next)&&(r.push(t),i))break;if(r.length>0&&i)break}if(r.length>0&&i)break}return r}toJSON(){return this.edges.map(t=>t.toJSON())}svg(){let t=`\nM${this.first.start.x},${this.first.start.y}`;for(let e of this)t+=e.svg();return t+=" z"}}}},function(t,e,n){"use strict";t.exports=function(t){let{Point:e,Segment:n,Line:i,Circle:r,Arc:s,Box:o,Vector:l}=t;t.Ray=class r{constructor(...n){if(this.pt=new e,0!=n.length)if(1==n.length&&n[0]instanceof e)this.pt=n[0].clone();else{if(2!=n.length||"number"!=typeof n[0]||"number"!=typeof n[1])throw t.Errors.ILLEGAL_PARAMETERS;this.pt=new e(n[0],n[1])}}clone(){return new r(this.pt)}get box(){return new o(this.pt.x,this.pt.y,Number.POSITIVE_INFINITY,this.pt.y)}get start(){return this.pt}get norm(){return new l(0,1)}intersect(t){return t instanceof n?this.intersectRay2Segment(this,t):t instanceof s?this.intersectRay2Arc(this,t):void 0}intersectRay2Segment(e,n){let r=[];if(e.box.not_intersect(n.box))return r;let s=new i(e.start,e.norm),o=s.intersect(n);for(let n of o)t.Utils.GE(n.x,e.start.x)&&r.push(n);return 2==o.length&&1==r.length&&e.start.on(s)&&r.push(e.start),r}intersectRay2Arc(e,n){let r=[];if(e.box.not_intersect(n.box))return r;let s=new i(e.start,e.norm).intersect(n);for(let n of s)t.Utils.GE(n.x,e.start.x)&&r.push(n);return r}},t.ray=((...e)=>new t.Ray(...e))}},function(t,e,n){"use strict";t.exports=function(t){let{Polygon:e,Point:n,Segment:i,Arc:r,Line:s,Ray:o}=t;t.ray_shoot=function(e,n){let r=void 0;if(e.box.not_intersect(n.box))return t.OUTSIDE;let l=new o(n),h=new s(l.pt,l.norm),a=e.edges.search(l.box);if(0==a.length)return t.OUTSIDE;let c=[];for(let e of a)for(let i of l.intersect(e.shape)){if(i.equalTo(n))return t.BOUNDARY;c.push({pt:i,edge:e})}c.sort((e,n)=>t.Utils.LT(e.pt.x,n.pt.x)?-1:t.Utils.GT(e.pt.x,n.pt.x)?1:0);let u=0;for(let e=0;e<c.length;e++){let n=c[e];if(n.pt.equalTo(n.edge.shape.start)){if(e>0&&n.pt.equalTo(c[e-1].pt)&&n.edge.prev===c[e-1].edge)continue;let i=n.edge.prev;for(;t.Utils.EQ_0(i.length);)i=i.prev;let r=i.shape.tangentInEnd(),s=n.pt.translate(r),o=n.edge.shape.tangentInStart(),l=n.pt.translate(o),a=s.leftTo(h),p=l.leftTo(h);(a&&!p||!a&&p)&&u++}else if(n.pt.equalTo(n.edge.shape.end)){if(e>0&&n.pt.equalTo(c[e-1].pt)&&n.edge.next===c[e-1].edge)continue;let i=n.edge.next;for(;t.Utils.EQ_0(i.length);)i=i.next;let r=i.shape.tangentInStart(),s=n.pt.translate(r),o=n.edge.shape.tangentInEnd(),l=n.pt.translate(o),a=s.leftTo(h),p=l.leftTo(h);(a&&!p||!a&&p)&&u++}else if(n.edge.shape instanceof i)u++;else{let e=n.edge.shape.box;t.Utils.EQ(n.pt.y,e.ymin)||t.Utils.EQ(n.pt.y,e.ymax)||u++}}return r=u%2==1?t.INSIDE:t.OUTSIDE}}},function(t,e,n){"use strict";t.exports=function(t){let{Edge:e,Face:n,PlanarSet:i,Box:r}=t,{ray_shoot:s}=t;t.Polygon=class e{constructor(){this.faces=new i,this.edges=new i}get box(){return[...this.faces].reduce((t,e)=>t.merge(e.box),new r)}get vertices(){return[...this.edges].map(t=>t.start)}isEmpty(){return 0===this.edges.size}addFace(...t){let e=new n(this,...t);return this.faces.add(e),e}deleteFace(t){for(let e of t){this.edges.delete(e)}return this.faces.delete(t)}removeChain(t,e,n){if(n.next!==e){for(let i=e;i!==n.next;i=i.next)if(t.remove(this.edges,i),t.isEmpty()){this.deleteFace(t);break}}else this.deleteFace(t)}addVertex(e,n){let i=n.shape.split(e);if(i.length<2)return;let r=new t.Edge(i[0]),s=n.prev;return n.face.insert(this.edges,r,s),this.edges.delete(n),n.shape=i[1],this.edges.add(n),r}reverse(){for(let t of this.faces)t.reverse();return this}clone(){let t=new e;for(let e of this.faces){let n=[];for(let t of e)n.push(t.shape.clone());t.addFace(n)}return t}area(){let t=[...this.faces].reduce((t,e)=>t+e.signedArea(),0);return Math.abs(t)}contains(e){let n=s(this,e);return n==t.INSIDE||n==t.BOUNDARY}distanceTo(e){let{Distance:n}=t;if(e instanceof t.Point){let[t,i]=n.point2polygon(e,this);return[t,i=i.reverse()]}if(e instanceof t.Circle||e instanceof t.Line||e instanceof t.Segment||e instanceof t.Arc){let[t,i]=n.shape2polygon(e,this);return[t,i=i.reverse()]}if(e instanceof t.Polygon){let i,r,s=[Number.POSITIVE_INFINITY,new t.Segment];for(let o of this.edges){let l=s[0];[i,r]=n.shape2planarSet(o.shape,e.edges,l),t.Utils.LT(i,l)&&(s=[i,r])}return s}}intersect(n){return n instanceof t.Point?this.contains(n)?[n]:[]:n instanceof t.Line?e.intersectLine2Polygon(n,this):n instanceof t.Circle||n instanceof t.Segment||n instanceof t.Arc?e.intersectShape2Polygon(n,this):n instanceof t.Polygon?e.intersectPolygon2Polygon(n,this):void 0}isValid(){let t=!0;for(let e of this.faces)if(!e.isSimple(this.edges)){t=!1;break}return t}translate(t){let n=new e;for(let e of this.faces){let i=[];for(let n of e)i.push(n.shape.translate(t));n.addFace(i)}return n}rotate(n=0,i=new t.Point){let r=new e;for(let t of this.faces){let e=[];for(let r of t)e.push(r.shape.rotate(n,i));r.addFace(e)}return r}transform(n=new t.Matrix){let i=new e;for(let t of this.faces){let e=[];for(let i of t)e.push(i.shape.transform(n));i.addFace(e)}return i}static intersectShape2Polygon(t,e){let n=[];if(e.isEmpty()||t.box.not_intersect(e.box))return n;let i=e.edges.search(t.box);for(let e of i)for(let i of t.intersect(e.shape))n.push(i);return n}static intersectLine2Polygon(t,e){let n=[];if(e.isEmpty())return n;for(let i of e.edges)for(let e of t.intersect(i.shape))n.push(e);return n}static intersectPolygon2Polygon(t,n){let i=[];if(t.isEmpty()||n.isEmpty())return i;if(t.box.not_intersect(n.box))return i;for(let r of t.edges)for(let t of e.intersectShape2Polygon(r.shape,n))i.push(t);return i}svg(t={}){let{stroke:e,strokeWidth:n,fill:i,fillRule:r,fillOpacity:s,id:o,className:l}=t,h=o&&o.length>0?`id="${o}"`:"",a=l&&l.length>0?`class="${l}"`:"",c=`\n<path stroke="${e||"black"}" stroke-width="${n||1}" fill="${i||"lightcyan"}" fill-rule="${r||"evenodd"}" fill-opacity="${s||1}" ${h} ${a} d="`;for(let t of this.faces)c+=t.svg();return c+='" >\n</path>'}toJSON(){return[...this.faces].map(t=>t.toJSON())}}}},function(t,e,n){"use strict";let i=n(0);t.exports=function(t){let{Polygon:e,Point:n,Segment:r,Arc:s,Circle:o,Line:l,Ray:h,Vector:a}=t,{vector:c}=t;t.Distance=class e{static point2point(t,e){return t.distanceTo(e)}static point2line(t,e){let n=t.projectionOn(e);return[c(t,n).length,new r(t,n)]}static point2circle(e,n){let[i,s]=e.distanceTo(n.center);if(t.Utils.EQ_0(i))return[n.r,new r(e,n.toArc().start)];{let t=Math.abs(i-n.r),s=c(n.pc,e).normalize().multiply(n.r),o=n.pc.translate(s);return[t,new r(e,o)]}}static point2segment(n,i){if(i.start.equalTo(i.end))return e.point2point(n,i.start);let s,o,l=new t.Vector(i.start,i.end),h=new t.Vector(i.start,n),a=new t.Vector(i.end,n),c=l.dot(h),u=-l.dot(a);if(t.Utils.GE(c,0)&&t.Utils.GE(u,0)){let t=i.tangentInStart();return s=Math.abs(t.cross(h)),o=i.start.translate(t.multiply(t.dot(h))),[s,new r(n,o)]}return c<0?n.distanceTo(i.start):n.distanceTo(i.end)}static point2arc(n,i){let r,s,o=new t.Circle(i.pc,i.r),l=[];return[r,s]=e.point2circle(n,o),s.end.on(i)&&l.push(e.point2circle(n,o)),l.push(e.point2point(n,i.start)),l.push(e.point2point(n,i.end)),e.sort(l),l[0]}static segment2line(t,n){let i=t.intersect(n);if(i.length>0)return[0,new r(i[0],i[0])];let s=[];return s.push(e.point2line(t.start,n)),s.push(e.point2line(t.end,n)),e.sort(s),s[0]}static segment2segment(t,n){let i=r.intersectSegment2Segment(t,n);if(i.length>0)return[0,new r(i[0],i[0])];let s=[];return s.push(e.point2segment(n.start,t)),s.push(e.point2segment(n.end,t)),s.push(e.point2segment(t.start,n)),s.push(e.point2segment(t.end,n)),e.sort(s),s[0]}static segment2circle(n,i){let s=n.intersect(i);if(s.length>0)return[0,new r(s[0],s[0])];let o=new t.Line(n.ps,n.pe),[l,h]=e.point2line(i.center,o);if(t.Utils.GE(l,i.r)&&h.end.on(n))return e.point2circle(h.end,i);{let[r,s]=e.point2circle(n.start,i),[o,l]=e.point2circle(n.end,i);return t.Utils.LT(r,o)?[r,s]:[o,l]}}static segment2arc(n,i){let s=n.intersect(i);if(s.length>0)return[0,new r(s[0],s[0])];let o=new t.Line(n.ps,n.pe),l=new t.Circle(i.pc,i.r),[h,a]=e.point2line(l.center,o);if(t.Utils.GE(h,l.r)&&a.end.on(n)){let[t,n]=e.point2circle(a.end,l);if(n.end.on(i))return[t,n]}let c,u,p=[];return p.push(e.point2arc(n.start,i)),p.push(e.point2arc(n.end,i)),[c,u]=e.point2segment(i.start,n),p.push([c,u.reverse()]),[c,u]=e.point2segment(i.end,n),p.push([c,u.reverse()]),e.sort(p),p[0]}static circle2circle(t,n){let i=t.intersect(n);if(i.length>0)return[0,new r(i[0],i[0])];if(t.center.equalTo(n.center)){let i=t.toArc(),r=n.toArc();return e.point2point(i.start,r.start)}{let i=new l(t.center,n.center),r=i.intersect(t),s=i.intersect(n),o=[];return o.push(e.point2point(r[0],s[0])),o.push(e.point2point(r[0],s[1])),o.push(e.point2point(r[1],s[0])),o.push(e.point2point(r[1],s[1])),e.sort(o),o[0]}}static circle2line(t,n){let i=t.intersect(n);if(i.length>0)return[0,new r(i[0],i[0])];let[s,o]=e.point2line(t.center,n),[l,h]=e.point2circle(o.end,t);return[l,h=h.reverse()]}static arc2line(n,i){let s=i.intersect(n);if(s.length>0)return[0,new r(s[0],s[0])];let o=new t.Circle(n.center,n.r),[l,h]=e.point2line(o.center,i);if(!t.Utils.GE(l,o.r)){let t=[];return t.push(e.point2line(n.start,i)),t.push(e.point2line(n.end,i)),e.sort(t),t[0]}{let[t,i]=e.point2circle(h.end,o);if(i.end.on(n))return[t,i]}}static arc2circle(n,i){let s=n.intersect(i);if(s.length>0)return[0,new r(s[0],s[0])];let o=new t.Circle(n.center,n.r),[l,h]=e.circle2circle(o,i);if(h.start.on(n))return[l,h];{let t=[];return t.push(e.point2circle(n.start,i)),t.push(e.point2circle(n.end,i)),e.sort(t),t[0]}}static arc2arc(n,i){let s=n.intersect(i);if(s.length>0)return[0,new r(s[0],s[0])];let o=new t.Circle(n.center,n.r),l=new t.Circle(i.center,i.r),[h,a]=e.circle2circle(o,l);if(a.start.on(n)&&a.end.on(i))return[h,a];{let t,r,s=[];return[t,r]=e.point2arc(n.start,i),r.end.on(i)&&s.push([t,r]),[t,r]=e.point2arc(n.end,i),r.end.on(i)&&s.push([t,r]),[t,r]=e.point2arc(i.start,n),r.end.on(n)&&s.push([t,r.reverse()]),[t,r]=e.point2arc(i.end,n),r.end.on(n)&&s.push([t,r.reverse()]),[t,r]=e.point2point(n.start,i.start),s.push([t,r]),[t,r]=e.point2point(n.start,i.end),s.push([t,r]),[t,r]=e.point2point(n.end,i.start),s.push([t,r]),[t,r]=e.point2point(n.end,i.end),s.push([t,r]),e.sort(s),s[0]}}static poin