Debian Codegraph @

closure-library/closure/goog/demos/quadtree.html

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