Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package methodsExercises.pr11RectangleIntersection;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Pr11RectangleIntersection {
- public static void main(String[] args) {
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));){
- Map<String,Rectangle> listOfRectangles = new LinkedHashMap<>();
- String[] params = reader.readLine().trim().split("\\s+");
- int ids = Integer.valueOf(params[0]);
- int numberOfIntersections = Integer.valueOf(params[1]);
- for (int i = 0; i < ids; i++) {
- params = reader.readLine().split("\\s+");
- String id = params[0];
- double width = Double.valueOf(params[1]);
- double height = Double.valueOf(params[2]);
- double horizontal = Double.valueOf(params[3]);
- double vertical = Double.valueOf(params[4]);
- Rectangle rectangle = new Rectangle(id, width, height, horizontal, vertical);
- listOfRectangles.put(id, rectangle);
- }
- for (int i = 0; i < numberOfIntersections; i++) {
- String[] idsForIntersection = reader.readLine().split("\\s+");
- String firstRect = idsForIntersection[0];
- String secondRect = idsForIntersection[1];
- System.out.println(listOfRectangles.get(firstRect)
- .doesIntersect(listOfRectangles.get(secondRect)));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- private String id;
- private double minX;
- private double minY;
- private double maxX;
- private double maxY;
- public Rectangle(String id,
- double width,
- double height,
- double horizontal,
- double vertical) {
- this.id = id;
- this.minX = horizontal;
- this.minY = vertical;
- this.maxX = horizontal + width;
- this.maxY = vertical + height ;
- }
- public boolean doesIntersect(Rectangle other){
- return this.maxX >= other.minX &&
- this.minX <= other.maxX &&
- this.maxY >= other.minY &&
- this.minY <= other.maxY;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment