quadtree.k 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. / z-order
  2. ds:(0 0;0 1;1 0;1 1)
  3. / parent vector from color vector
  4. prt:{0,p@(!#p)@&(#p:&~x)#4}
  5. / paths to root from parent vector
  6. pth:{(x@)\'!#x}
  7. / Z-order curve
  8. zo:{,/r+/:2*r:4/2\!x}
  9. / render
  10. drw:{ d:#'ps:pth[prt[c:0,,/x]] / depth, paths to root
  11. dm:2#*s:*/'(-/1|/\d)#\:2 / size of each pixel, dimensions of graph
  12. pts:{[c;s;ps;i]|(c@i;+/'*/(s*s:s@p;4!-1+p:-1_'ps@i))}[c;s;ps;&1&c] / upper left pixels in final graph
  13. `0:(,""),".#"dm#(2 0 1@{y+(~y)*x}\@[&*/dm;;:;].pts)@zo@(-1+|/d)(2*)/1 / fill out the rest of the box
  14. }
  15. / quad tree from (2^N)x(2^N) grid. qt[grd;zo@N]
  16. qt0:{(p;s):z;|((ps@&~w),\:-2!s;@[w;&w;:;1+x@ps@&w:(f-1)=-/y@1 -1_'0 1+\:ps:p+(f:s*s)*!5])}
  17. qt:{0,*(#*|:){(r;q):z;0 1_'z,'qt0[x;y]@**|z}[grd;+\=':0,grd:(,/x)@<zo[y]]/(();,0,-2!y)}
  18. ex0:{a:x[1;z]
  19. $[#n:,/x[0]?(n0:&'~a)+\:'1+4*z
  20. o[x;y,'(1+,/(4*(!#a)+#*|y)+'n0;a);n]
  21. y,'(!0;a)]}
  22. / extract subtree from x at index y
  23. ex:{ex0[(0,1+&~,/x;x);(0N;!0);,y]}
  24. / ammend z to x at index y
  25. / Here z and x are (p;c) parent color pairs
  26. am:{x,'(y^+/1(1*(#*|x)*4*~^:)\*z;*|z)}
  27. / reorder color vector based on parent vector
  28. ro:{y@<x}