【问题标题】:How do I find the intersection of a Rectangle?如何找到矩形的交集?
【发布时间】:2022-01-20 01:48:40
【问题描述】:
import java.util.Scanner;

public class RectangleTester {

    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle(0, 0, 0, 0);
        Rectangle rectangle2 = new Rectangle(0, 0, 0, 0);

        Scanner input = new Scanner(System.in);
        System.out.println("Rectangle 1:");
        System.out.println("Enter the x coordinate ");
        int r1x1 = input.nextInt();
        System.out.println("Enter the y coordinate");
        int r1y1 = input.nextInt();
        System.out.println("Enter the width");
        int width1 = input.nextInt();
        System.out.println("Enter the height");
        int height1 = input.nextInt();
        rectangle1.setLeft(r1x1);
        rectangle1.setBottom(r1y1);
        rectangle1.setWidth(width1);
        rectangle1.setHeight(height1);
        
        System.out.println("\n*****************\nRectangle 2:");
        System.out.println("Enter the x coordinate ");
        int r2x1 = input.nextInt();
        System.out.println("Enter the y coordinate");
        int r2y1 = input.nextInt();
        System.out.println("Enter the width");
        int width2 = input.nextInt();
        System.out.println("Enter the height");
        int height2 = input.nextInt();
        rectangle2.setLeft(r2x1);
        rectangle2.setBottom(r2y1);
        rectangle2.setWidth(width2);
        rectangle2.setHeight(height2);
    
        System.out.println("\n****************\n"+"Rectangle 1: \n"+rectangle1.toString()+"\nArea is " + rectangle1.area(rectangle1) + "\nPerimeter is " + rectangle1.perimeter(rectangle1));
        System.out.println("*****************"+"\nRectangle 2: \n"+rectangle2.toString()+"\nArea is " + rectangle2.area(rectangle2) + "\nPerimeter is " + rectangle2.perimeter(rectangle2));
        
        int r1y2=r1y1+height1;
        int r1x2=r1x1+width1;
        int r2y2=r2y1+height2;
        int r2x2=r2x1+width1;
        
        int r3x1=Math.max(r1x1, r2x1);
        int r3x2=Math.min(r1x2, r2x2);
        int r3y1=Math.max(r1y1, r2y1);
        int r3y2=Math.min(r1y2, r2y2);
            
    }

    public static boolean intersection() {      
    }
}

我正在尝试编写一个名为intersection 的方法,它接受两个矩形参数并返回当/如果它们重叠时形成的矩形,但是如果它们不重叠,该方法应该返回一个所有字段都为 0 的矩形

如果以下条件之一为真,则两个矩形不重叠。

  1. 一个矩形高于另一个矩形的上边缘。
  2. 一个矩形位于另一个矩形左边缘的左侧。

如果矩形仅接触,但不重叠,则宽度或高度应为零,但所有其他参数应正确计算和存储。

我真的可以在如何构建这个问题上使用一些帮助,因为逻辑让我感到困惑!

【问题讨论】:

    标签: java intersection rectangles


    【解决方案1】:

    这个问题在 LeetCode 上。 https://leetcode.com/problems/rectangle-overlap/

    考虑一个由左下角和右上角表示的矩形。

    1st rectangle: x1,y1    x2,y2
    2nd rectangle: x3,y3    x4,y4
    

    矩形重叠,如果:

    • x3x4 介于 x1x2 之间
    • x1x2 介于 x3x4 之间

    因此,如果您采用 x 坐标并对它们进行排序,以使它们重叠,则前两个和后两个应该来自不同的矩形。

    同样的逻辑适用于 y 坐标。

    Java没有元组类型,但可以使用Map.Entry类,键为x(或y)值,值为12,表示第一个或第二个矩形。

    【讨论】:

    • OP 已经在使用java.awt.Rectangle。如果他想要一个坐标类,他可以使用java.awt.Point
    【解决方案2】:

    你可以看到方法 java.awt.Rectangle#intersection 这种方法符合您的描述

    附言 从java.awt.Rectangle#intersection实现

        int tx1 = this.x;
        int ty1 = this.y;
        int rx1 = r.x;
        int ry1 = r.y;
        long tx2 = tx1; tx2 += this.width;
        long ty2 = ty1; ty2 += this.height;
        long rx2 = rx1; rx2 += r.width;
        long ry2 = ry1; ry2 += r.height;
        if (tx1 < rx1) tx1 = rx1;
        if (ty1 < ry1) ty1 = ry1;
        if (tx2 > rx2) tx2 = rx2;
        if (ty2 > ry2) ty2 = ry2;
        tx2 -= tx1;
        ty2 -= ty1;
        // tx2,ty2 will never overflow (they will never be
        // larger than the smallest of the two source w,h)
        // they might underflow, though...
        if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE;
        if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE;
        return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
    

    【讨论】:

    • 这似乎是一个编码问题,因此不允许使用Rectangle.intersection
    • 他可以在那里看到实现,并编写他自己的代码。那里的实现非常简单。
    猜你喜欢
    • 2012-11-20
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    相关资源
    最近更新 更多