HW3.28

时间:2023-03-09 06:20:18
HW3.28

HW3.28

HW3.28

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);

         System.out.print("Enter r1's center x-, y-coordinates, width, and height: ");
         double x1 = input.nextDouble();
         double y1 = input.nextDouble();
         double width1 = input.nextDouble();
         double height1 = input.nextDouble();

         System.out.print("Enter r2's center x-, y-coordinates, width, and height: ");
         double x2 = input.nextDouble();
         double y2 = input.nextDouble();
         double width2 = input.nextDouble();
         double height2 = input.nextDouble();

         input.close();

         boolean isInside = false;

         if((x1 - width1 / 2 >= x2 - width2 / 2) && (x1 + width1 / 2 <= x2 + width2 / 2) &&
             (y1 - height1 / 2 >= y2 - height2 / 2) && (y1 + height1 / 2 <= y2 + height2 / 2))
         {
             System.out.println("r1 is inside r2");
             isInside = true;
         }
         else if((x2 - width2 / 2 >= x1 - width1 / 2) && (x2 + width2 / 2 <= x1 + width1 / 2) &&
             (y2 - height2 / 2 >= y1 - height1 / 2) && (y2 + height2 / 2 <= y1 + height1 / 2))
         {
             System.out.println("r2 is inside r1");
             isInside = true;
         }

         if(!isInside)
         {
             if(x1 >= x2)
             {
                 if(y1 >= y2)
                 {
                     if((x1 - width1 / 2 >= x2 + width2 / 2) && (y1 - width1 / 2 >= y2 + width2 / 2))
                         System.out.println("r2 does not overlap r1");
                     else
                         System.out.println("r2 overlaps r1");
                 }
                 else
                 {
                     if((x1 - width1 / 2 >= x2 + width2 / 2) && (y1 + width1 / 2 <= y2 - width2 / 2))
                         System.out.println("r2 does not overlap r1");
                     else
                         System.out.println("r2 overlaps r1");
                 }
             }
             else
             {
                 if(y1 >= y2)
                 {
                     if((x1 + width1 / 2 <= x2 - width2 / 2) && (y1 - width1 / 2 >= y2 + width2 / 2))
                         System.out.println("r2 does not overlap r1");
                     else
                         System.out.println("r2 overlaps r1");
                 }
                 else
                 {
                     if((x1 + width1 / 2 <= x2 - width2 / 2) && (y1 + width1 / 2 <= y2 - width2 / 2))
                         System.out.println("r2 does not overlap r1");
                     else
                         System.out.println("r2 overlaps r1");
                 }
             }
         }
     }
 }