Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
The Google Closure Library is a collection of JavaScript code
designed for use with the Google Closure JavaScript Compiler.
This non-official distribution was prepared by the ClojureScript
team at http://clojure.org/
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Datastructure: A point Quad Tree for representing 2D data. Each
* region has the same ratio as the bounds for the tree.
*
* The implementation currently requires pre-determined bounds for data as it
* can not rebalance itself to that degree.
*
* @see ../demos/quadtree.html
*/
goog.provide('goog.structs.QuadTree');
goog.provide('goog.structs.QuadTree.Node');
goog.provide('goog.structs.QuadTree.Point');
goog.require('goog.math.Coordinate');
/**
* Constructs a new quad tree.
* @param {number} minX Minimum x-value that can be held in tree.
* @param {number} minY Minimum y-value that can be held in tree.
* @param {number} maxX Maximum x-value that can be held in tree.
* @param {number} maxY Maximum y-value that can be held in tree.
* @constructor
* @final
*/
goog.structs.QuadTree = function(minX, minY, maxX, maxY) {
/**
* Count of the number of items in the tree.
* @private {number}
*/
this.count_ = 0;
/**
* The root node for the quad tree.
* @private {goog.structs.QuadTree.Node}
*/
this.root_ =
new goog.structs.QuadTree.Node(minX, minY, maxX - minX, maxY - minY);
};
/**
* Returns a reference to the tree's root node. Callers shouldn't modify nodes,
* directly. This is a convenience for visualization and debugging purposes.
* @return {goog.structs.QuadTree.Node} The root node.
*/
goog.structs.QuadTree.prototype.getRootNode = function() {
return this.root_;
};
/**
* Sets the value of an (x, y) point within the quad-tree.
* @param {number} x The x-coordinate.
* @param {number} y The y-coordinate.
* @param {*} value The value associated with the point.
*/
goog.structs.QuadTree.prototype.set = function(x, y, value) {
var root = this.root_;
if (x < root.x || y < root.y || x > root.x + root.w || y > root.y + root.h) {
throw Error('Out of bounds : (' + x + ', ' + y + ')');
}
if (this.insert_(root, new goog.structs.QuadTree.Point(x, y, value))) {
this.count_++;
}
};
/**
* Gets the value of the point at (x, y) or null if the point is empty.
* @param {number} x The x-coordinate.
* @param {number} y The y-coordinate.
* @param {*=} opt_default The default value to return if the node doesn't
* exist.
* @return {*} The value of the node, the default value if the node
* doesn't exist, or undefined if the node doesn't exist and no default
* has been provided.
*/
goog.structs.QuadTree.prototype.get = function(x, y, opt_default) {
var node = this.find_(this.root_, x, y);
return node ? node.point.value : opt_default;
};
/**
* Removes a point from (x, y) if it exists.
* @param {number} x The x-coordinate.
* @param {number} y The y-coordinate.
* @return {*} The value of the node that was removed, or null if the
* node doesn't exist.
*/
goog.structs.QuadTree.prototype.remove = function(x, y) {
var node = this.find_(this.root_, x, y);
if (node) {
var value = node.point.value;
node.point = null;
node.nodeType = goog.structs.QuadTree.NodeType.EMPTY;
this.balance_(node);
this.count_--;
return value;
} else {
return null;
}
};
/**
* Returns true if the point at (x, y) exists in the tree.
* @param {number} x The x-coordinate.
* @param {number} y The y-coordinate.
* @return {boolean} Whether the tree contains a point at (x, y).
*/
goog.structs.QuadTree.prototype.contains = function(x, y) {
return this.get(x, y) != null;
};
/**
* @return {boolean} Whether the tree is empty.
*/
goog.structs.QuadTree.prototype.isEmpty = function() {
return this.root_.nodeType == goog.structs.QuadTree.NodeType.EMPTY;
};
/**
* @return {number} The number of items in the tree.
*/
goog.structs.QuadTree.prototype.getCount = function() {
return this.count_;
};
/**
* Removes all items from the tree.
*/
goog.structs.QuadTree.prototype.clear = function() {
this.root_.nw = this.root_.ne = this.root_.sw = this.root_.se = null;
this.root_.nodeType = goog.structs.QuadTree.NodeType.EMPTY;
this.root_.point = null;
this.count_ = 0;
};
/**
* Returns an array containing the coordinates of each point stored in the tree.
* @return {!Array} Array of coordinates.
*/
goog.structs.QuadTree.prototype.getKeys = function() {
var arr = [];
this.traverse_(this.root_, function(node) {
arr.push(new goog.math.Coordinate(node.point.x, node.point.y));
});
return arr;
};
/**
* Returns an array containing all values stored within the tree.
* @return {!Array