Determine if two rectangles overlap each other?

前端 未结 23 1203
礼貌的吻别
礼貌的吻别 2020-11-22 04:09

I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectang

23条回答
  •  Happy的楠姐
    2020-11-22 04:55

    This is from exercise 3.28 from the book Introduction to Java Programming- Comprehensive Edition. The code tests whether the two rectangles are indenticle, whether one is inside the other and whether one is outside the other. If none of these condition are met then the two overlap.

    **3.28 (Geometry: two rectangles) Write a program that prompts the user to enter the center x-, y-coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure 3.9. Test your program to cover all cases. Here are the sample runs:

    Enter r1's center x-, y-coordinates, width, and height: 2.5 4 2.5 43 Enter r2's center x-, y-coordinates, width, and height: 1.5 5 0.5 3 r2 is inside r1

    Enter r1's center x-, y-coordinates, width, and height: 1 2 3 5.5 Enter r2's center x-, y-coordinates, width, and height: 3 4 4.5 5 r2 overlaps r1

    Enter r1's center x-, y-coordinates, width, and height: 1 2 3 3 Enter r2's center x-, y-coordinates, width, and height: 40 45 3 2 r2 does not overlap r1

    import java.util.Scanner;
    
    public class ProgrammingEx3_28 {
    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 w1 = input.nextDouble();
        double h1 = input.nextDouble();
        w1 = w1 / 2;
        h1 = h1 / 2;
        System.out
                .print("Enter r2's center x-, y-coordinates, width, and height:");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        double w2 = input.nextDouble();
        double h2 = input.nextDouble();
        w2 = w2 / 2;
        h2 = h2 / 2;
    
        // Calculating range of r1 and r2
        double x1max = x1 + w1;
        double y1max = y1 + h1;
        double x1min = x1 - w1;
        double y1min = y1 - h1;
        double x2max = x2 + w2;
        double y2max = y2 + h2;
        double x2min = x2 - w2;
        double y2min = y2 - h2;
    
        if (x1max == x2max && x1min == x2min && y1max == y2max
                && y1min == y2min) {
            // Check if the two are identicle
            System.out.print("r1 and r2 are indentical");
    
        } else if (x1max <= x2max && x1min >= x2min && y1max <= y2max
                && y1min >= y2min) {
            // Check if r1 is in r2
            System.out.print("r1 is inside r2");
        } else if (x2max <= x1max && x2min >= x1min && y2max <= y1max
                && y2min >= y1min) {
            // Check if r2 is in r1
            System.out.print("r2 is inside r1");
        } else if (x1max < x2min || x1min > x2max || y1max < y2min
                || y2min > y1max) {
            // Check if the two overlap
            System.out.print("r2 does not overlaps r1");
        } else {
            System.out.print("r2 overlaps r1");
        }
    
    }
    }
    

提交回复
热议问题