mellowdeep

rectIntersection

Jun 29th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package methodsExercises.pr11RectangleIntersection;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7.  
  8. public class Pr11RectangleIntersection {
  9.  
  10.     public static void main(String[] args) {
  11.        
  12.     try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));){
  13.        
  14.         Map<String,Rectangle> listOfRectangles = new LinkedHashMap<>();
  15.         String[] params = reader.readLine().trim().split("\\s+");
  16.         int ids = Integer.valueOf(params[0]);
  17.         int numberOfIntersections = Integer.valueOf(params[1]);
  18.        
  19.         for (int i = 0; i < ids; i++) {
  20.             params = reader.readLine().split("\\s+");
  21.            
  22.             String id = params[0];
  23.             double width = Double.valueOf(params[1]);
  24.             double height = Double.valueOf(params[2]);
  25.             double horizontal = Double.valueOf(params[3]);
  26.             double vertical = Double.valueOf(params[4]);
  27.            
  28.             Rectangle rectangle = new Rectangle(id, width, height, horizontal, vertical);
  29.             listOfRectangles.put(id, rectangle);
  30.         }
  31.         for (int i = 0; i < numberOfIntersections; i++) {
  32.             String[] idsForIntersection = reader.readLine().split("\\s+");
  33.             String firstRect = idsForIntersection[0];
  34.             String secondRect = idsForIntersection[1];
  35.             System.out.println(listOfRectangles.get(firstRect)
  36.                     .doesIntersect(listOfRectangles.get(secondRect)));
  37.         }
  38.        
  39.     } catch (Exception e) {
  40.         e.printStackTrace();
  41.     }
  42.     }
  43.    
  44. }
  45. private String id;
  46.     private double minX;
  47.     private double minY;
  48.     private double maxX;
  49.     private double maxY;
  50.    
  51.     public Rectangle(String id,
  52.             double width,
  53.             double height,
  54.             double horizontal,
  55.             double vertical) {
  56.         this.id = id;
  57.         this.minX = horizontal;
  58.         this.minY = vertical;
  59.         this.maxX = horizontal + width;
  60.         this.maxY = vertical + height ;
  61.     }
  62.    
  63.     public boolean doesIntersect(Rectangle other){
  64.          return this.maxX >= other.minX &&
  65.                    this.minX <= other.maxX &&
  66.                    this.maxY >= other.minY &&
  67.                    this.minY <= other.maxY;        
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment