Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Point.java
- package pointInRectangle;
- public class Point {
- private int x;
- private int y;
- public Point(int x, int y) {
- //нова точка
- this.x = x;
- this.y = y;
- }
- //alt + insert -> getters and setters
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- }
- Rectangle.java
- package pointInRectangle;
- public class Rectangle {
- private Point bottomLeft;
- private Point topRight;
- public Rectangle(Point bottomLeft, Point topRight) {
- //нов правоъгълник
- this.bottomLeft = bottomLeft;
- this.topRight = topRight;
- }
- public Point getBottomLeft() {
- return bottomLeft;
- }
- public void setBottomLeft(Point bottomLeft) {
- this.bottomLeft = bottomLeft;
- }
- public Point getTopRight() {
- return topRight;
- }
- public void setTopRight(Point topRight) {
- this.topRight = topRight;
- }
- //true -> ако дадената точка е в правоъгълника
- //false -> ако дадената точка не е в правоъгълника
- public boolean contains (Point point) {
- //1. да е вътре по х
- boolean isInsideX = point.getX() >= bottomLeft.getX() && point.getX() <= topRight.getX();
- //2. да е вътре по y
- boolean isInsideY = point.getY() >= bottomLeft.getY() && point.getY() <= topRight.getY();
- return isInsideX && isInsideY;
- }
- }
- Main.java
- package pointInRectangle;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //[{bottomLeftX}, {bottomLeftY}, {topRightX}, {topRightY}]
- int [] coordinates = Arrays.stream(scanner.nextLine().split(" "))
- .mapToInt(Integer::parseInt).toArray();
- //[0] -> bottomLeft x
- int bottomLeftX = coordinates[0];
- //[1] -> bottomLeftY
- int bottomLeftY = coordinates[1];
- //[2] -> topRightX
- int topRightX = coordinates[2];
- //[3] -> topRightY
- int topRightY = coordinates[3];
- Point bottomLeft = new Point(bottomLeftX, bottomLeftY); //долу ляво
- Point topRight = new Point(topRightX, topRightY); //горе дясно
- Rectangle rectangle = new Rectangle(bottomLeft, topRight); //правоъгълник
- int countPoints = Integer.parseInt(scanner.nextLine()); //общ брой на проверени точки
- for (int pointCount = 1; pointCount <= countPoints; pointCount++) {
- //"0 0" -> [0, 0]
- int [] checkPointCoordinates = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
- int x = checkPointCoordinates[0];
- int y = checkPointCoordinates[1];
- Point searchedPoint = new Point(x, y);
- System.out.println(rectangle.contains(searchedPoint));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement