Advertisement
Guest User

PtsArea

a guest
Jun 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. public class Point {
  2. private final int x;
  3. private final int y;
  4.  
  5. public int getX() {
  6. return x;
  7. }
  8.  
  9. public int getY() {
  10. return y;
  11. }
  12.  
  13. public Point(int x, int y) {
  14. this.x = x;
  15. this.y = y;
  16. }
  17.  
  18. @Override
  19. public boolean equals(Object o) {
  20. if (o == null) {
  21. return false;
  22. }
  23. if (this.getClass() != o.getClass()){
  24. return false;
  25. }
  26.  
  27. Point that = (Point) o;
  28. return (this.x == that.x) && (this.y == that.y);
  29. }
  30.  
  31. @Override
  32. public int hashCode() {
  33. return 31 * x + 37 * y;
  34. }
  35.  
  36. @Override
  37. public String toString() {
  38. return "Point{" + "x=" + x + ", y=" + y + '}';
  39. }
  40. }
  41.  
  42. //-------------------------------
  43. public class Area_question {
  44. public final NavigableSet<Point> xIndex
  45. = new TreeSet<>(new Comparator<Point>() {
  46. @Override
  47. public int compare(Point o1, Point o2) {
  48. return o1.getX() - o2.getX();
  49. }
  50. });
  51. public final NavigableSet<Point> yIndex
  52. = new TreeSet<>(new Comparator<Point>() {
  53. @Override
  54. public int compare(Point o1, Point o2) {
  55. return o1.getY() - o2.getY();
  56. }
  57. });
  58. void addPoint(Point point){
  59. xIndex.add(point);
  60. yIndex.add(point);
  61. }
  62.  
  63. Collection<Point> selectPoints(
  64. int leftX, int rightX,
  65. int bottomY, int topY){
  66. Point leftEdge = new Point(leftX, 0);
  67. Point rightEdge = new Point(rightX, 0);
  68. Point bottomEdge = new Point(0, bottomY);
  69. Point topEdge = new Point(0, topY);
  70. Collection<Point> xSelection = xIndex
  71. .subSet(leftEdge, true, rightEdge, true);
  72. Collection<Point> ySelection = yIndex
  73. .subSet(bottomEdge, true, topEdge, true);
  74.  
  75. System.out.println("x selection:");
  76. for (Point p: xSelection)
  77. System.out.println(p);
  78. System.out.println("---------");
  79.  
  80. xSelection.retainAll(ySelection);
  81.  
  82. System.out.println("after y selection:");
  83. for (Point p: xSelection)
  84. System.out.println(p);
  85. System.out.println("---------");
  86.  
  87. return xSelection;
  88. }
  89. }
  90.  
  91. //-------------------------------
  92. public class Area_test {
  93. public static void main(String[] args) {
  94. Area_question area = new Area_question();
  95. area.addPoint(new Point(100, 101));
  96. area.addPoint(new Point(902, 103));
  97. area.addPoint(new Point(104, 905));
  98. area.addPoint(new Point(906, 907));
  99. System.out.println("Before");
  100. for (Point p: area.xIndex)
  101. System.out.println(p);
  102. System.out.println("=======");
  103. System.out.println(area.selectPoints(0,500,1,490));
  104. System.out.println("=======");
  105. System.out.println(area.selectPoints(0,500,1,490));
  106. System.out.println("After");
  107. for (Point p: area.xIndex)
  108. System.out.println(p);
  109. }
  110. }
  111. //-------------------------------
  112. Результаты:
  113.  
  114. Before
  115. Point{x=100, y=101}
  116. Point{x=104, y=905}
  117. Point{x=902, y=103}
  118. Point{x=906, y=907}
  119. =======
  120. x selection:
  121. Point{x=100, y=101}
  122. Point{x=104, y=905}
  123. ---------
  124. after y selection:
  125. Point{x=100, y=101}
  126. ---------
  127. [Point{x=100, y=101}]
  128. =======
  129. x selection:
  130. Point{x=100, y=101}
  131. ---------
  132. after y selection:
  133. Point{x=100, y=101}
  134. ---------
  135. [Point{x=100, y=101}]
  136. After
  137. Point{x=100, y=101}
  138. Point{x=902, y=103}
  139. Point{x=906, y=907}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement