import java.util.Scanner;

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

        System.out.println("Enter the coordinates and dimensions of the first rectangle:");
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double width1 = scanner.nextDouble();
        double height1 = scanner.nextDouble();

        System.out.println("Enter the coordinates and dimensions of the second rectangle:");
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        double width2 = scanner.nextDouble();
        double height2 = scanner.nextDouble();

        // Check if the rectangles overlap
        boolean overlap = (Math.abs(x1 - x2) < (width1 + width2) / 2) && (Math.abs(y1 - y2) < (height1 + height2) / 2);

        if (overlap) {
            System.out.println("The rectangles overlap.");
        } else {
            System.out.println("The rectangles do not overlap.");
        }
    }
}
用java写一段代码输入两个矩形的中心与长宽判断两个矩形是否重合

原文地址: http://www.cveoy.top/t/topic/bnpk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录