2019-10-22 07:15:35 +02:00
|
|
|
/*
|
2018-12-23 03:07:31 +05:30
|
|
|
* The MIT License
|
2019-10-12 20:05:54 +03:00
|
|
|
* Copyright © 2014-2019 Ilkka Seppälä
|
2018-12-23 03:07:31 +05:30
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-10-22 07:15:35 +02:00
|
|
|
|
2018-12-23 03:07:31 +05:30
|
|
|
package com.iluwatar.spatialpartition;
|
|
|
|
|
2020-01-04 22:06:08 +05:30
|
|
|
import java.util.Collection;
|
2018-12-23 03:07:31 +05:30
|
|
|
import java.util.Hashtable;
|
|
|
|
|
|
|
|
/**
|
2019-11-12 01:51:12 +05:30
|
|
|
* The quadtree data structure is being used to keep track of the objects' locations. It has the
|
|
|
|
* insert(Point) and query(range) methods to insert a new object and find the objects within a
|
|
|
|
* certain (rectangular) range respectively.
|
2018-12-23 03:07:31 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
public class QuadTree {
|
|
|
|
Rect boundary;
|
|
|
|
int capacity;
|
|
|
|
boolean divided;
|
|
|
|
Hashtable<Integer, Point> points;
|
|
|
|
QuadTree northwest;
|
|
|
|
QuadTree northeast;
|
|
|
|
QuadTree southwest;
|
|
|
|
QuadTree southeast;
|
|
|
|
|
|
|
|
QuadTree(Rect boundary, int capacity) {
|
|
|
|
this.boundary = boundary;
|
|
|
|
this.capacity = capacity;
|
|
|
|
this.divided = false;
|
2020-01-04 22:06:08 +05:30
|
|
|
this.points = new Hashtable<>();
|
2019-11-12 01:51:12 +05:30
|
|
|
this.northwest = null;
|
|
|
|
this.northeast = null;
|
|
|
|
this.southwest = null;
|
2018-12-23 03:07:31 +05:30
|
|
|
this.southeast = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(Point p) {
|
2019-02-17 02:23:06 +05:30
|
|
|
if (this.boundary.contains(p)) {
|
2018-12-23 03:07:31 +05:30
|
|
|
if (this.points.size() < this.capacity) {
|
|
|
|
points.put(p.id, p);
|
|
|
|
} else {
|
|
|
|
if (!this.divided) {
|
|
|
|
this.divide();
|
|
|
|
}
|
|
|
|
if (this.northwest.boundary.contains(p)) {
|
|
|
|
this.northwest.insert(p);
|
|
|
|
} else if (this.northeast.boundary.contains(p)) {
|
|
|
|
this.northeast.insert(p);
|
|
|
|
} else if (this.southwest.boundary.contains(p)) {
|
|
|
|
this.southwest.insert(p);
|
|
|
|
} else if (this.southeast.boundary.contains(p)) {
|
|
|
|
this.southeast.insert(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void divide() {
|
2020-01-04 22:06:08 +05:30
|
|
|
var x = this.boundary.coordinateX;
|
|
|
|
var y = this.boundary.coordinateY;
|
|
|
|
var width = this.boundary.width;
|
|
|
|
var height = this.boundary.height;
|
|
|
|
var nw = new Rect(x - width / 4, y + height / 4, width / 2, height / 2);
|
2019-11-12 01:51:12 +05:30
|
|
|
this.northwest = new QuadTree(nw, this.capacity);
|
2020-01-04 22:06:08 +05:30
|
|
|
var ne = new Rect(x + width / 4, y + height / 4, width / 2, height / 2);
|
2019-11-12 01:51:12 +05:30
|
|
|
this.northeast = new QuadTree(ne, this.capacity);
|
2020-01-04 22:06:08 +05:30
|
|
|
var sw = new Rect(x - width / 4, y - height / 4, width / 2, height / 2);
|
2019-11-12 01:51:12 +05:30
|
|
|
this.southwest = new QuadTree(sw, this.capacity);
|
2020-01-04 22:06:08 +05:30
|
|
|
var se = new Rect(x + width / 4, y - height / 4, width / 2, height / 2);
|
2019-11-12 01:51:12 +05:30
|
|
|
this.southeast = new QuadTree(se, this.capacity);
|
2018-12-23 03:07:31 +05:30
|
|
|
this.divided = true;
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:06:08 +05:30
|
|
|
Collection<Point> query(Rect r, Collection<Point> relevantPoints) {
|
2018-12-23 03:07:31 +05:30
|
|
|
//could also be a circle instead of a rectangle
|
|
|
|
if (this.boundary.intersects(r)) {
|
2020-01-04 22:06:08 +05:30
|
|
|
this.points
|
|
|
|
.values()
|
|
|
|
.stream()
|
|
|
|
.filter(r::contains)
|
|
|
|
.forEach(relevantPoints::add);
|
2018-12-23 03:07:31 +05:30
|
|
|
if (this.divided) {
|
|
|
|
this.northwest.query(r, relevantPoints);
|
|
|
|
this.northeast.query(r, relevantPoints);
|
|
|
|
this.southwest.query(r, relevantPoints);
|
|
|
|
this.southeast.query(r, relevantPoints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relevantPoints;
|
|
|
|
}
|
|
|
|
}
|