hilbert2D.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Hilbert Curve: Generates 2D-Coordinates in a very fast way.
  3. *
  4. * @author Dylan Grafmyre
  5. *
  6. * Based on work by:
  7. * @author Thomas Diewald
  8. * @link http://www.openprocessing.org/sketch/15493
  9. *
  10. * @param center Center of Hilbert curve.
  11. * @param size Total width of Hilbert curve.
  12. * @param iterations Number of subdivisions.
  13. * @param v0 Corner index -X, -Z.
  14. * @param v1 Corner index -X, +Z.
  15. * @param v2 Corner index +X, +Z.
  16. * @param v3 Corner index +X, -Z.
  17. */
  18. function hilbert2D ( center, size, iterations, v0, v1, v2, v3 ) {
  19. // Default Vars
  20. var center = undefined !== center ? center : new THREE.Vector3( 0, 0, 0 ),
  21. size = undefined !== size ? size : 10,
  22. half = size / 2,
  23. iterations = undefined !== iterations ? iterations : 1,
  24. v0 = undefined !== v0 ? v0 : 0,
  25. v1 = undefined !== v1 ? v1 : 1,
  26. v2 = undefined !== v2 ? v2 : 2,
  27. v3 = undefined !== v3 ? v3 : 3
  28. ;
  29. var vec_s = [
  30. new THREE.Vector3( center.x - half, center.y, center.z - half ),
  31. new THREE.Vector3( center.x - half, center.y, center.z + half ),
  32. new THREE.Vector3( center.x + half, center.y, center.z + half ),
  33. new THREE.Vector3( center.x + half, center.y, center.z - half )
  34. ];
  35. var vec = [
  36. vec_s[ v0 ],
  37. vec_s[ v1 ],
  38. vec_s[ v2 ],
  39. vec_s[ v3 ]
  40. ];
  41. // Recurse iterations
  42. if ( 0 <= -- iterations ) {
  43. var tmp = [];
  44. Array.prototype.push.apply( tmp, hilbert2D ( vec[ 0 ], half, iterations, v0, v3, v2, v1 ) );
  45. Array.prototype.push.apply( tmp, hilbert2D ( vec[ 1 ], half, iterations, v0, v1, v2, v3 ) );
  46. Array.prototype.push.apply( tmp, hilbert2D ( vec[ 2 ], half, iterations, v0, v1, v2, v3 ) );
  47. Array.prototype.push.apply( tmp, hilbert2D ( vec[ 3 ], half, iterations, v2, v1, v0, v3 ) );
  48. // Return recursive call
  49. return tmp;
  50. }
  51. // Return complete Hilbert Curve.
  52. return vec;
  53. }