kdtree.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <!Doctype html>
  2. <html>
  3. <head>
  4. <script src="http://s1.bdstatic.com/r/www/cache/ecom/esl/1-8-8/esl.js"></script>
  5. <script src="http://ubilabs.github.io/kd-tree-javascript/kdTree.js"></script>
  6. <meta charset="utf-8">
  7. </head>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0px;
  12. }
  13. </style>
  14. <body>
  15. <canvas id="main"></canvas>
  16. </body>
  17. <script>
  18. require.config({
  19. packages: [{
  20. name: 'echarts',
  21. location: '../src',
  22. main: 'echarts'
  23. }, {
  24. name: 'zrender',
  25. location: '../../zrender/src',
  26. main: 'zrender'
  27. }]
  28. });
  29. require(['echarts/data/KDTree'], function (KDTree) {
  30. var points = [];
  31. var points2 = [];
  32. var width = window.innerWidth;
  33. var height = window.innerHeight;
  34. var canvas = document.getElementById('main');
  35. canvas.width = width;
  36. canvas.height = height;
  37. var ctx = canvas.getContext('2d');
  38. for (var i = 0; i < 50000; i++) {
  39. var point = [Math.round(width * Math.random()), Math.round(height * Math.random())];
  40. points.push({
  41. array: point
  42. });
  43. points2.push(point);
  44. }
  45. var time = performance.now();
  46. var kdTree1 = new KDTree(points);
  47. console.log(performance.now() - time);
  48. var squaredDistance = function (a, b) {
  49. a = a.array;
  50. b = b.array;
  51. return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
  52. }
  53. var squaredDistance2 = function (a, b) {
  54. return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
  55. }
  56. var kdTree2 = new kdTree(points2, squaredDistance2, [0, 1]);
  57. // Find nearest
  58. var p2 = [Math.round(width * Math.random()), Math.round(height * Math.random())];
  59. p = {
  60. array: p2
  61. };
  62. var time = performance.now();
  63. for (var i = 0; i < 50000; i++) {
  64. var nearest = kdTree1.nearest(p, squaredDistance);
  65. }
  66. console.log(performance.now() - time);
  67. console.log(p, nearest);
  68. // Find nearst N
  69. var nearestN = [];
  70. var time = performance.now();
  71. for (var i = 0; i < 50000; i++) {
  72. var nearest = kdTree1.nearestN(p, 5, squaredDistance, nearestN);
  73. }
  74. console.log(performance.now() - time);
  75. console.log(p.array, nearestN.map(function(a) {
  76. return a.array;
  77. }));
  78. setTimeout(function () {
  79. var time = performance.now();
  80. for (var i = 0; i < 50000; i++) {
  81. var nearest = kdTree2.nearest(p2, 5);
  82. }
  83. console.log(performance.now() - time);
  84. console.log(p2, nearest.map(function(a) {
  85. return a[0];
  86. }));
  87. }, 1000);
  88. // var buildSplitLines = function (node, minx, miny, maxx, maxy) {
  89. // switch (node.axis) {
  90. // case 0:
  91. // var x = node.data[0];
  92. // ctx.moveTo(x + 0.5, miny);
  93. // ctx.lineTo(x + 0.5, maxy);
  94. // ctx.stroke();
  95. // node.left && buildSplitLines(node.left, minx, miny, node.data[0], maxy);
  96. // node.right && buildSplitLines(node.right, node.data[0], miny, maxx, maxy);
  97. // break;
  98. // case 1:
  99. // var y = node.data[1];
  100. // ctx.moveTo(minx, y + 0.5);
  101. // ctx.lineTo(maxx, y + 0.5);
  102. // ctx.stroke();
  103. // node.left && buildSplitLines(node.left, minx, miny, maxx, node.data[1]);
  104. // node.right && buildSplitLines(node.right, minx, node.data[1], maxx, maxy);
  105. // break;
  106. // }
  107. // }
  108. // ctx.fillStyle = 'black';
  109. // buildSplitLines(kdTree1.root, 0, 0, width, height);
  110. // ctx.fillStyle = 'red';
  111. // for (var i = 0; i < points.length; i++) {
  112. // var p = points[i];
  113. // ctx.fillRect(p[0], p[1], 4, 4);
  114. // }
  115. })
  116. </script>
  117. </html>