quadtree.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2008 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>QuadTree Demo</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.structs');
  13. goog.require('goog.events');
  14. goog.require('goog.structs.QuadTree');
  15. </script>
  16. <style>
  17. .region {
  18. position: absolute;
  19. -moz-outline: 1px solid #CCC;
  20. outline: 1px solid #CCC;
  21. z-index: 500;
  22. }
  23. .point {
  24. position: absolute;
  25. background-color: red;
  26. width: 4px;
  27. height: 4px;
  28. z-index: 1000;
  29. }
  30. #el {
  31. width: 500px;
  32. height: 500px;
  33. background-color: #FCFCFC;
  34. }
  35. #values {
  36. font-size: small;
  37. }
  38. #info {
  39. position: absolute;
  40. top: 5px;
  41. left: 505px;
  42. width: 200px;
  43. font: normal 11px verdana;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div id="el"></div>
  49. <div id="info">
  50. <p>Click on the area to the left to add a point to the quadtree, clicking on
  51. a point will remove it from the tree.</p>
  52. <pre id="values"></pre>
  53. </div>
  54. <script>
  55. function visualize(node) {
  56. var div = document.createElement('div');
  57. div.className = 'region';
  58. div.style.top = node.y + 'px';
  59. div.style.left = node.x + 'px';
  60. div.style.width = node.w+ 'px';
  61. div.style.height = node.h + 'px';
  62. if (node.nodeType == goog.structs.QuadTree.NodeType.POINTER) {
  63. visualize(node.nw, div);
  64. visualize(node.ne, div);
  65. visualize(node.sw, div);
  66. visualize(node.se, div);
  67. } else if (node.nodeType == goog.structs.QuadTree.NodeType.LEAF) {
  68. var point = document.createElement('div');
  69. point.className = 'point';
  70. point.style.top = (node.point.y - 2) + 'px';
  71. point.style.left = (node.point.x - 2) + 'px'
  72. point.text = node.point.value;
  73. point.point = node.point;
  74. document.getElementById('el').appendChild(point);
  75. }
  76. document.getElementById('el').appendChild(div);
  77. var values = ['Values:'];
  78. qt.forEach(function(value, coord) {
  79. values.push(coord + ' ' + value);
  80. });
  81. document.getElementById('values').innerHTML = values.join('\n');
  82. }
  83. var maxW = 500, maxH = 500;
  84. var qt = new goog.structs.QuadTree(0, 0, maxW, maxH);
  85. visualize(qt.getRootNode());
  86. goog.events.listen(document.body, 'click', function(e) {
  87. if (e.target.className == 'point') {
  88. qt.remove(e.target.point.x, e.target.point.y);
  89. } else {
  90. var x = e.clientX;
  91. var y = e.clientY;
  92. if (x < maxW && y < maxH) qt.set(x, y, new Date().toLocaleString());
  93. }
  94. var el = document.getElementById('el');
  95. el.innerHTML = '';
  96. visualize(qt.getRootNode());
  97. });
  98. </script>
  99. </body>
  100. </html>