12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.25/esri/copyright.txt for details.
- */
- import{GeometricEffectOffsetMethod as t}from"./enums.js";const e=.03;class s{constructor(){this._path=[]}path(){return this._path}addPath(t,e){e||t.reverse(),Array.prototype.push.apply(this._path,t),e||t.reverse()}static mergePath(t,e){e&&Array.prototype.push.apply(t,e)}startPath(t){this._path.push(t)}lineTo(t){this._path.push(t)}close(){const t=this._path;t.length>1&&(t[0][0]===t[t.length-1][0]&&t[0][1]===t[t.length-1][1]||t.push([t[0][0],t[0][1]]))}}class n{constructor(t=0,e=!1){}normalize(t){const e=Math.sqrt(t[0]*t[0]+t[1]*t[1]);0!==e&&(t[0]/=e,t[1]/=e)}calculateLength(t,e){const s=e[0]-t[0],n=e[1]-t[1];return Math.sqrt(s*s+n*n)}calculateSegLength(t,e){return this.calculateLength(t[e],t[e+1])}calculatePathLength(t){let e=0;const s=t?t.length:0;for(let n=0;n<s-1;++n)e+=this.calculateSegLength(t,n);return e}calculatePathArea(t){let e=0;const s=t?t.length:0;for(let n=0;n<s-1;++n)e+=(t[n+1][0]-t[n][0])*(t[n+1][1]+t[n][1]);return e/2}getCoord2D(t,e,s){return[t[0]+(e[0]-t[0])*s,t[1]+(e[1]-t[1])*s]}getSegCoord2D(t,e,s){return this.getCoord2D(t[e],t[e+1],s)}getAngle(t,e,s){const n=e[0]-t[0],r=e[1]-t[1];return Math.atan2(r,n)}getSegAngle(t,e,s){return this.getAngle(t[e],t[e+1],s)}getAngleCS(t,e,s){const n=e[0]-t[0],r=e[1]-t[1],h=Math.sqrt(n*n+r*r);return h>0?[n/h,r/h]:[1,0]}getSegAngleCS(t,e,s){return this.getAngleCS(t[e],t[e+1],s)}cut(t,e,s,n){return[s<=0?t[e]:this.getSegCoord2D(t,e,s),n>=1?t[e+1]:this.getSegCoord2D(t,e,n)]}addSegment(t,e,s){s&&t.push(e[0]),t.push(e[1])}getSubCurve(t,e,s){const n=[];return this.appendSubCurve(n,t,e,s)?n:null}appendSubCurve(t,e,s,n){const r=e?e.length-1:0;let h=0,l=!0,o=0;for(;o<r;){const r=this.calculateSegLength(e,o);if(0!==r){if(l){if(h+r>s){const a=(s-h)/r;let u=1,c=!1;h+r>=n&&(u=(n-h)/r,c=!0);const i=this.cut(e,o,a,u);if(i&&this.addSegment(t,i,l),c)break;l=!1}}else{if(h+r>n){const s=this.cut(e,o,0,(n-h)/r);s&&this.addSegment(t,s,l);break}this.addSegment(t,[e[o],e[o+1]],l)}h+=r,++o}else++o}return!0}getCIMPointAlong(t,e){const s=t?t.length-1:0;let n=0,r=-1;for(;r<s;){++r;const s=this.calculateSegLength(t,r);if(0!==s){if(n+s>e){const h=(e-n)/s;return this.getCoord2D(t[r],t[r+1],h)}n+=s}}return null}isEmpty(t,e){if(!t||t.length<=1)return!0;const s=t?t.length-1:0;let n=-1;for(;n<s;){if(++n,t[n+1][0]!==t[n][0]||t[n+1][1]!==t[n][1])return!1;if(e&&t[n+1][2]!==t[n][2])return!1}return!0}offset(e,s,n,r,h){if(!e||e.length<2)return null;let l=0,o=e[l++],a=l;for(;l<e.length;){const t=e[l];t[0]===o[0]&&t[1]===o[1]||(l!==a&&(e[a]=e[l]),o=e[a++]),l++}const u=e[0][0]===e[a-1][0]&&e[0][1]===e[a-1][1];if(u&&--a,a<(u?3:2))return null;const c=[];o=u?e[a-1]:null;let i=e[0];for(let g=0;g<a;g++){const h=g===a-1?u?e[0]:null:e[g+1];if(o)if(h){const e=[h[0]-i[0],h[1]-i[1]];this.normalize(e);const l=[i[0]-o[0],i[1]-o[1]];this.normalize(l);const a=l[0]*e[1]-l[1]*e[0],u=l[0]*e[0]+l[1]*e[1];if(0===a&&1===u){i=h;continue}if(a>=0==s<=0){if(u<1){const t=[e[0]-l[0],e[1]-l[1]];this.normalize(t);const n=Math.sqrt((1+u)/2);if(n>1/r){const e=-Math.abs(s)/n;c.push([i[0]-t[0]*e,i[1]-t[1]*e])}}}else switch(n){case t.Mitered:{const t=Math.sqrt((1+u)/2);if(t>0&&1/t<r){const n=[e[0]-l[0],e[1]-l[1]];this.normalize(n);const r=Math.abs(s)/t;c.push([i[0]-n[0]*r,i[1]-n[1]*r]);break}}case t.Bevelled:c.push([i[0]+l[1]*s,i[1]-l[0]*s]),c.push([i[0]+e[1]*s,i[1]-e[0]*s]);break;case t.Rounded:if(u<1){c.push([i[0]+l[1]*s,i[1]-l[0]*s]);const t=Math.floor(2.5*(1-u));if(t>0){const n=1/t;let r=n;for(let h=1;h<t;h++,r+=n){const t=[l[1]*(1-r)+e[1]*r,-l[0]*(1-r)-e[0]*r];this.normalize(t),c.push([i[0]+t[0]*s,i[1]+t[1]*s])}}c.push([i[0]+e[1]*s,i[1]-e[0]*s])}break;case t.Square:default:if(a<0)c.push([i[0]+(l[1]+l[0])*s,i[1]+(l[1]-l[0])*s]),c.push([i[0]+(e[1]-e[0])*s,i[1]-(e[0]+e[1])*s]);else{const t=Math.sqrt((1+Math.abs(u))/2),n=[e[0]-l[0],e[1]-l[1]];this.normalize(n);const r=s/t;c.push([i[0]-n[0]*r,i[1]-n[1]*r])}}}else{const t=[i[0]-o[0],i[1]-o[1]];this.normalize(t),c.push([i[0]+t[1]*s,i[1]-t[0]*s])}else{const t=[h[0]-i[0],h[1]-i[1]];this.normalize(t),c.push([i[0]+t[1]*s,i[1]-t[0]*s])}o=i,i=h}return c.length<(u?3:2)?null:(u&&c.push([c[0][0],c[0][1]]),c)}}export{n as CurveHelper,e as PIXEL_TOLERANCE,s as PathHelper};
|