【问题标题】:How can I see if two rectangles have one equal point using java?java - 如何使用java查看两个矩形是否有一个相等的点?
【发布时间】:2014-11-06 00:49:15
【问题描述】:

我有矩形 A = 左上点 (-4,-1)、右下点 (-2-2) 和矩形 B 左上点 (-2,-2)、右下点 (-1,- 3)

然后:

Rectangle first = new Rectangle(-4,-1,2,1);  //x1,y1,width,height
Rectangle second = new Rectangle(-2,-2,1,1);

好的,我有 2 个具有 1 个相同点的矩形。 我使用 first.intersects(second) 并返回 false ..

如何使用 java 来做到这一点,如果矩形 A 中的一个或多个点属于矩形 B,我需要返回 True?

我现在的代码:

公共类 GeometriaRectangle2 {

public static void main(String[] args) {
    Scanner lector = new Scanner(System.in);
    while (lector.hasNextLine()) {
        String a[] = lector.nextLine().split(",");
        int b[] = new int[a.length];

        for (int i = 0; i < a.length; i++) {
            b[i] = Integer.parseInt(a[i]);
        }
        int c = Math.abs(b[0] - b[2]);
        int d = Math.abs(b[1] - b[3]);
        int e = Math.abs(b[4] - b[6]);
        int f = Math.abs(b[5] - b[7]);
        Rectangle r = new Rectangle(b[0], b[1], c, d);
        Rectangle r2 = new Rectangle(b[4], b[5], e, f);
        int tw = r.width;
        int th = r.height;
        int rw = r2.width;
        int rh = r2.height;
        if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
            System.out.println(false);
        }
        int tx = r.x;
        int ty = r.y;
        int rx = r2.x;
        int ry = r2.y;
        rw += rx;
        rh += ry;
        tw += tx;
        th += ty;
        // overflow || intersect or attached
        if ((rw < rx || rw >= tx) && (rh < ry || rh >= ty)
                && (tw < tx || tw >= rx) && (th < ty || th >= ry)) {
            System.out.println(true);
        } else {
            System.out.println(false);
        }
    }
}

}

示例:输入:

-3,3,-1,1,1,-1,3,-3

-3,3,-1,1,-2,4,2,2

输出:

错误

是的

【问题讨论】:

  • 根据你的例子,这两个矩形不感兴趣......
  • Java 已经为你处理了这个问题:Rectangle intersection = first.intersection(second); 或者你可以用这个answer写一个...
  • @Mr.Polywhirl 返回false,因为这两个矩形不重叠...

标签: java


【解决方案1】:

java.awt.Rectangle 是 AWT 的一部分,旨在表示图像(和屏幕上)的离散区域。点和维度是整数。 intersects() 方法做你想要的,你给的矩形不相交。 false 是正确的值。

想想图像中的像素。 java.awt.Rectangle 的坐标本质上是像素的中心,尺寸是计数。因此,例如new Rectangle(-4, -1, 2, 1),它的右下角并不像您假设的那样位于 (-2, -2),它的右下角位于 (-3, -1):

红色矩形最左上角的像素是 (-4, -1) 处的像素。最底部最右边的像素是 (-3, -1) 处的像素。另请注意,它不会与蓝色矩形重叠。

即使不是这种情况,您的矩形也不会相交。它们在一点接触,但不重叠。可以说,交叉点是 (-2, -2) 处的“矩形”,宽度为 0,高度为 0 - 它是空的。

【讨论】:

  • 它的右下角在 (-3, -1)。为什么?如果宽度为 2 且高度为 1 右下角不是 -2,-2?
  • @MiguelGarcíaGonzález 因为您不会像图像中的像素那样考虑它,这是 AWT 类旨在处理的。我添加了一张图片。
【解决方案2】:

您还可以修改intersects 函数的当前代码以满足您的需要。在您的情况下,当两个矩形(甚至是边线)中的一对点共享相同的坐标时,您想声明它是正确的 - (阅读:附加)。因此,您可以尝试以下方式:

public boolean intersectsOrAttached(Rectangle r, Rectangle r2) {
    int tw = r.width;
    int th = r.height;
    int rw = r2.width;
    int rh = r2.height;
    if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
        return false;
    }

    int tx = r.x;
    int ty = r.y;
    int rx = r2.x;
    int ry = r2.y;
    rw += rx;
    rh += ry;
    tw += tx;
    th += ty;

    //overflow || intersect or attached
    return ((rw < rx || rw >= tx) &&
        (rh < ry || rh >= ty) &&
        (tw < tx || tw >= rx) &&
        (th < ty || th >= ry));
}

更新

其实在线评审系统有点棘手。 我设法让它部分接受,我不确定是时间限制还是内存限制,以及一些我们无法轻易弄清楚的被忽视的边界情况。 不过在这里,最好自己优化一下:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class OverlappingRectangles {

    public static void main(String[] args) {
        String f = args[0];
        try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            while (br.ready()) {
                String cs[] = br.readLine().split(",");
                int[] ci = new int[cs.length];
                int i = 0;
                for (String s : cs) {
                    ci[i] = Integer.parseInt(s);
                    i += 1;
                }

                if (ck(ci[0], ci[1], ci[2], ci[3], ci[4], ci[5], ci[6], ci[7])) {
                    System.out.println("True");
                } else {
                    System.out.println("False");
                }
            }
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }

    public static boolean ck(int rx, int ry, int rw, int rh, int tx, int ty, int tw, int th) {
        //ATTENTION!
        //rx,ry -> upper-left corner of rect A
        //rw,rh -> bottom-right corner of rect A
        //tx,ty -> upper-left corner of rect B
        //tw,th -> bottom-right corner of rect B
        return ((rw < rx || rw >= tx)
                && (rh < ry || rh >= ty)
                && (tw < tx || tw >= rx)
                && (th < ty || th >= ry));
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 2023-03-07
    • 2012-08-22
    • 1970-01-01
    • 2023-03-14
    • 2020-05-05
    • 1970-01-01
    相关资源
    最近更新 更多