Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Point {
- private final int x;
- private final int y;
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public Point(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public boolean equals(Object o) {
- if (o == null) {
- return false;
- }
- if (this.getClass() != o.getClass()){
- return false;
- }
- Point that = (Point) o;
- return (this.x == that.x) && (this.y == that.y);
- }
- @Override
- public int hashCode() {
- return 31 * x + 37 * y;
- }
- @Override
- public String toString() {
- return "Point{" + "x=" + x + ", y=" + y + '}';
- }
- }
- //-------------------------------
- public class Area_question {
- public final NavigableSet<Point> xIndex
- = new TreeSet<>(new Comparator<Point>() {
- @Override
- public int compare(Point o1, Point o2) {
- return o1.getX() - o2.getX();
- }
- });
- public final NavigableSet<Point> yIndex
- = new TreeSet<>(new Comparator<Point>() {
- @Override
- public int compare(Point o1, Point o2) {
- return o1.getY() - o2.getY();
- }
- });
- void addPoint(Point point){
- xIndex.add(point);
- yIndex.add(point);
- }
- Collection<Point> selectPoints(
- int leftX, int rightX,
- int bottomY, int topY){
- Point leftEdge = new Point(leftX, 0);
- Point rightEdge = new Point(rightX, 0);
- Point bottomEdge = new Point(0, bottomY);
- Point topEdge = new Point(0, topY);
- Collection<Point> xSelection = xIndex
- .subSet(leftEdge, true, rightEdge, true);
- Collection<Point> ySelection = yIndex
- .subSet(bottomEdge, true, topEdge, true);
- System.out.println("x selection:");
- for (Point p: xSelection)
- System.out.println(p);
- System.out.println("---------");
- xSelection.retainAll(ySelection);
- System.out.println("after y selection:");
- for (Point p: xSelection)
- System.out.println(p);
- System.out.println("---------");
- return xSelection;
- }
- }
- //-------------------------------
- public class Area_test {
- public static void main(String[] args) {
- Area_question area = new Area_question();
- area.addPoint(new Point(100, 101));
- area.addPoint(new Point(902, 103));
- area.addPoint(new Point(104, 905));
- area.addPoint(new Point(906, 907));
- System.out.println("Before");
- for (Point p: area.xIndex)
- System.out.println(p);
- System.out.println("=======");
- System.out.println(area.selectPoints(0,500,1,490));
- System.out.println("=======");
- System.out.println(area.selectPoints(0,500,1,490));
- System.out.println("After");
- for (Point p: area.xIndex)
- System.out.println(p);
- }
- }
- //-------------------------------
- Результаты:
- Before
- Point{x=100, y=101}
- Point{x=104, y=905}
- Point{x=902, y=103}
- Point{x=906, y=907}
- =======
- x selection:
- Point{x=100, y=101}
- Point{x=104, y=905}
- ---------
- after y selection:
- Point{x=100, y=101}
- ---------
- [Point{x=100, y=101}]
- =======
- x selection:
- Point{x=100, y=101}
- ---------
- after y selection:
- Point{x=100, y=101}
- ---------
- [Point{x=100, y=101}]
- After
- Point{x=100, y=101}
- Point{x=902, y=103}
- Point{x=906, y=907}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement