d3-delaunay
Advanced tools
Comparing version 3.0.0-alpha.3 to 3.0.0
@@ -1,2 +0,2 @@ | ||
// https://github.com/d3/d3-delaunay Version 3.0.0-alpha.3. Copyright 2018 Observable, Inc. | ||
// https://github.com/d3/d3-delaunay Version 3.0.0. Copyright 2018 Observable, Inc. | ||
// https://github.com/mapbox/delaunator Version 2.0.0. Copyright 2017, Mapbox, Inc. | ||
@@ -459,2 +459,38 @@ (function (global, factory) { | ||
class Path { | ||
constructor() { | ||
this._x0 = this._y0 = // start of current subpath | ||
this._x1 = this._y1 = null; // end of current subpath | ||
this._ = ""; | ||
} | ||
moveTo(x, y) { | ||
this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`; | ||
} | ||
closePath() { | ||
if (this._x1 !== null) { | ||
this._x1 = this._x0, this._y1 = this._y0; | ||
this._ += "Z"; | ||
} | ||
} | ||
lineTo(x, y) { | ||
this._ += `L${this._x1 = +x},${this._y1 = +y}`; | ||
} | ||
arc(x, y, r) { | ||
x = +x, y = +y, r = +r; | ||
const x0 = x + r; | ||
const y0 = y; | ||
if (r < 0) throw new Error("negative radius"); | ||
if (this._x1 === null) this._ += `M${x0},${y0}`; | ||
else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) this._ += "L" + x0 + "," + y0; | ||
if (!r) return; | ||
this._ += `A${r},${r},0,1,1,${x - r},${y}A${r},${r},0,1,1,${this._x1 = x0},${this._y1 = y0}`; | ||
} | ||
rect(x, y, w, h) { | ||
this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${+w}v${+h}h${-w}Z`; | ||
} | ||
value() { | ||
return this._ || null; | ||
} | ||
} | ||
class Voronoi { | ||
@@ -534,2 +570,3 @@ constructor(delaunay, [xmin, ymin, xmax, ymax] = [0, 0, 960, 500]) { | ||
render(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {delaunay: {halfedges, hull, triangles}, circumcenters, vectors} = this; | ||
@@ -555,7 +592,11 @@ for (let i = 0, n = halfedges.length; i < n; ++i) { | ||
} | ||
return buffer && buffer.value(); | ||
} | ||
renderBounds(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
context.rect(this.xmin, this.ymin, this.xmax - this.xmin, this.ymax - this.ymin); | ||
return buffer && buffer.value(); | ||
} | ||
renderCell(i, context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const points = this._clip(i); | ||
@@ -568,2 +609,3 @@ if (points === null) return; | ||
context.closePath(); | ||
return buffer && buffer.value(); | ||
} | ||
@@ -781,2 +823,3 @@ _renderSegment(x0, y0, x1, y1, context) { | ||
render(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points, halfedges, triangles} = this; | ||
@@ -792,4 +835,6 @@ for (let i = 0, n = halfedges.length; i < n; ++i) { | ||
this.renderHull(context); | ||
return buffer && buffer.value(); | ||
} | ||
renderPoints(context, r = 2) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points} = this; | ||
@@ -801,4 +846,6 @@ for (let i = 0, n = points.length; i < n; i += 2) { | ||
} | ||
return buffer && buffer.value(); | ||
} | ||
renderHull(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points, hull, triangles} = this; | ||
@@ -812,4 +859,6 @@ const n = hull.length; | ||
} | ||
return buffer && buffer.value(); | ||
} | ||
renderTriangle(i, context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points, triangles} = this; | ||
@@ -823,2 +872,3 @@ const t0 = triangles[i *= 3] * 2; | ||
context.closePath(); | ||
return buffer && buffer.value(); | ||
} | ||
@@ -825,0 +875,0 @@ } |
@@ -1,3 +0,3 @@ | ||
// https://github.com/d3/d3-delaunay Version 3.0.0-alpha.3. Copyright 2018 Observable, Inc. | ||
// https://github.com/d3/d3-delaunay Version 3.0.0. Copyright 2018 Observable, Inc. | ||
// https://github.com/mapbox/delaunator Version 2.0.0. Copyright 2017, Mapbox, Inc. | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.d3=t.d3||{})}(this,function(t){"use strict";function e(t){if(!ArrayBuffer.isView(t))throw new Error("Expected coords to be a typed array.");var e=1/0,a=1/0,c=-1/0,f=-1/0,u=t.length>>1,x=this.ids=new Uint32Array(u);this.coords=t;for(var g=0;g<u;g++){var d=t[2*g],m=t[2*g+1];d<e&&(e=d),m<a&&(a=m),d>c&&(c=d),m>f&&(f=m),x[g]=g}var y,_,v,p=(e+c)/2,w=(a+f)/2,b=1/0;for(g=0;g<u;g++){var T=i(p,w,t[2*g],t[2*g+1]);T<b&&(y=g,b=T)}for(b=1/0,g=0;g<u;g++)g!==y&&(T=i(t[2*y],t[2*y+1],t[2*g],t[2*g+1]))<b&&T>0&&(_=g,b=T);var k=1/0;for(g=0;g<u;g++)if(g!==y&&g!==_){var A=s(t[2*y],t[2*y+1],t[2*_],t[2*_+1],t[2*g],t[2*g+1]);A<k&&(v=g,k=A)}if(k===1/0)throw new Error("No Delaunay triangulation exists for this input.");if(n(t[2*y],t[2*y+1],t[2*_],t[2*_+1],t[2*v],t[2*v+1])<0){var M=_;_=v,v=M}var S=t[2*y],E=t[2*y+1],z=t[2*_],F=t[2*_+1],j=t[2*v],L=t[2*v+1],P=function(t,e,i,n,s,r){var h=(i-=t)*i+(n-=e)*n,o=(s-=t)*s+(r-=e)*r,l=i*r-n*s;return{x:t+.5*(r*h-n*o)/l,y:e+.5*(i*o-s*h)/l}}(S,E,z,F,j,L);for(this._cx=P.x,this._cy=P.y,function t(e,i,n,s,r,h){var a,c,f;if(s-n<=20)for(a=n+1;a<=s;a++){for(f=e[a],c=a-1;c>=n&&o(i,e[c],f,r,h)>0;)e[c+1]=e[c--];e[c+1]=f}else{var u=n+s>>1;for(c=s,l(e,u,a=n+1),o(i,e[n],e[s],r,h)>0&&l(e,n,s),o(i,e[a],e[s],r,h)>0&&l(e,a,s),o(i,e[n],e[a],r,h)>0&&l(e,n,a),f=e[a];;){do{a++}while(o(i,e[a],f,r,h)<0);do{c--}while(o(i,e[c],f,r,h)>0);if(c<a)break;l(e,a,c)}e[n+1]=e[c],e[c]=f,s-a+1>=c-n?(t(e,i,a,s,r,h),t(e,i,n,c-1,r,h)):(t(e,i,n,c-1,r,h),t(e,i,a,s,r,h))}}(x,t,0,x.length-1,P.x,P.y),this._hashSize=Math.ceil(Math.sqrt(u)),this._hash=[],g=0;g<this._hashSize;g++)this._hash[g]=null;var U=this.hull=r(t,y);this._hashEdge(U),U.t=0,U=r(t,_,U),this._hashEdge(U),U.t=1,U=r(t,v,U),this._hashEdge(U),U.t=2;var I,K,B=2*u-5,D=this.triangles=new Uint32Array(3*B),H=this.halfedges=new Int32Array(3*B);this.trianglesLen=0,this._addTriangle(y,_,v,-1,-1,-1);for(var V=0;V<x.length;V++)if(d=t[2*(g=x[V])],m=t[2*g+1],!(d===I&&m===K||(I=d,K=m,d===S&&m===E||d===z&&m===F||d===j&&m===L))){var q,C=this._hashKey(d,m),N=C;do{q=this._hash[N],N=(N+1)%this._hashSize}while((!q||q.removed)&&N!==C);for(U=q;n(d,m,U.x,U.y,U.next.x,U.next.y)>=0;)if((U=U.next)===q)throw new Error("Something is wrong with the input points.");var O=U===q,G=this._addTriangle(U.i,g,U.next.i,-1,-1,U.t);U.t=G,(U=r(t,g,U)).t=this._legalize(G+2),U.prev.prev.t===H[G+1]&&(U.prev.prev.t=G+2);for(var J=U.next;n(d,m,J.x,J.y,J.next.x,J.next.y)<0;)G=this._addTriangle(J.i,g,J.next.i,J.prev.t,-1,J.t),J.prev.t=this._legalize(G+2),this.hull=h(J),J=J.next;if(O)for(J=U.prev;n(d,m,J.prev.x,J.prev.y,J.x,J.y)<0;)G=this._addTriangle(J.prev.i,g,J.i,-1,J.t,J.prev.t),this._legalize(G+2),J.prev.t=G,this.hull=h(J),J=J.prev;this._hashEdge(U),this._hashEdge(U.prev)}this.triangles=D.subarray(0,this.trianglesLen),this.halfedges=H.subarray(0,this.trianglesLen)}function i(t,e,i,n){var s=t-i,r=e-n;return s*s+r*r}function n(t,e,i,n,s,r){return(n-e)*(s-i)-(i-t)*(r-n)}function s(t,e,i,n,s,r){var h=(i-=t)*i+(n-=e)*n,o=(s-=t)*s+(r-=e)*r;if(0===h||0===o)return 1/0;var l=i*r-n*s;if(0===l)return 1/0;var a=.5*(r*h-n*o)/l,c=.5*(i*o-s*h)/l;return a*a+c*c}function r(t,e,i){var n={i:e,x:t[2*e],y:t[2*e+1],t:0,prev:null,next:null,removed:!1};return i?(n.next=i.next,n.prev=i,i.next.prev=n,i.next=n):(n.prev=n,n.next=n),n}function h(t){return t.prev.next=t.next,t.next.prev=t.prev,t.removed=!0,t.prev}function o(t,e,n,s,r){return i(t[2*e],t[2*e+1],s,r)-i(t[2*n],t[2*n+1],s,r)||t[2*e]-t[2*n]||t[2*e+1]-t[2*n+1]}function l(t,e,i){var n=t[e];t[e]=t[i],t[i]=n}function a(t){return t[0]}function c(t){return t[1]}e.from=function(t,i,n){i||(i=a),n||(n=c);for(var s=t.length,r=new Float64Array(2*s),h=0;h<s;h++){var o=t[h];r[2*h]=i(o),r[2*h+1]=n(o)}return new e(r)},e.prototype={_hashEdge:function(t){this._hash[this._hashKey(t.x,t.y)]=t},_hashKey:function(t,e){var i=t-this._cx,n=e-this._cy,s=1-i/(Math.abs(i)+Math.abs(n));return Math.floor((2+(n<0?-s:s))/4*this._hashSize)},_legalize:function(t){var e,i,n,s,r,h,o,l,a,c,f=this.triangles,u=this.coords,x=this.halfedges,g=x[t],d=t-t%3,m=g-g%3,y=d+(t+1)%3,_=d+(t+2)%3,v=m+(g+2)%3,p=f[_],w=f[t],b=f[y],T=f[v];if(e=u[2*p],i=u[2*p+1],n=u[2*w],s=u[2*w+1],r=u[2*b],h=u[2*b+1],o=u[2*T],l=u[2*T+1],a=(n-=o)*n+(s-=l)*s,c=(r-=o)*r+(h-=l)*h,(e-=o)*(s*c-a*h)-(i-=l)*(n*c-a*r)+(e*e+i*i)*(n*h-s*r)<0){f[t]=T,f[g]=p,this._link(t,x[v]),this._link(g,x[_]),this._link(_,v);var k=m+(g+1)%3;return this._legalize(t),this._legalize(k)}return _},_link:function(t,e){this.halfedges[t]=e,-1!==e&&(this.halfedges[e]=t)},_addTriangle:function(t,e,i,n,s,r){var h=this.trianglesLen;return this.triangles[h]=t,this.triangles[h+1]=e,this.triangles[h+2]=i,this._link(h,n),this._link(h+1,s),this._link(h+2,r),this.trianglesLen+=3,h}};class f{constructor(t,[e,i,n,s]=[0,0,960,500]){if(!((n=+n)>=(e=+e)&&(s=+s)>=(i=+i)))throw new Error("invalid bounds");const{points:r,halfedges:h,hull:o,triangles:l}=this.delaunay=t,a=this.circumcenters=new Float64Array(l.length/3*2),c=this.edges=new Uint32Array(h.length),f=this.index=new Uint32Array(r.length),u=this.vectors=new Float64Array(2*r.length);this.xmax=n,this.xmin=e,this.ymax=s,this.ymin=i;for(let t=0,e=0,i=h.length;t<i;++t){const i=l[t];if(f[2*i]!==f[2*i+1])continue;const n=f[2*i]=e;let s=t;do{if(c[e++]=Math.floor(s/3),-1===(s=h[s]))break;if(l[s=s%3==2?s-2:s+1]!==i)break}while(s!==t);if(s!==t){const r=e;for(s=t;-1!==(s=h[s%3==0?s+2:s-1])&&l[s]===i;)c[e++]=Math.floor(s/3);r<e&&(c.subarray(n,r).reverse(),c.subarray(n,e).reverse())}f[2*i+1]=e}for(let t=0,e=0,i=l.length;t<i;t+=3,e+=2){const i=2*l[t],n=2*l[t+1],s=2*l[t+2],h=r[i],o=r[i+1],c=r[n],f=r[n+1],u=r[s],x=r[s+1],g=h-c,d=h-u,m=o-f,y=o-x,_=h*h+o*o,v=_-c*c-f*f,p=_-u*u-x*x,w=2*(d*m-g*y);a[e]=(m*p-y*v)/w,a[e+1]=(d*v-g*p)/w}for(let t,e,i,n=o.length,s=2*l[o[n-1]],h=r[s],a=r[s+1],c=0;c<n;++c)t=s,e=h,i=a,h=r[s=2*l[o[c]]],a=r[s+1],u[2*t+2]=u[2*s]=i-a,u[2*t+3]=u[2*s+1]=h-e}render(t){const{delaunay:{halfedges:e,hull:i,triangles:n},circumcenters:s,vectors:r}=this;for(let i=0,n=e.length;i<n;++i){const n=e[i];if(n<i)continue;const r=2*Math.floor(i/3),h=2*Math.floor(n/3),o=s[r],l=s[r+1],a=s[h],c=s[h+1];this._renderSegment(o,l,a,c,t)}for(let e=0,h=i.length;e<h;++e){const h=2*Math.floor(i[e]/3),o=s[h],l=s[h+1],a=4*n[i[e]],c=this._project(o,l,r[a+2],r[a+3]);c&&this._renderSegment(o,l,c[0],c[1],t)}}renderBounds(t){t.rect(this.xmin,this.ymin,this.xmax-this.xmin,this.ymax-this.ymin)}renderCell(t,e){const i=this._clip(t);if(null!==i){e.moveTo(i[0],i[1]);for(let t=2,n=i.length;t<n;t+=2)e.lineTo(i[t],i[t+1]);e.closePath()}}_renderSegment(t,e,i,n,s){let r;const h=this._regioncode(t,e),o=this._regioncode(i,n);0===h&&0===o?(s.moveTo(t,e),s.lineTo(i,n)):(r=this._clipSegment(t,e,i,n,h,o))&&(s.moveTo(r[0],r[1]),s.lineTo(r[2],r[3]))}contains(t,e,i){return(e=+e)==e&&(i=+i)==i&&this._step(t,e,i)===t}find(t,e,i=0){if((t=+t)!=t||(e=+e)!=e)return-1;let n;for(;(n=this._step(i,t,e))>=0&&n!==i;)i=n;return n}_step(t,e,i){const{delaunay:{points:n,triangles:s},edges:r,index:h}=this;if(0===n.length)return-1;const o=h[2*t],l=h[2*t+1];if(o===l)return-1;let a=t,c=3*r[o],f=(e-n[2*a])**2+(i-n[2*a+1])**2;switch(t){case s[c]:c=s[c+2];break;case s[c+1]:c=s[c];break;case s[c+2]:c=s[c+1]}let u=(e-n[2*c])**2+(i-n[2*c+1])**2;u<f&&(f=u,a=c);for(let h=o;h<l;++h){switch(c=3*r[h],t){case s[c]:c=s[c+1];break;case s[c+1]:c=s[c+2];break;case s[c+2]:c=s[c]}(u=(e-n[2*c])**2+(i-n[2*c+1])**2)<f&&(f=u,a=c)}return a}_cell(t){const{index:e,edges:i,circumcenters:n}=this,s=e[2*t],r=e[2*t+1];if(s===r)return null;const h=new Float64Array(2*(r-s));for(let t=s,e=0;t<r;++t,e+=2){const s=2*i[t];h[e]=n[s],h[e+1]=n[s+1]}return h}_clip(t){const e=this._cell(t);if(null===e)return null;const{vectors:i}=this,n=4*t;return i[n]||i[n+1]?this._clipInfinite(t,e,i[n],i[n+1],i[n+2],i[n+3]):this._clipFinite(t,e)}_clipFinite(t,e){const i=e.length;let n,s,r,h,o,l=null,a=e[i-2],c=e[i-1],f=this._regioncode(a,c);for(let t=0;t<i;t+=2)if(n=a,s=c,a=e[t],c=e[t+1],r=f,f=this._regioncode(a,c),0===r&&0===f)h=o,o=0,l?l.push(a,c):l=[a,c];else{let e,i,u,x,g;if(0===r){if(null===(e=this._clipSegment(n,s,a,c,r,f)))continue;[i,u,x,g]=e}else{if(null===(e=this._clipSegment(a,c,n,s,f,r)))continue;[x,g,i,u]=e,h=o,o=this._edgecode(i,u),h&&o&&this._edge(t,h,o,l,l.length),l?l.push(i,u):l=[i,u]}h=o,o=this._edgecode(x,g),h&&o&&this._edge(t,h,o,l,l.length),l?l.push(x,g):l=[x,g]}if(l)h=o,o=this._edgecode(l[0],l[1]),h&&o&&this._edge(t,h,o,l,l.length);else if(this.contains(t,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2))return[this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax,this.xmin,this.ymin];return l}_clipSegment(t,e,i,n,s,r){for(;;){if(0===s&&0===r)return[t,e,i,n];if(s&r)return null;let h,o,l=s||r;8&l?(h=t+(i-t)*(this.ymax-e)/(n-e),o=this.ymax):4&l?(h=t+(i-t)*(this.ymin-e)/(n-e),o=this.ymin):2&l?(o=e+(n-e)*(this.xmax-t)/(i-t),h=this.xmax):(o=e+(n-e)*(this.xmin-t)/(i-t),h=this.xmin),s?(t=h,e=o,s=this._regioncode(t,e)):(i=h,n=o,r=this._regioncode(i,n))}}_clipInfinite(t,e,i,n,s,r){let h,o=Array.from(e);if((h=this._project(o[0],o[1],i,n))&&o.unshift(h[0],h[1]),(h=this._project(o[o.length-2],o[o.length-1],s,r))&&o.push(h[0],h[1]),o=this._clipFinite(t,o))for(let e,i=0,n=o.length,s=this._edgecode(o[n-2],o[n-1]);i<n;i+=2)e=s,s=this._edgecode(o[i],o[i+1]),e&&s&&(i=this._edge(t,e,s,o,i),n=o.length);else this.contains(t,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2)&&(o=[this.xmin,this.ymin,this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax]);return o}_edge(t,e,i,n,s){for(;e!==i;){let i,r;switch(e){case 5:e=4;continue;case 4:e=6,i=this.xmax,r=this.ymin;break;case 6:e=2;continue;case 2:e=10,i=this.xmax,r=this.ymax;break;case 10:e=8;continue;case 8:e=9,i=this.xmin,r=this.ymax;break;case 9:e=1;continue;case 1:e=5,i=this.xmin,r=this.ymin}this.contains(t,i,r)&&(n.splice(s,0,i,r),s+=2)}return s}_project(t,e,i,n){let s,r,h,o=1/0;if(n<0){if(e<=this.ymin)return null;(s=(this.ymin-e)/n)<o&&(h=this.ymin,r=t+(o=s)*i)}else if(n>0){if(e>=this.ymax)return null;(s=(this.ymax-e)/n)<o&&(h=this.ymax,r=t+(o=s)*i)}if(i>0){if(t>=this.xmax)return null;(s=(this.xmax-t)/i)<o&&(r=this.xmax,h=e+(o=s)*n)}else if(i<0){if(t<=this.xmin)return null;(s=(this.xmin-t)/i)<o&&(r=this.xmin,h=e+(o=s)*n)}return[r,h]}_edgecode(t,e){return(t===this.xmin?1:t===this.xmax?2:0)|(e===this.ymin?4:e===this.ymax?8:0)}_regioncode(t,e){return(t<this.xmin?1:t>this.xmax?2:0)|(e<this.ymin?4:e>this.ymax?8:0)}}const u=2*Math.PI;class x{constructor(t){const{halfedges:i,hull:n,triangles:s}=new e(t);this.points=t,this.halfedges=i,this.hull=Uint32Array.from(function*(t){let e=t;do{yield e.t}while((e=e.next)!==t)}(n)),this.triangles=s}voronoi(t){return new f(this,t)}render(t){const{points:e,halfedges:i,triangles:n}=this;for(let s=0,r=i.length;s<r;++s){const r=i[s];if(r<s)continue;const h=2*n[s],o=2*n[r];t.moveTo(e[h],e[h+1]),t.lineTo(e[o],e[o+1])}this.renderHull(t)}renderPoints(t,e=2){const{points:i}=this;for(let n=0,s=i.length;n<s;n+=2){const s=i[n],r=i[n+1];t.moveTo(s+e,r),t.arc(s,r,e,0,u)}}renderHull(t){const{points:e,hull:i,triangles:n}=this,s=i.length;let r,h=2*n[i[s-1]];for(let o=0;o<s;++o)r=h,h=2*n[i[o]],t.moveTo(e[r],e[r+1]),t.lineTo(e[h],e[h+1])}renderTriangle(t,e){const{points:i,triangles:n}=this,s=2*n[t*=3],r=2*n[t+1],h=2*n[t+2];e.moveTo(i[s],i[s+1]),e.lineTo(i[r],i[r+1]),e.lineTo(i[h],i[h+1]),e.closePath()}}x.from=function(t,e=function(t){return t[0]},i=function(t){return t[1]},n){return new x("length"in t?function(t,e,i,n){const s=t.length,r=new Float64Array(2*s);for(let h=0;h<s;++h){const s=t[h];r[2*h]=e.call(n,s,h,t),r[2*h+1]=i.call(n,s,h,t)}return r}(t,e,i,n):Float64Array.from(function*(t,e,i,n){let s=0;for(const r of t)yield e.call(n,r,s,t),yield i.call(n,r,s,t),++s}(t,e,i,n)))},t.Delaunay=x,t.Voronoi=f,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.d3=t.d3||{})}(this,function(t){"use strict";function e(t){if(!ArrayBuffer.isView(t))throw new Error("Expected coords to be a typed array.");var e=1/0,a=1/0,c=-1/0,u=-1/0,f=t.length>>1,x=this.ids=new Uint32Array(f);this.coords=t;for(var _=0;_<f;_++){var d=t[2*_],g=t[2*_+1];d<e&&(e=d),g<a&&(a=g),d>c&&(c=d),g>u&&(u=g),x[_]=_}var m,y,v,p=(e+c)/2,w=(a+u)/2,b=1/0;for(_=0;_<f;_++){var T=i(p,w,t[2*_],t[2*_+1]);T<b&&(m=_,b=T)}for(b=1/0,_=0;_<f;_++)_!==m&&(T=i(t[2*m],t[2*m+1],t[2*_],t[2*_+1]))<b&&T>0&&(y=_,b=T);var $=1/0;for(_=0;_<f;_++)if(_!==m&&_!==y){var k=s(t[2*m],t[2*m+1],t[2*y],t[2*y+1],t[2*_],t[2*_+1]);k<$&&(v=_,$=k)}if($===1/0)throw new Error("No Delaunay triangulation exists for this input.");if(n(t[2*m],t[2*m+1],t[2*y],t[2*y+1],t[2*v],t[2*v+1])<0){var M=y;y=v,v=M}var A=t[2*m],E=t[2*m+1],S=t[2*y],z=t[2*y+1],F=t[2*v],L=t[2*v+1],j=function(t,e,i,n,s,r){var h=(i-=t)*i+(n-=e)*n,l=(s-=t)*s+(r-=e)*r,o=i*r-n*s;return{x:t+.5*(r*h-n*l)/o,y:e+.5*(i*l-s*h)/o}}(A,E,S,z,F,L);for(this._cx=j.x,this._cy=j.y,function t(e,i,n,s,r,h){var a,c,u;if(s-n<=20)for(a=n+1;a<=s;a++){for(u=e[a],c=a-1;c>=n&&l(i,e[c],u,r,h)>0;)e[c+1]=e[c--];e[c+1]=u}else{var f=n+s>>1;for(c=s,o(e,f,a=n+1),l(i,e[n],e[s],r,h)>0&&o(e,n,s),l(i,e[a],e[s],r,h)>0&&o(e,a,s),l(i,e[n],e[a],r,h)>0&&o(e,n,a),u=e[a];;){do{a++}while(l(i,e[a],u,r,h)<0);do{c--}while(l(i,e[c],u,r,h)>0);if(c<a)break;o(e,a,c)}e[n+1]=e[c],e[c]=u,s-a+1>=c-n?(t(e,i,a,s,r,h),t(e,i,n,c-1,r,h)):(t(e,i,n,c-1,r,h),t(e,i,a,s,r,h))}}(x,t,0,x.length-1,j.x,j.y),this._hashSize=Math.ceil(Math.sqrt(f)),this._hash=[],_=0;_<this._hashSize;_++)this._hash[_]=null;var P=this.hull=r(t,m);this._hashEdge(P),P.t=0,P=r(t,y,P),this._hashEdge(P),P.t=1,P=r(t,v,P),this._hashEdge(P),P.t=2;var U,I,K=2*f-5,B=this.triangles=new Uint32Array(3*K),D=this.halfedges=new Int32Array(3*K);this.trianglesLen=0,this._addTriangle(m,y,v,-1,-1,-1);for(var H=0;H<x.length;H++)if(d=t[2*(_=x[H])],g=t[2*_+1],!(d===U&&g===I||(U=d,I=g,d===A&&g===E||d===S&&g===z||d===F&&g===L))){var V,Z=this._hashKey(d,g),q=Z;do{V=this._hash[q],q=(q+1)%this._hashSize}while((!V||V.removed)&&q!==Z);for(P=V;n(d,g,P.x,P.y,P.next.x,P.next.y)>=0;)if((P=P.next)===V)throw new Error("Something is wrong with the input points.");var C=P===V,N=this._addTriangle(P.i,_,P.next.i,-1,-1,P.t);P.t=N,(P=r(t,_,P)).t=this._legalize(N+2),P.prev.prev.t===D[N+1]&&(P.prev.prev.t=N+2);for(var O=P.next;n(d,g,O.x,O.y,O.next.x,O.next.y)<0;)N=this._addTriangle(O.i,_,O.next.i,O.prev.t,-1,O.t),O.prev.t=this._legalize(N+2),this.hull=h(O),O=O.next;if(C)for(O=P.prev;n(d,g,O.prev.x,O.prev.y,O.x,O.y)<0;)N=this._addTriangle(O.prev.i,_,O.i,-1,O.t,O.prev.t),this._legalize(N+2),O.prev.t=N,this.hull=h(O),O=O.prev;this._hashEdge(P),this._hashEdge(P.prev)}this.triangles=B.subarray(0,this.trianglesLen),this.halfedges=D.subarray(0,this.trianglesLen)}function i(t,e,i,n){var s=t-i,r=e-n;return s*s+r*r}function n(t,e,i,n,s,r){return(n-e)*(s-i)-(i-t)*(r-n)}function s(t,e,i,n,s,r){var h=(i-=t)*i+(n-=e)*n,l=(s-=t)*s+(r-=e)*r;if(0===h||0===l)return 1/0;var o=i*r-n*s;if(0===o)return 1/0;var a=.5*(r*h-n*l)/o,c=.5*(i*l-s*h)/o;return a*a+c*c}function r(t,e,i){var n={i:e,x:t[2*e],y:t[2*e+1],t:0,prev:null,next:null,removed:!1};return i?(n.next=i.next,n.prev=i,i.next.prev=n,i.next=n):(n.prev=n,n.next=n),n}function h(t){return t.prev.next=t.next,t.next.prev=t.prev,t.removed=!0,t.prev}function l(t,e,n,s,r){return i(t[2*e],t[2*e+1],s,r)-i(t[2*n],t[2*n+1],s,r)||t[2*e]-t[2*n]||t[2*e+1]-t[2*n+1]}function o(t,e,i){var n=t[e];t[e]=t[i],t[i]=n}function a(t){return t[0]}function c(t){return t[1]}e.from=function(t,i,n){i||(i=a),n||(n=c);for(var s=t.length,r=new Float64Array(2*s),h=0;h<s;h++){var l=t[h];r[2*h]=i(l),r[2*h+1]=n(l)}return new e(r)},e.prototype={_hashEdge:function(t){this._hash[this._hashKey(t.x,t.y)]=t},_hashKey:function(t,e){var i=t-this._cx,n=e-this._cy,s=1-i/(Math.abs(i)+Math.abs(n));return Math.floor((2+(n<0?-s:s))/4*this._hashSize)},_legalize:function(t){var e,i,n,s,r,h,l,o,a,c,u=this.triangles,f=this.coords,x=this.halfedges,_=x[t],d=t-t%3,g=_-_%3,m=d+(t+1)%3,y=d+(t+2)%3,v=g+(_+2)%3,p=u[y],w=u[t],b=u[m],T=u[v];if(e=f[2*p],i=f[2*p+1],n=f[2*w],s=f[2*w+1],r=f[2*b],h=f[2*b+1],l=f[2*T],o=f[2*T+1],a=(n-=l)*n+(s-=o)*s,c=(r-=l)*r+(h-=o)*h,(e-=l)*(s*c-a*h)-(i-=o)*(n*c-a*r)+(e*e+i*i)*(n*h-s*r)<0){u[t]=T,u[_]=p,this._link(t,x[v]),this._link(_,x[y]),this._link(y,v);var $=g+(_+1)%3;return this._legalize(t),this._legalize($)}return y},_link:function(t,e){this.halfedges[t]=e,-1!==e&&(this.halfedges[e]=t)},_addTriangle:function(t,e,i,n,s,r){var h=this.trianglesLen;return this.triangles[h]=t,this.triangles[h+1]=e,this.triangles[h+2]=i,this._link(h,n),this._link(h+1,s),this._link(h+2,r),this.trianglesLen+=3,h}};class u{constructor(){this._x0=this._y0=this._x1=this._y1=null,this._=""}moveTo(t,e){this._+=`M${this._x0=this._x1=+t},${this._y0=this._y1=+e}`}closePath(){null!==this._x1&&(this._x1=this._x0,this._y1=this._y0,this._+="Z")}lineTo(t,e){this._+=`L${this._x1=+t},${this._y1=+e}`}arc(t,e,i){const n=(t=+t)+(i=+i),s=e=+e;if(i<0)throw new Error("negative radius");null===this._x1?this._+=`M${n},${s}`:(Math.abs(this._x1-n)>epsilon||Math.abs(this._y1-s)>epsilon)&&(this._+="L"+n+","+s),i&&(this._+=`A${i},${i},0,1,1,${t-i},${e}A${i},${i},0,1,1,${this._x1=n},${this._y1=s}`)}rect(t,e,i,n){this._+=`M${this._x0=this._x1=+t},${this._y0=this._y1=+e}h${+i}v${+n}h${-i}Z`}value(){return this._||null}}class f{constructor(t,[e,i,n,s]=[0,0,960,500]){if(!((n=+n)>=(e=+e)&&(s=+s)>=(i=+i)))throw new Error("invalid bounds");const{points:r,halfedges:h,hull:l,triangles:o}=this.delaunay=t,a=this.circumcenters=new Float64Array(o.length/3*2),c=this.edges=new Uint32Array(h.length),u=this.index=new Uint32Array(r.length),f=this.vectors=new Float64Array(2*r.length);this.xmax=n,this.xmin=e,this.ymax=s,this.ymin=i;for(let t=0,e=0,i=h.length;t<i;++t){const i=o[t];if(u[2*i]!==u[2*i+1])continue;const n=u[2*i]=e;let s=t;do{if(c[e++]=Math.floor(s/3),-1===(s=h[s]))break;if(o[s=s%3==2?s-2:s+1]!==i)break}while(s!==t);if(s!==t){const r=e;for(s=t;-1!==(s=h[s%3==0?s+2:s-1])&&o[s]===i;)c[e++]=Math.floor(s/3);r<e&&(c.subarray(n,r).reverse(),c.subarray(n,e).reverse())}u[2*i+1]=e}for(let t=0,e=0,i=o.length;t<i;t+=3,e+=2){const i=2*o[t],n=2*o[t+1],s=2*o[t+2],h=r[i],l=r[i+1],c=r[n],u=r[n+1],f=r[s],x=r[s+1],_=h-c,d=h-f,g=l-u,m=l-x,y=h*h+l*l,v=y-c*c-u*u,p=y-f*f-x*x,w=2*(d*g-_*m);a[e]=(g*p-m*v)/w,a[e+1]=(d*v-_*p)/w}for(let t,e,i,n=l.length,s=2*o[l[n-1]],h=r[s],a=r[s+1],c=0;c<n;++c)t=s,e=h,i=a,h=r[s=2*o[l[c]]],a=r[s+1],f[2*t+2]=f[2*s]=i-a,f[2*t+3]=f[2*s+1]=h-e}render(t){const e=null==t?t=new u:void 0,{delaunay:{halfedges:i,hull:n,triangles:s},circumcenters:r,vectors:h}=this;for(let e=0,n=i.length;e<n;++e){const n=i[e];if(n<e)continue;const s=2*Math.floor(e/3),h=2*Math.floor(n/3),l=r[s],o=r[s+1],a=r[h],c=r[h+1];this._renderSegment(l,o,a,c,t)}for(let e=0,i=n.length;e<i;++e){const i=2*Math.floor(n[e]/3),l=r[i],o=r[i+1],a=4*s[n[e]],c=this._project(l,o,h[a+2],h[a+3]);c&&this._renderSegment(l,o,c[0],c[1],t)}return e&&e.value()}renderBounds(t){const e=null==t?t=new u:void 0;return t.rect(this.xmin,this.ymin,this.xmax-this.xmin,this.ymax-this.ymin),e&&e.value()}renderCell(t,e){const i=null==e?e=new u:void 0,n=this._clip(t);if(null!==n){e.moveTo(n[0],n[1]);for(let t=2,i=n.length;t<i;t+=2)e.lineTo(n[t],n[t+1]);return e.closePath(),i&&i.value()}}_renderSegment(t,e,i,n,s){let r;const h=this._regioncode(t,e),l=this._regioncode(i,n);0===h&&0===l?(s.moveTo(t,e),s.lineTo(i,n)):(r=this._clipSegment(t,e,i,n,h,l))&&(s.moveTo(r[0],r[1]),s.lineTo(r[2],r[3]))}contains(t,e,i){return(e=+e)==e&&(i=+i)==i&&this._step(t,e,i)===t}find(t,e,i=0){if((t=+t)!=t||(e=+e)!=e)return-1;let n;for(;(n=this._step(i,t,e))>=0&&n!==i;)i=n;return n}_step(t,e,i){const{delaunay:{points:n,triangles:s},edges:r,index:h}=this;if(0===n.length)return-1;const l=h[2*t],o=h[2*t+1];if(l===o)return-1;let a=t,c=3*r[l],u=(e-n[2*a])**2+(i-n[2*a+1])**2;switch(t){case s[c]:c=s[c+2];break;case s[c+1]:c=s[c];break;case s[c+2]:c=s[c+1]}let f=(e-n[2*c])**2+(i-n[2*c+1])**2;f<u&&(u=f,a=c);for(let h=l;h<o;++h){switch(c=3*r[h],t){case s[c]:c=s[c+1];break;case s[c+1]:c=s[c+2];break;case s[c+2]:c=s[c]}(f=(e-n[2*c])**2+(i-n[2*c+1])**2)<u&&(u=f,a=c)}return a}_cell(t){const{index:e,edges:i,circumcenters:n}=this,s=e[2*t],r=e[2*t+1];if(s===r)return null;const h=new Float64Array(2*(r-s));for(let t=s,e=0;t<r;++t,e+=2){const s=2*i[t];h[e]=n[s],h[e+1]=n[s+1]}return h}_clip(t){const e=this._cell(t);if(null===e)return null;const{vectors:i}=this,n=4*t;return i[n]||i[n+1]?this._clipInfinite(t,e,i[n],i[n+1],i[n+2],i[n+3]):this._clipFinite(t,e)}_clipFinite(t,e){const i=e.length;let n,s,r,h,l,o=null,a=e[i-2],c=e[i-1],u=this._regioncode(a,c);for(let t=0;t<i;t+=2)if(n=a,s=c,a=e[t],c=e[t+1],r=u,u=this._regioncode(a,c),0===r&&0===u)h=l,l=0,o?o.push(a,c):o=[a,c];else{let e,i,f,x,_;if(0===r){if(null===(e=this._clipSegment(n,s,a,c,r,u)))continue;[i,f,x,_]=e}else{if(null===(e=this._clipSegment(a,c,n,s,u,r)))continue;[x,_,i,f]=e,h=l,l=this._edgecode(i,f),h&&l&&this._edge(t,h,l,o,o.length),o?o.push(i,f):o=[i,f]}h=l,l=this._edgecode(x,_),h&&l&&this._edge(t,h,l,o,o.length),o?o.push(x,_):o=[x,_]}if(o)h=l,l=this._edgecode(o[0],o[1]),h&&l&&this._edge(t,h,l,o,o.length);else if(this.contains(t,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2))return[this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax,this.xmin,this.ymin];return o}_clipSegment(t,e,i,n,s,r){for(;;){if(0===s&&0===r)return[t,e,i,n];if(s&r)return null;let h,l,o=s||r;8&o?(h=t+(i-t)*(this.ymax-e)/(n-e),l=this.ymax):4&o?(h=t+(i-t)*(this.ymin-e)/(n-e),l=this.ymin):2&o?(l=e+(n-e)*(this.xmax-t)/(i-t),h=this.xmax):(l=e+(n-e)*(this.xmin-t)/(i-t),h=this.xmin),s?(t=h,e=l,s=this._regioncode(t,e)):(i=h,n=l,r=this._regioncode(i,n))}}_clipInfinite(t,e,i,n,s,r){let h,l=Array.from(e);if((h=this._project(l[0],l[1],i,n))&&l.unshift(h[0],h[1]),(h=this._project(l[l.length-2],l[l.length-1],s,r))&&l.push(h[0],h[1]),l=this._clipFinite(t,l))for(let e,i=0,n=l.length,s=this._edgecode(l[n-2],l[n-1]);i<n;i+=2)e=s,s=this._edgecode(l[i],l[i+1]),e&&s&&(i=this._edge(t,e,s,l,i),n=l.length);else this.contains(t,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2)&&(l=[this.xmin,this.ymin,this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax]);return l}_edge(t,e,i,n,s){for(;e!==i;){let i,r;switch(e){case 5:e=4;continue;case 4:e=6,i=this.xmax,r=this.ymin;break;case 6:e=2;continue;case 2:e=10,i=this.xmax,r=this.ymax;break;case 10:e=8;continue;case 8:e=9,i=this.xmin,r=this.ymax;break;case 9:e=1;continue;case 1:e=5,i=this.xmin,r=this.ymin}this.contains(t,i,r)&&(n.splice(s,0,i,r),s+=2)}return s}_project(t,e,i,n){let s,r,h,l=1/0;if(n<0){if(e<=this.ymin)return null;(s=(this.ymin-e)/n)<l&&(h=this.ymin,r=t+(l=s)*i)}else if(n>0){if(e>=this.ymax)return null;(s=(this.ymax-e)/n)<l&&(h=this.ymax,r=t+(l=s)*i)}if(i>0){if(t>=this.xmax)return null;(s=(this.xmax-t)/i)<l&&(r=this.xmax,h=e+(l=s)*n)}else if(i<0){if(t<=this.xmin)return null;(s=(this.xmin-t)/i)<l&&(r=this.xmin,h=e+(l=s)*n)}return[r,h]}_edgecode(t,e){return(t===this.xmin?1:t===this.xmax?2:0)|(e===this.ymin?4:e===this.ymax?8:0)}_regioncode(t,e){return(t<this.xmin?1:t>this.xmax?2:0)|(e<this.ymin?4:e>this.ymax?8:0)}}const x=2*Math.PI;class _{constructor(t){const{halfedges:i,hull:n,triangles:s}=new e(t);this.points=t,this.halfedges=i,this.hull=Uint32Array.from(function*(t){let e=t;do{yield e.t}while((e=e.next)!==t)}(n)),this.triangles=s}voronoi(t){return new f(this,t)}render(t){const e=null==t?t=new u:void 0,{points:i,halfedges:n,triangles:s}=this;for(let e=0,r=n.length;e<r;++e){const r=n[e];if(r<e)continue;const h=2*s[e],l=2*s[r];t.moveTo(i[h],i[h+1]),t.lineTo(i[l],i[l+1])}return this.renderHull(t),e&&e.value()}renderPoints(t,e=2){const i=null==t?t=new u:void 0,{points:n}=this;for(let i=0,s=n.length;i<s;i+=2){const s=n[i],r=n[i+1];t.moveTo(s+e,r),t.arc(s,r,e,0,x)}return i&&i.value()}renderHull(t){const e=null==t?t=new u:void 0,{points:i,hull:n,triangles:s}=this,r=n.length;let h,l=2*s[n[r-1]];for(let e=0;e<r;++e)h=l,l=2*s[n[e]],t.moveTo(i[h],i[h+1]),t.lineTo(i[l],i[l+1]);return e&&e.value()}renderTriangle(t,e){const i=null==e?e=new u:void 0,{points:n,triangles:s}=this,r=2*s[t*=3],h=2*s[t+1],l=2*s[t+2];return e.moveTo(n[r],n[r+1]),e.lineTo(n[h],n[h+1]),e.lineTo(n[l],n[l+1]),e.closePath(),i&&i.value()}}_.from=function(t,e=function(t){return t[0]},i=function(t){return t[1]},n){return new _("length"in t?function(t,e,i,n){const s=t.length,r=new Float64Array(2*s);for(let h=0;h<s;++h){const s=t[h];r[2*h]=e.call(n,s,h,t),r[2*h+1]=i.call(n,s,h,t)}return r}(t,e,i,n):Float64Array.from(function*(t,e,i,n){let s=0;for(const r of t)yield e.call(n,r,s,t),yield i.call(n,r,s,t),++s}(t,e,i,n)))},t.Delaunay=_,t.Voronoi=f,Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-delaunay", | ||
"version": "3.0.0-alpha.3", | ||
"publishConfig": { | ||
"tag": "next" | ||
}, | ||
"version": "3.0.0", | ||
"description": "Compute the Voronoi diagram of a set of two-dimensional points.", | ||
@@ -8,0 +5,0 @@ "keywords": [ |
@@ -98,23 +98,23 @@ # d3-delaunay | ||
<a href="#delaunay_render" name="delaunay_render">#</a> <i>delaunay</i>.<b>render</b>(<i>context</i>) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
<a href="#delaunay_render" name="delaunay_render">#</a> <i>delaunay</i>.<b>render</b>([<i>context</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
<img alt="delaunay.render" src="https://raw.githubusercontent.com/d3/d3-delaunay/master/img/delaunay-mesh.png"> | ||
Renders the edges of the Delaunay triangulation to the specified *context*. The specified *context* must implement the *context*.moveTo and *context*.lineTo methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). | ||
Renders the edges of the Delaunay triangulation to the specified *context*. The specified *context* must implement the *context*.moveTo and *context*.lineTo methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). If a *context* is not specified, an SVG path string is returned instead. | ||
<a href="#delaunay_renderHull" name="delaunay_renderHull">#</a> <i>delaunay</i>.<b>renderHull</b>(<i>context</i>) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
<a href="#delaunay_renderHull" name="delaunay_renderHull">#</a> <i>delaunay</i>.<b>renderHull</b>([<i>context</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
<img alt="delaunay.renderHull" src="https://raw.githubusercontent.com/d3/d3-delaunay/master/img/delaunay-hull.png"> | ||
Renders the convex hull of the Delaunay triangulation to the specified *context*. The specified *context* must implement the *context*.moveTo and *context*.lineTo methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). | ||
Renders the convex hull of the Delaunay triangulation to the specified *context*. The specified *context* must implement the *context*.moveTo and *context*.lineTo methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). If a *context* is not specified, an SVG path string is returned instead. | ||
<a href="#delaunay_renderTriangle" name="delaunay_renderTriangle">#</a> <i>delaunay</i>.<b>renderTriangle</b>(<i>context</i>) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
<a href="#delaunay_renderTriangle" name="delaunay_renderTriangle">#</a> <i>delaunay</i>.<b>renderTriangle</b>(<i>i</i>[, <i>context</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
<img alt="delaunay.renderTriangle" src="https://raw.githubusercontent.com/d3/d3-delaunay/master/img/delaunay-triangle.png"> | ||
Renders triangle *i* of the Delaunay triangulation to the specified *context*. The specified *context* must implement the *context*.moveTo, *context*.lineTo and *context*.closePath methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). | ||
Renders triangle *i* of the Delaunay triangulation to the specified *context*. The specified *context* must implement the *context*.moveTo, *context*.lineTo and *context*.closePath methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). If a *context* is not specified, an SVG path string is returned instead. | ||
<a href="#delaunay_renderPoints" name="delaunay_renderPoints">#</a> <i>delaunay</i>.<b>renderPoints</b>(<i>context</i>[, <i>radius</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
<a href="#delaunay_renderPoints" name="delaunay_renderPoints">#</a> <i>delaunay</i>.<b>renderPoints</b>(\[<i>context</i>\]\[, <i>radius</i>\]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
Renders the input points of the Delaunay triangulation to the specified *context* as circles with the specified *radius*. If *radius* is not specified, it defaults to 2. The specified *context* must implement the *context*.moveTo and *context*.arc methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). | ||
Renders the input points of the Delaunay triangulation to the specified *context* as circles with the specified *radius*. If *radius* is not specified, it defaults to 2. The specified *context* must implement the *context*.moveTo and *context*.arc methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). If a *context* is not specified, an SVG path string is returned instead. | ||
@@ -137,13 +137,12 @@ <a href="#delaunay_voronoi" name="delaunay_voronoi">#</a> <i>delaunay</i>.<b>voronoi</b>([<i>bounds</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/delaunay.js "Source") | ||
… TODO The triangle indexes [*i0*, *i1*, …] in counterclockwise order. Together with the start and end vectors [*cell*.v0](#cell_v0) and [*cell*.vn](#cell_vn) if any, the [circumcenters](#voronoi_circumcenters) of these triangles form the exterior polygon of the cell. For coincident points, only the cell associated with the first input point is non-null. | ||
… | ||
<a href="#voronoi_index" name="voronoi_index">#</a> <i>voronoi</i>.<b>index</b> | ||
… TODO The triangle indexes [*i0*, *i1*, …] in counterclockwise order. Together with the start and end vectors [*cell*.v0](#cell_v0) and [*cell*.vn](#cell_vn) if any, the [circumcenters](#voronoi_circumcenters) of these triangles form the exterior polygon of the cell. For coincident points, only the cell associated with the first input point is non-null. | ||
… | ||
<a href="#voronoi_vectors" name="voronoi_vectors">#</a> <i>voronoi</i>.<b>vectors</b> | ||
… TODO The start vector [*vx0*, *vy0*], if the cell’s associated point is on the [convex hull](#delaunay_hull) of the Delaunay triangulation. Together with the cell’s [triangle circumcenters](#cell_triangles) and end vector [*cell*.vn](#cell_vn) if any, the start vector forms the exterior polygon of the cell. The end vector [*vxn*, *vyn*], if the cell’s associated point is on the [convex hull](#delaunay_hull) of the Delaunay triangulation. Together with the cell’s [triangle circumcenters](#cell_triangles) and start vector [*cell*.v0](#cell_v0) if any, the end vector forms the exterior polygon of the cell. | ||
… | ||
<a href="#voronoi_xmin" name="voronoi_xmin">#</a> <i>voronoi</i>.<b>xmin</b><br> | ||
@@ -164,18 +163,18 @@ <a href="#voronoi_ymin" name="voronoi_ymin">#</a> <i>voronoi</i>.<b>ymin</b><br> | ||
<a href="#voronoi_render" name="voronoi_render">#</a> <i>voronoi</i>.<b>render</b>(<i>context</i>) [<>](https://github.com/d3/d3-delaunay/blob/master/src/voronoi.js "Source") | ||
<a href="#voronoi_render" name="voronoi_render">#</a> <i>voronoi</i>.<b>render</b>([<i>context</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/voronoi.js "Source") | ||
<img alt="voronoi.render" src="https://raw.githubusercontent.com/d3/d3-delaunay/master/img/voronoi-mesh.png"> | ||
Renders the mesh of Voronoi cells to the specified *context*. The specified *context* must implement the *context*.moveTo and *context*.lineTo methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). | ||
Renders the mesh of Voronoi cells to the specified *context*. The specified *context* must implement the *context*.moveTo and *context*.lineTo methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). If a *context* is not specified, an SVG path string is returned instead. | ||
<a href="#voronoi_renderBounds" name="voronoi_renderBounds">#</a> <i>voronoi</i>.<b>renderBounds</b>(<i>context</i>) [<>](https://github.com/d3/d3-delaunay/blob/master/src/voronoi.js "Source") | ||
<a href="#voronoi_renderBounds" name="voronoi_renderBounds">#</a> <i>voronoi</i>.<b>renderBounds</b>([<i>context</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/voronoi.js "Source") | ||
<img alt="voronoi.renderBounds" src="https://raw.githubusercontent.com/d3/d3-delaunay/master/img/voronoi-bounds.png"> | ||
Renders the viewport extent to the specified *context*. The specified *context* must implement the *context*.rect method from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). Equivalent to *context*.rect(*voronoi*.xmin, *voronoi*.ymin, *voronoi*.xmax - *voronoi*.xmin, *voronoi*.ymax - *voronoi*.ymin). | ||
Renders the viewport extent to the specified *context*. The specified *context* must implement the *context*.rect method from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). Equivalent to *context*.rect(*voronoi*.xmin, *voronoi*.ymin, *voronoi*.xmax - *voronoi*.xmin, *voronoi*.ymax - *voronoi*.ymin). If a *context* is not specified, an SVG path string is returned instead. | ||
<a href="#voronoi_renderCell" name="voronoi_renderCell">#</a> <i>voronoi</i>.<b>renderCell</b>(<i>i</i>, <i>context</i>) [<>](https://github.com/d3/d3-delaunay/blob/master/src/voronoi.js "Source") | ||
<a href="#voronoi_renderCell" name="voronoi_renderCell">#</a> <i>voronoi</i>.<b>renderCell</b>(<i>i</i>[, <i>context</i>]) [<>](https://github.com/d3/d3-delaunay/blob/master/src/voronoi.js "Source") | ||
<img alt="cell.render" src="https://raw.githubusercontent.com/d3/d3-delaunay/master/img/spectral.png"> | ||
Renders the cell with the specified index *i* to the specified *context*. The specified *context* must implement the *context*.moveTo , *context*.lineTo and *context*.closePath methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). | ||
Renders the cell with the specified index *i* to the specified *context*. The specified *context* must implement the *context*.moveTo , *context*.lineTo and *context*.closePath methods from the [CanvasPathMethods API](https://www.w3.org/TR/2dcontext/#canvaspathmethods). If a *context* is not specified, an SVG path string is returned instead. |
import Delaunator from "delaunator"; | ||
import Path from "./path"; | ||
import Voronoi from "./voronoi.js"; | ||
@@ -26,2 +27,3 @@ | ||
render(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points, halfedges, triangles} = this; | ||
@@ -37,4 +39,6 @@ for (let i = 0, n = halfedges.length; i < n; ++i) { | ||
this.renderHull(context); | ||
return buffer && buffer.value(); | ||
} | ||
renderPoints(context, r = 2) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points} = this; | ||
@@ -46,4 +50,6 @@ for (let i = 0, n = points.length; i < n; i += 2) { | ||
} | ||
return buffer && buffer.value(); | ||
} | ||
renderHull(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points, hull, triangles} = this; | ||
@@ -57,4 +63,6 @@ const n = hull.length; | ||
} | ||
return buffer && buffer.value(); | ||
} | ||
renderTriangle(i, context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {points, triangles} = this; | ||
@@ -68,2 +76,3 @@ const t0 = triangles[i *= 3] * 2; | ||
context.closePath(); | ||
return buffer && buffer.value(); | ||
} | ||
@@ -70,0 +79,0 @@ } |
@@ -0,1 +1,3 @@ | ||
import Path from "./path"; | ||
export default class Voronoi { | ||
@@ -75,2 +77,3 @@ constructor(delaunay, [xmin, ymin, xmax, ymax] = [0, 0, 960, 500]) { | ||
render(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const {delaunay: {halfedges, hull, triangles}, circumcenters, vectors} = this; | ||
@@ -96,7 +99,11 @@ for (let i = 0, n = halfedges.length; i < n; ++i) { | ||
} | ||
return buffer && buffer.value(); | ||
} | ||
renderBounds(context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
context.rect(this.xmin, this.ymin, this.xmax - this.xmin, this.ymax - this.ymin); | ||
return buffer && buffer.value(); | ||
} | ||
renderCell(i, context) { | ||
const buffer = context == null ? context = new Path : undefined; | ||
const points = this._clip(i); | ||
@@ -109,2 +116,3 @@ if (points === null) return; | ||
context.closePath(); | ||
return buffer && buffer.value(); | ||
} | ||
@@ -111,0 +119,0 @@ _renderSegment(x0, y0, x1, y1, context) { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
75605
10
1304
0
178