UNPKG

d3-hilbert

Version:

D3 layout to visualize distance variables using a continuous Hilbert space-filling space.

3 lines (2 loc) 1.74 kB
// Version 1.4.1 d3-hilbert - https://github.com/vasturiano/d3-hilbert !function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).d3=t.d3||{})}(this,(function(t){"use strict";t.hilbert=function(){var t=function(){function t(t,n,e,r){0==r&&(1==e&&(n[0]=t-1-n[0],n[1]=t-1-n[1]),n.push(n.shift()))}return{point2Distance:function(n,e,r){for(var i,o,u=0,a=[n,e],f=r/2;f>=1;f/=2)u+=f*f*(3*(i=(a[0]&f)>0)^(o=(a[1]&f)>0)),t(f,a,i,o);return u},distance2Point:function(n,e){for(var r,i,o=n,u=[0,0],a=1;a<e;a*=2)t(a,u,r=1&o/2,i=1&(o^r)),u[0]+=a*r,u[1]+=a*i,o/=4;return u}}}(),n={},e=1,r=4,i=!0;return n.canvasWidth=function(t){return arguments.length?(e=+t,n):e},n.order=function(t){return arguments.length?(r=+t,n):r},n.simplifyCurves=function(t){return arguments.length?(i=t,n):i},n.layout=function(o){var u=function(n,e,r,i,o){if(o){for(;!Number.isInteger(n)||!Number.isInteger(e);)n*=4,e*=4,r+=1;for(;!(n%4)&&!(e%4)&&r>0;)n/=4,e/=4,r-=1}var u=Math.pow(4,r);n=Math.min(n,u),e=Math.min(e,u-n);for(var a,f=Math.pow(2,r),s=i/f,c=t.distance2Point(n,f),l=[],h=c,d=1;d<e;d++)a=t.distance2Point(n+d,f),l.push(a[0]>h[0]?"R":a[0]<h[0]?"L":a[1]>h[1]?"D":"U"),h=a;return{cellWidth:s,startCell:c,pathVertices:l}}(o.start,o.length,r,e,i);return o.cellWidth=u.cellWidth,o.startCell=u.startCell,o.pathVertices=u.pathVertices,n},n.getValAtXY=function(n,i){var o=Math.pow(2,r),u=[n,i].map((function(t){return Math.floor(t*o/e)}));return t.point2Distance(u[0],u[1],o)},n.getXyAtVal=function(n){return n>Math.pow(4,r)||n<0?(console.error("Value is outside hilbert space boundaries."),null):t.distance2Point(n,Math.pow(2,r))},n}}));