【问题标题】:How to check if a rectangle is intersecting?如何检查一个矩形是否相交?
【发布时间】:2016-09-04 17:13:25
【问题描述】:

您好,我正在尝试创建此构造函数:public Rectangle createIntersection(Rectangle r){ .... 返回一个新的 Rectangle 对象,该对象表示此 Rectangle 与指定 Rectangle 的交集。

到目前为止,我已经为构造函数做了这个,但我不确定它是否正确:

public Rectangle createIntersection(Rectangle r) { 
   Rectangle r1 = new Rectangle () ; 
   Rectangle r2 = new Rectangle ();
   r2.setRect(r);
   r2.createIntersection(r1); 
   return r2;
}

然后我应该创建这个构造函数public Boolean intersects (Rectangle r),如果它与指定的 Rectangle 相交则返回 true,否则返回 false。如果它们的内部重叠,则据说它们相交。所以我知道为此我需要使用四个实例变量,我一直在使用(int x int y int height and int width)。我知道它必须通过做x + width 来确定它是否相交,如果这个值小于它对面的点,那么矩形是重叠的。我不知道怎么写。

【问题讨论】:

  • "I am trying to make this constructor: public Rectangle createIntersection(Rectangle r){ ...." -- 这不是构造函数。请澄清。
  • 这个函数永远不会返回。
  • 提示:先做数学。坐下,拿一支笔和一张纸。手动绘制矩形;看看你是否能计算出可能的交叉点的不同类别(如果我没有弄错,你只需要担心三种不同的情况)。所以,创建一个“算法”;以及当它在纸上起作用时;尝试将其放入代码中。当你不知道数学应该如何计算时,不要试图解决编程难题。你在学骑独轮车的时候也不会学杂耍,对吗?
  • 为什么? java.awt.Rectangle.createIntersection() 已经存在。

标签: java graphics2d


【解决方案1】:

该方法返回两个矩形的重叠区域,如果不重叠则返回null:

 public static Rectangle createIntersection(Rectangle r1, Rectangle r2) {

     // Left x
     int leftX = Math.max(r1.x, r2.x);

     // Right x
     int rightX = (int) Math.min(r1.getMaxX(), r2.getMaxX());

     // TopY
     int topY = Math.max(r1.y,r2.y);

     // Bottom y
     int botY =  (int) Math.min(r1.getMaxY(), r2.getMaxY());

     if ((rightX > leftX) && (botY > topY)) {
         return new Rectangle(leftX, topY, (rightX - leftX), (botY -topY));
     }

     return null;
 }

一些测试:

 public static void main(String [] args) {

        Rectangle r1 = new Rectangle(10,10,10,10);
        Rectangle r2 = new Rectangle(10,10,10,10);
        System.out.println(createIntersection(r1, r2));

        r1 = new Rectangle(10,10,10,10);
        r2 = new Rectangle(15,15,10,10);
        System.out.println(createIntersection(r1, r2));

        r1 = new Rectangle(20,20,10,10);
        r2 = new Rectangle(15,15,10,10);
        System.out.println(createIntersection(r1, r2));

        r1 = new Rectangle(15,30,10,10);
        r2 = new Rectangle(15,15,10,10);
        System.out.println(createIntersection(r1, r2));

        r1 = new Rectangle(15,30,10,10);
        r2 = new Rectangle(15,15,10,20);
        System.out.println(createIntersection(r1, r2));
 }

如果代码不清楚,请不要犹豫。

【讨论】:

  • 晚上好。我没有投反对票;但想了一会儿;我认为您的解决方案根本不正确。
  • 感谢您的反馈。你能说它有什么问题吗? (顺便说一句,如果您认为它应该被否决,请不要犹豫(0:)
  • 嗯。现在我仔细看看......不再那么确定了。我可能忽略了您正在调用 getMaxX(),但请使用它的最小值。可能你的算法是正确的。啊,随便。
  • 我确实运行(并发布)了一些额外的测试。希望OP能进一步测试并给出反馈。
【解决方案2】:

我猜你把事情搞混了。您在谈论构造函数;但是你在这里说得通;是矩形类的普通成员方法!

您要编码的第一件事是:

class Rectangle {
 ...
 public boolean intersects(Rectangle other) {
   // should return true if "this" and "other" rectangle "intersect"
   return intersectsOnX(other.x, other.width) && intersectsOnY(other.y, other.height);
 }

 private boolean intersectsOnX(int otherX, int otherWidth) {
   // check if other starts to the right of this
   if (this.x + width <= otherX) return false;
   // check if this starts to the left of other
   if (otherX + otherWidth <= this.x) return false;
   // then this / other must be overlapping 
   return true;
}

intersectsOnY() 以相同的方式工作(实际上它会使用完全相同的比较;因此您实际上希望避免那里的代码重复。

现在,有人可以调用 intersects() 来了解是否存在交集;如果该方法返回 true;您可以调用应该放入 Rectangle 类的其他方法:

public Rectangle getIntersection(Rectangle other) {

以上内容是您的起点——正如我所评论的:解决这个问题并不难;所以试试看吧。

【讨论】:

  • PO 可能考虑通过添加交集功能来扩展java.awt.Rectangle
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
相关资源
最近更新 更多