Advertisement
Alice_Kim

Quadtree

Jan 18th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. package pgdp.datastructures;
  2.  
  3. import java.util.NoSuchElementException;
  4.  
  5. public class QuadTreeKnotenImpl implements QuadTreeKnoten {
  6. private QuadTreeKnoten topLeft = null;
  7. private QuadTreeKnoten topRight = null;
  8. private QuadTreeKnoten bottomRight = null;
  9. private QuadTreeKnoten bottomLeft = null;
  10. private int dimension;
  11. private int color;
  12.  
  13. private QuadTreeKnotenImpl() {
  14. }
  15.  
  16. public QuadTreeKnotenImpl(int dimension, int color) {
  17. this.dimension = dimension;
  18. this.color = color;
  19. }
  20.  
  21. @Override
  22. public QuadTreeKnoten getTopLeft() {
  23. if (topLeft.isLeaf()) {
  24. throw new NoSuchElementException();
  25. }
  26. return topLeft;
  27. }
  28.  
  29. @Override
  30. public QuadTreeKnoten getTopRight() {
  31. if (topRight.isLeaf()) {
  32. throw new NoSuchElementException();
  33. }
  34. return topRight;
  35. }
  36.  
  37. @Override
  38. public QuadTreeKnoten getBottomLeft() {
  39. if (bottomLeft.isLeaf()) {
  40. throw new NoSuchElementException();
  41. }
  42. return bottomLeft;
  43. }
  44.  
  45. @Override
  46. public QuadTreeKnoten getBottomRight() {
  47. if (bottomRight.isLeaf()) {
  48. throw new NoSuchElementException();
  49. }
  50. return bottomRight;
  51. }
  52.  
  53. @Override
  54. public int getRelativeColor(int x, int y) {
  55. if (x < 0 || y < 0 || x >= dimension || y >= dimension) {
  56. throw new IllegalArgumentException();
  57. }
  58. if (isLeaf()) {
  59. return color;
  60. }
  61. ChildAndCoordinates child = getChildByCoordinates(x, y);
  62. return child.child.getRelativeColor(child.x, child.y);
  63. }
  64.  
  65. @Override
  66. public void setRelativeColor(int x, int y, int color) {
  67. if (x < 0 || y < 0 || x >= dimension || y >= dimension) {
  68. throw new IllegalArgumentException();
  69. }
  70. if (isLeaf() && this.color == color) {
  71. return;
  72. }
  73. if (dimension == 1) {
  74. this.color = color;
  75. return;
  76. }
  77. ChildAndCoordinates child = getChildByCoordinates(x, y);
  78. child.child.setRelativeColor(child.x, child.y, color);
  79. if (topLeft.isLeaf() && topRight.isLeaf() &&
  80. bottomLeft.isLeaf() && bottomRight.isLeaf()) {
  81. int child_color = topLeft.getRelativeColor(0, 0);
  82. if (topRight.getRelativeColor(0, 0) == child_color &&
  83. bottomLeft.getRelativeColor(0, 0) == child_color &&
  84. bottomRight.getRelativeColor(0, 0) == child_color) {
  85. this.color = child_color;
  86. topLeft = null;
  87. topRight = null;
  88. bottomLeft = null;
  89. bottomRight = null;
  90. }
  91. }
  92. }
  93.  
  94. @Override
  95. public int getDimension() {
  96. return dimension;
  97. }
  98.  
  99. @Override
  100. public int getSize() {
  101. int count = 1;
  102. if (topRight != null) {
  103. count += topRight.getSize();
  104. count += topLeft.getSize();
  105. count += bottomRight.getSize();
  106. count += bottomLeft.getSize();
  107. }
  108. return count;
  109. }
  110.  
  111. @Override
  112. public boolean isLeaf() {
  113. if (this.getBottomLeft() == null && this.getBottomRight() == null
  114. && this.getTopLeft() == null && this.getTopRight() == null)
  115. return true;
  116. return false;
  117. }
  118.  
  119. @Override
  120. public int[][] toArray() {
  121. int[][] image = new int[dimension][dimension];
  122. for (int i = 0; i < image.length; i++) {
  123. for (int j = 0; j < image.length; j++) {
  124. try {
  125. image[i][j] = getRelativeColor(i, j);
  126. } catch (IllegalArgumentException ex) {
  127. continue;
  128. }
  129. }
  130. }
  131. return image;
  132. }
  133.  
  134. public static QuadTreeKnoten buildFromIntArray(int[][] image) {
  135. // set node
  136. // get color from array image
  137. // set color in tree
  138. // return root
  139.  
  140. return null;
  141. }
  142.  
  143. private class ChildAndCoordinates {
  144. public QuadTreeKnoten child = null;
  145. public int x;
  146. public int y;
  147.  
  148. ChildAndCoordinates(QuadTreeKnoten child, int x, int y) {
  149. this.child = child;
  150. this.x = x;
  151. this.y = y;
  152. }
  153. }
  154.  
  155. private ChildAndCoordinates getChildByCoordinates(int x, int y) {
  156. if (isLeaf()) {
  157. topLeft = new QuadTreeKnotenImpl(dimension / 2, color);
  158. topRight = new QuadTreeKnotenImpl(dimension / 2, color);
  159. bottomLeft = new QuadTreeKnotenImpl(dimension / 2, color);
  160. bottomRight = new QuadTreeKnotenImpl(dimension / 2, color);
  161. }
  162.  
  163. int midPoint = getDimension() / 2;
  164. if (x < midPoint) {
  165. if (y < midPoint) {
  166. return new ChildAndCoordinates(topLeft, x, y);
  167. } else {
  168. return new ChildAndCoordinates(bottomLeft, x, y - midPoint);
  169. }
  170. } else {
  171. if (y < midPoint) {
  172. return new ChildAndCoordinates(topRight, x - midPoint, y);
  173. } else {
  174. return new ChildAndCoordinates(bottomRight, x - midPoint, y - midPoint);
  175. }
  176. }
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement