UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 3.81 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{GeometryCursor as t}from"../../geometry/GeometryCursor.js";import{GeometricEffectOffsetMethod as e}from"./enums.js";const n=.03;class s{constructor(t=0,e=!1){}isEmpty(t){if(!t.nextPoint())return!0;let e,n,s,r;for(e=t.x,n=t.y;t.nextPoint();e=n,n=r)if(s=t.x,r=t.y,s!==e||r!==n)return t.seekPathStart(),!1;return t.seekPathStart(),!0}normalize(t){const e=Math.sqrt(t[0]*t[0]+t[1]*t[1]);0!==e&&(t[0]/=e,t[1]/=e)}getLength(t,e,n,s){const r=n-t,o=s-e;return Math.sqrt(r*r+o*o)}getSegLength(t){const[[e,n],[s,r]]=t;return this.getLength(e,n,s,r)}getCoord2D(t,e,n,s,r){return[t+(n-t)*r,e+(s-e)*r]}getSegCoord2D(t,e){const[[n,s],[r,o]]=t;return this.getCoord2D(n,s,r,o,e)}getAngle(t,e,n,s,r){const o=n-t,i=s-e;return Math.atan2(i,o)}getAngleCS(t,e,n,s,r,o){null==t&&(t=[0,0]);const i=s-e,u=r-n,h=Math.sqrt(i*i+u*u);return 0!==h?(t[0]=i/h,t[1]=u/h,t):(t[0]=1,t[1]=0,t)}getSegAngleCS(t,e,n){const[[s,r],[o,i]]=e;return this.getAngleCS(t,s,r,o,i,n)}cut(t,e,n,s,r,o){return[r<=0?[t,e]:this.getCoord2D(t,e,n,s,r),o>=1?[n,s]:this.getCoord2D(t,e,n,s,o)]}getSubCurve(e,n,s){const r=t.createEmptyOptimizedCIM("esriGeometryPolyline");return this.appendSubCurve(r,e,n,s)?r:null}appendSubCurve(t,e,n,s){t.startPath(),e.seekPathStart();let r=0,o=!0;if(!e.nextPoint())return!1;let i=e.x,u=e.y;for(;e.nextPoint();){const h=this.getLength(i,u,e.x,e.y);if(0!==h){if(o){if(r+h>n){const l=(n-r)/h;let a=1,c=!1;r+h>=s&&(a=(s-r)/h,c=!0);const f=this.cut(i,u,e.x,e.y,l,a);if(f&&t.pushPoints(f),c)break;o=!1}}else{if(r+h>s){const n=this.cut(i,u,e.x,e.y,0,(s-r)/h);n&&t.pushPoint(n[1]);break}t.pushXY(e.x,e.y)}r+=h,i=e.x,u=e.y}else i=e.x,u=e.y}return!0}getCIMPointAlong(t,e){if(!t.nextPoint())return null;let n,s,r,o,i=0;for(n=t.x,s=t.y;t.nextPoint();n=r,s=o){r=t.x,o=t.y;const u=this.getLength(n,s,r,o);if(0!==u){if(i+u>e){const t=(e-i)/u;return this.getCoord2D(n,s,r,o,t)}i+=u}}return null}offset(t,n,s,r,o){if(!t||t.length<2)return null;let i=0,u=t[i++],h=i;for(;i<t.length;){const e=t[i];e[0]===u[0]&&e[1]===u[1]||(i!==h&&(t[h]=t[i]),u=t[h++]),i++}const l=t[0][0]===t[h-1][0]&&t[0][1]===t[h-1][1];if(l&&--h,h<(l?3:2))return null;const a=[];u=l?t[h-1]:null;let c=t[0];for(let f=0;f<h;f++){const o=f===h-1?l?t[0]:null:t[f+1];if(u)if(o){const t=[o[0]-c[0],o[1]-c[1]];this.normalize(t);const i=[c[0]-u[0],c[1]-u[1]];this.normalize(i);const h=i[0]*t[1]-i[1]*t[0],l=i[0]*t[0]+i[1]*t[1];if(0===h&&1===l){c=o;continue}if(h>=0==n<=0){if(l<1){const e=[t[0]-i[0],t[1]-i[1]];this.normalize(e);const s=Math.sqrt((1+l)/2);if(s>1/r){const t=-Math.abs(n)/s;a.push([c[0]-e[0]*t,c[1]-e[1]*t])}}}else switch(s){case e.Mitered:{const e=Math.sqrt((1+l)/2);if(e>0&&1/e<r){const s=[t[0]-i[0],t[1]-i[1]];this.normalize(s);const r=Math.abs(n)/e;a.push([c[0]-s[0]*r,c[1]-s[1]*r]);break}}case e.Bevelled:a.push([c[0]+i[1]*n,c[1]-i[0]*n]),a.push([c[0]+t[1]*n,c[1]-t[0]*n]);break;case e.Rounded:if(l<1){a.push([c[0]+i[1]*n,c[1]-i[0]*n]);const e=Math.floor(2.5*(1-l));if(e>0){const s=1/e;let r=s;for(let o=1;o<e;o++,r+=s){const e=[i[1]*(1-r)+t[1]*r,-i[0]*(1-r)-t[0]*r];this.normalize(e),a.push([c[0]+e[0]*n,c[1]+e[1]*n])}}a.push([c[0]+t[1]*n,c[1]-t[0]*n])}break;case e.Square:default:if(h<0)a.push([c[0]+(i[1]+i[0])*n,c[1]+(i[1]-i[0])*n]),a.push([c[0]+(t[1]-t[0])*n,c[1]-(t[0]+t[1])*n]);else{const e=Math.sqrt((1+Math.abs(l))/2),s=[t[0]-i[0],t[1]-i[1]];this.normalize(s);const r=n/e;a.push([c[0]-s[0]*r,c[1]-s[1]*r])}}}else{const t=[c[0]-u[0],c[1]-u[1]];this.normalize(t),a.push([c[0]+t[1]*n,c[1]-t[0]*n])}else{const t=[o[0]-c[0],o[1]-c[1]];this.normalize(t),a.push([c[0]+t[1]*n,c[1]-t[0]*n])}u=c,c=o}return a.length<(l?3:2)?null:(l&&a.push([a[0][0],a[0][1]]),a)}}export{s as CurveHelper,n as pixelTolerance};