Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. package bearmaps;
  2.  
  3. import java.util.List;
  4.  
  5. public class KDTree {
  6. /* @source <a href = "https://www.youtube.com/watch?v=irkJ4gczM0I&feature=youtu.be">
  7. Project 2b Walkthrough </a>
  8. */
  9.  
  10. //Enum
  11. private static final boolean HORIZONTAL = false;
  12. private static final boolean VERTICAL = true;
  13.  
  14. //Declaration of private class
  15. private class Node {
  16. private Point p;
  17. private boolean orientation;
  18. private Node leftChild;
  19. private Node rightChild;
  20.  
  21. Node(Point p, boolean orientation) {
  22. this.p = p;
  23. this.orientation = orientation;
  24. }
  25.  
  26. //Returns this Node's point
  27. public Point getPoint() {
  28. return this.p;
  29. }
  30.  
  31. //Calculates distance between this and parameter Point other
  32. public double getDistanceBetween(Point other) {
  33. double diffInX = p.getX() - other.getX();
  34. double diffInY = p.getY() - other.getY();
  35. return Math.sqrt(Math.pow(diffInX, 2) + Math.pow(diffInY, 2));
  36. }
  37. }
  38.  
  39. //Declaration of instance variables
  40. private Node root;
  41.  
  42. /* Basic Constructor */
  43. public KDTree(List<Point> points) {
  44. for (Point p: points) {
  45. root = add(p, root, HORIZONTAL);
  46. }
  47. }
  48.  
  49. //Insert method
  50. private Node add(Point p, Node n, boolean orientation) {
  51. if (n == null) {
  52. return new Node(p, orientation);
  53. }
  54. if (p.equals(n.p)) {
  55. return n;
  56. }
  57.  
  58. int compare = comparePoints(p, n.getPoint(), orientation);
  59. if (compare < 0) {
  60. n.leftChild = add(p, n.leftChild, !orientation);
  61. } else {
  62. n.rightChild = add(p, n.rightChild, !orientation);
  63. }
  64. return n;
  65. }
  66.  
  67. /* Compare two points with a given orientation */
  68. private int comparePoints(Point a, Point b, boolean orientation) {
  69. if (orientation == HORIZONTAL) {
  70. return Double.compare(a.getX(), b.getX());
  71. } else {
  72. return Double.compare(a.getY(), b.getY());
  73. }
  74. }
  75. /* Returns the closest point to the inputted coordinates.
  76. This should take O(logN) time on average, where N is the number of points.
  77. */
  78. public Point nearest(double x, double y) {
  79. Point goal = new Point(x, y);
  80. return nearestHelper(root, goal, root, HORIZONTAL).getPoint();
  81. }
  82.  
  83. //Helper method for nearest
  84. private Node nearestHelper(Node n, Point goal, Node best, boolean orientation) {
  85. if (n == null) {
  86. return best;
  87. }
  88. if (n.getDistanceBetween(goal) < best.getDistanceBetween(goal)) {
  89. best = n;
  90. }
  91. Node goodSide = null;
  92. Node badSide = null;
  93. if (comparePoints(goal, n.getPoint(), orientation) < 0) {
  94. goodSide = n.leftChild;
  95. badSide = n.rightChild;
  96. } else {
  97. goodSide = n.rightChild;
  98. badSide = n.leftChild;
  99. }
  100.  
  101. //Determine best pont on bad side
  102. Point bestBadSidePoint = null;
  103. if (orientation == HORIZONTAL) {
  104. bestBadSidePoint = new Point(n.getPoint().getX(), goal.getY());
  105. } else {
  106. bestBadSidePoint = new Point(goal.getX(), n.getPoint().getY());
  107. }
  108. best = nearestHelper(goodSide, goal, best, !orientation);
  109.  
  110. //Pruning rule
  111. if (Point.distance(bestBadSidePoint, goal) < Point.distance(best.getPoint(), goal)) {
  112. best = nearestHelper(badSide, goal, best, !orientation);
  113. }
  114. return best;
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement