Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. package sonc.quad;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Map;
  6. import java.util.Set;
  7.  
  8. class NodeTrie<T extends HasPoint> extends Trie<T> {
  9.  
  10. Map<Quadrant, Trie<T>> descendants;
  11.  
  12. NodeTrie(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY) {
  13. super(topLeftX, topLeftY, bottomRightX, bottomRightY);
  14. descendants = new HashMap<Quadrant, Trie<T>>();
  15. /*
  16. * a---b---c
  17. * | NW NE |
  18. * | |
  19. * d---e---f
  20. * | SW SE |
  21. * | |
  22. * g---h---i
  23. */
  24. double middleX = topLeftX + ((bottomRightX - topLeftX) / 2);
  25. double middleY = topLeftY + ((bottomRightY - topLeftY ) / 2);
  26.  
  27. double a1 = topLeftX;
  28. double a2 = topLeftY;
  29.  
  30. double b1 = middleX;
  31. double b2 = topLeftY;
  32.  
  33. double c1 = bottomRightX;
  34. double c2 = topLeftY;
  35. //___________________
  36. double d1 = topLeftX;
  37. double d2 = middleY;
  38.  
  39. double e1 = middleX;
  40. double e2 = middleY;
  41.  
  42. double f1 = bottomRightX;
  43. double f2 = middleY;
  44. //___________________
  45. double g1 = topLeftX;
  46. double g2 = bottomRightY;
  47.  
  48. double h1 = middleX;
  49. double h2 = bottomRightY;
  50.  
  51. double i1 = bottomRightX;
  52. double i2 = bottomRightY;
  53.  
  54. descendants.put(Quadrant.NE, new LeafTrie<T>(b1, b2, f1, f2));
  55. descendants.put(Quadrant.SE, new LeafTrie<T>(e1, e2, i1, i2);
  56. descendants.put(Quadrant.NW, new LeafTrie<T>(a1, a2, e1, e2));
  57. descendants.put(Quadrant.SW, new LeafTrie<T>(d1, d2, h1, h2);
  58. }
  59.  
  60. @Override
  61. T find(T point) {
  62. return descendants.get(quadrantOf(point)).find(point);
  63. }
  64.  
  65. Quadrant quadrantOf(T point) {
  66. double middleX = topLeftX + ((bottomRightX - topLeftX) / 2);
  67. double middleY = topLeftY + ((bottomRightY - topLeftY ) / 2);
  68. if (point.getX() > middleX) {
  69. if (point.getY() < middleY) {
  70. return Quadrant.NE;
  71. } else if (point.getY() > middleY)
  72. return Quadrant.SE;
  73. } else if(point.getX() < middleX){
  74. if (point.getY() < middleY) {
  75. return Quadrant.NW;
  76. } else if (point.getY() > middleY)
  77. return Quadrant.SW;
  78. }
  79. throw new PointOutOfBounds Exception;
  80. }
  81.  
  82. @Override
  83. Trie<T> insert(T point) {
  84. Quadrant quad = quadrantOf(point);
  85. Trie<T> tmp = descendants.get(quad).insert(point);
  86. descendants.put(quad, tmp);
  87. return this;
  88. }
  89.  
  90. @Override
  91. Trie<T> insertReplace(T point) {
  92. Quadrant quad = quadrantOf(point);
  93. Trie<T> tmp = descendants.get(quad);
  94. descendants.put(quad, tmp.insertReplace(point));
  95. return this;
  96. }
  97.  
  98. void delete(T point) {
  99. descendants.get(quadrantOf(point)).delete(point);
  100. }
  101.  
  102. @Override
  103. void collectNear(double x, double y, double radius, Set<T> nodes) {
  104. if (nodes == null)
  105. nodes = new HashSet<T>();
  106. descendants.get(Quadrant.NW).collectNear(x, y, radius, nodes);
  107. descendants.get(Quadrant.SW).collectNear(x, y, radius, nodes);
  108. descendants.get(Quadrant.SE).collectNear(x, y, radius, nodes);
  109. descendants.get(Quadrant.NE).collectNear(x, y, radius, nodes);
  110. }
  111.  
  112. @Override
  113. void collectAll(Set<T> nodes) {
  114. if (nodes == null)
  115. nodes = new HashSet<T>();
  116. descendants.get(Quadrant.NW).collectAll(nodes);
  117. descendants.get(Quadrant.SW).collectAll(nodes);
  118. descendants.get(Quadrant.SE).collectAll(nodes);
  119. descendants.get(Quadrant.NE).collectAll(nodes);
  120. }
  121.  
  122. @Override
  123. public String toString() {
  124. return null;
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement