Advertisement
desislava_topuzakova

2. Point in Rectangle

Oct 25th, 2022
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. Point.java
  2. package pointInRectangle;
  3.  
  4. public class Point {
  5.     private int x;
  6.     private int y;
  7.  
  8.     public Point(int x, int y) {
  9.         //нова точка
  10.         this.x = x;
  11.         this.y = y;
  12.     }
  13.  
  14.     //alt + insert -> getters and setters
  15.     public int getX() {
  16.         return x;
  17.     }
  18.  
  19.     public void setX(int x) {
  20.         this.x = x;
  21.     }
  22.  
  23.     public int getY() {
  24.         return y;
  25.     }
  26.  
  27.     public void setY(int y) {
  28.         this.y = y;
  29.     }
  30. }
  31.  
  32.  
  33. Rectangle.java
  34. package pointInRectangle;
  35.  
  36. public class Rectangle {
  37.     private Point bottomLeft;
  38.     private Point topRight;
  39.  
  40.     public Rectangle(Point bottomLeft, Point topRight) {
  41.         //нов правоъгълник
  42.         this.bottomLeft = bottomLeft;
  43.         this.topRight = topRight;
  44.     }
  45.  
  46.     public Point getBottomLeft() {
  47.         return bottomLeft;
  48.     }
  49.  
  50.     public void setBottomLeft(Point bottomLeft) {
  51.         this.bottomLeft = bottomLeft;
  52.     }
  53.  
  54.     public Point getTopRight() {
  55.         return topRight;
  56.     }
  57.  
  58.     public void setTopRight(Point topRight) {
  59.         this.topRight = topRight;
  60.     }
  61.     //true -> ако дадената точка е в правоъгълника
  62.     //false -> ако дадената точка не е в правоъгълника
  63.     public boolean contains (Point point) {
  64.         //1. да е вътре по х
  65.         boolean isInsideX = point.getX() >= bottomLeft.getX() && point.getX() <= topRight.getX();
  66.         //2. да е вътре по y
  67.         boolean isInsideY = point.getY() >= bottomLeft.getY() && point.getY() <= topRight.getY();
  68.  
  69.         return isInsideX && isInsideY;
  70.     }
  71. }
  72.  
  73.  
  74. Main.java
  75. package pointInRectangle;
  76.  
  77. import java.util.Arrays;
  78. import java.util.Scanner;
  79.  
  80. public class Main {
  81.     public static void main(String[] args) {
  82.         Scanner scanner = new Scanner(System.in);
  83.         //[{bottomLeftX}, {bottomLeftY}, {topRightX}, {topRightY}]
  84.         int [] coordinates = Arrays.stream(scanner.nextLine().split(" "))
  85.                 .mapToInt(Integer::parseInt).toArray();
  86.         //[0] -> bottomLeft x
  87.         int bottomLeftX = coordinates[0];
  88.         //[1] -> bottomLeftY
  89.         int bottomLeftY = coordinates[1];
  90.         //[2] -> topRightX
  91.         int topRightX = coordinates[2];
  92.         //[3] -> topRightY
  93.         int topRightY = coordinates[3];
  94.  
  95.         Point bottomLeft = new Point(bottomLeftX, bottomLeftY); //долу ляво
  96.         Point topRight = new Point(topRightX, topRightY); //горе дясно
  97.         Rectangle rectangle = new Rectangle(bottomLeft, topRight); //правоъгълник
  98.  
  99.         int countPoints = Integer.parseInt(scanner.nextLine()); //общ брой на проверени точки
  100.         for (int pointCount = 1; pointCount <= countPoints; pointCount++) {
  101.             //"0 0" -> [0, 0]
  102.             int [] checkPointCoordinates = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  103.             int x = checkPointCoordinates[0];
  104.             int y = checkPointCoordinates[1];
  105.             Point searchedPoint = new Point(x, y);
  106.             System.out.println(rectangle.contains(searchedPoint));
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement