Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package sonc.quad;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Set;
  6.  
  7.  
  8. class NodeTrie<T extends HasPoint> extends Trie<T>
  9. {
  10.  
  11. HashMap<Quadrant, Trie<T>> tries = new HashMap<>();
  12.  
  13. // Constructor
  14. NodeTrie(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY)
  15. {
  16. super(topLeftX, topLeftY, bottomRightX, bottomRightY);
  17. tries.put(Quadrant.NE, new LeafTrie<T>(this.topLeftX + ((this.bottomRightX - topLeftX) / 2), this.topLeftY, this.bottomRightX, this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2)));
  18. tries.put(Quadrant.NW, new LeafTrie<T>(this.topLeftX, this.topLeftY, this.topLeftX + ((this.bottomRightX - this.topLeftX) / 2), this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2)));
  19. tries.put(Quadrant.SE, new LeafTrie<T>(this.topLeftX + ((this.bottomRightX - this.topLeftX) / 2), this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2), this.bottomRightX, this.bottomRightY));
  20. tries.put(Quadrant.SW, new LeafTrie<T>(this.topLeftX + ((this.bottomRightX - this.topLeftX) / 2), this.topLeftY + ((this.bottomRightY - this.topLeftY) / 2), this.bottomRightX, this.bottomRightY));
  21.  
  22. }
  23.  
  24. void collectAll(Set<T> nodes)
  25. {
  26. for (Map.Entry<Quadrant, Trie<T>> entry : tries.entrySet())
  27. {
  28. entry.getValue().collectAll(nodes);
  29. }
  30. }
  31.  
  32. void collectNear(double x, double y, double radius, Set<T> nodes)
  33. {
  34. for (Map.Entry<Quadrant, Trie<T>> entry : tries.entrySet())
  35. {
  36. entry.getValue().collectNear(x, y, radius, nodes);
  37. }
  38. }
  39.  
  40. void delete(T point)
  41. {
  42. tries.get(this.quadrantOf(point)).delete(point);
  43. }
  44.  
  45. T find(T point)
  46. {
  47. Quadrant quadrant = this.quadrantOf(point);
  48.  
  49. if (quadrant != null)
  50. return tries.get(quadrant).find(point);
  51. else
  52. return null;
  53. }
  54.  
  55. Trie<T> insert(T point)
  56. {
  57. Trie<T> t_aux = tries.get(this.quadrantOf(point));
  58. Quadrant q_aux = quadrantOf(point);
  59. if(q_aux!=null) {
  60. tries.remove(q_aux);
  61. tries.put(q_aux, t_aux.insert(point));
  62. }
  63. return this;
  64. }
  65.  
  66. Trie<T> insertReplace(T point)
  67. {
  68. try
  69. {
  70. Trie<T> t_aux = tries.get(this.quadrantOf(point));
  71. Quadrant q_aux = quadrantOf(point);
  72. tries.remove(q_aux);
  73. tries.put(q_aux, t_aux.insertReplace(point));
  74. }
  75. catch (PointOutOfBoundException e)
  76. {
  77. e.printStackTrace();
  78. }
  79.  
  80. return this;
  81. }
  82.  
  83. Quadrant quadrantOf(T point)
  84. {
  85. if (tries.get(Quadrant.NE).contains(point))
  86. return Quadrant.NE;
  87. System.out.print("aqui: " + tries.get(Quadrant.NW).contains(point));
  88. if (tries.get(Quadrant.NW).contains(point))
  89. return Quadrant.NW;
  90. else if (tries.get(Quadrant.SE).contains(point))
  91. return Quadrant.SE;
  92. else if (tries.get(Quadrant.SW).contains(point))
  93. return Quadrant.SW;
  94. //else
  95. //throw new PointOutOfBoundException("quadrantOf: Point not find in quadrant");
  96. return null;
  97. }
  98.  
  99. boolean contains(T point)
  100. {
  101. return tries.get(Quadrant.NE).contains(point) || tries.get(Quadrant.NW).contains(point) || tries.get(Quadrant.SE).contains(point) || tries.get(Quadrant.SW).contains(point);
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement