Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sonc.quad;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- class NodeTrie<T extends HasPoint> extends Trie<T> {
- Map<Quadrant, Trie<T>> descendants;
- NodeTrie(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY) {
- super(topLeftX, topLeftY, bottomRightX, bottomRightY);
- descendants = new HashMap<Quadrant, Trie<T>>();
- /*
- * a---b---c
- * | NW NE |
- * | |
- * d---e---f
- * | SW SE |
- * | |
- * g---h---i
- */
- double middleX = topLeftX + ((bottomRightX - topLeftX) / 2);
- double middleY = topLeftY + ((bottomRightY - topLeftY ) / 2);
- double a1 = topLeftX;
- double a2 = topLeftY;
- double b1 = middleX;
- double b2 = topLeftY;
- double c1 = bottomRightX;
- double c2 = topLeftY;
- //___________________
- double d1 = topLeftX;
- double d2 = middleY;
- double e1 = middleX;
- double e2 = middleY;
- double f1 = bottomRightX;
- double f2 = middleY;
- //___________________
- double g1 = topLeftX;
- double g2 = bottomRightY;
- double h1 = middleX;
- double h2 = bottomRightY;
- double i1 = bottomRightX;
- double i2 = bottomRightY;
- descendants.put(Quadrant.NE, new LeafTrie<T>(b1, b2, f1, f2));
- descendants.put(Quadrant.SE, new LeafTrie<T>(e1, e2, i1, i2);
- descendants.put(Quadrant.NW, new LeafTrie<T>(a1, a2, e1, e2));
- descendants.put(Quadrant.SW, new LeafTrie<T>(d1, d2, h1, h2);
- }
- @Override
- T find(T point) {
- return descendants.get(quadrantOf(point)).find(point);
- }
- Quadrant quadrantOf(T point) {
- double middleX = topLeftX + ((bottomRightX - topLeftX) / 2);
- double middleY = topLeftY + ((bottomRightY - topLeftY ) / 2);
- if (point.getX() > middleX) {
- if (point.getY() < middleY) {
- return Quadrant.NE;
- } else if (point.getY() > middleY)
- return Quadrant.SE;
- } else if(point.getX() < middleX){
- if (point.getY() < middleY) {
- return Quadrant.NW;
- } else if (point.getY() > middleY)
- return Quadrant.SW;
- }
- throw new PointOutOfBounds Exception;
- }
- @Override
- Trie<T> insert(T point) {
- Quadrant quad = quadrantOf(point);
- Trie<T> tmp = descendants.get(quad).insert(point);
- descendants.put(quad, tmp);
- return this;
- }
- @Override
- Trie<T> insertReplace(T point) {
- Quadrant quad = quadrantOf(point);
- Trie<T> tmp = descendants.get(quad);
- descendants.put(quad, tmp.insertReplace(point));
- return this;
- }
- void delete(T point) {
- descendants.get(quadrantOf(point)).delete(point);
- }
- @Override
- void collectNear(double x, double y, double radius, Set<T> nodes) {
- if (nodes == null)
- nodes = new HashSet<T>();
- descendants.get(Quadrant.NW).collectNear(x, y, radius, nodes);
- descendants.get(Quadrant.SW).collectNear(x, y, radius, nodes);
- descendants.get(Quadrant.SE).collectNear(x, y, radius, nodes);
- descendants.get(Quadrant.NE).collectNear(x, y, radius, nodes);
- }
- @Override
- void collectAll(Set<T> nodes) {
- if (nodes == null)
- nodes = new HashSet<T>();
- descendants.get(Quadrant.NW).collectAll(nodes);
- descendants.get(Quadrant.SW).collectAll(nodes);
- descendants.get(Quadrant.SE).collectAll(nodes);
- descendants.get(Quadrant.NE).collectAll(nodes);
- }
- @Override
- public String toString() {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement