本文介绍了使用左下点,宽度和高度找到两个矩形交集的矩形的Java方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了解决方案,但希望确保我的逻辑效率最高。我觉得有更好的方法。我有左下角的(x,y)坐标,2个矩形的高度和宽度,我需要返回第三个矩形,它们是它们的交点。我不想发布代码,因为我觉得它是作弊。

I have found the solution but wanted to ensure my logic is the most efficient. I feel that there is a better way. I have the (x,y) coordinate of the bottom left corner, height and width of 2 rectangles, and i need to return a third rectangle that is their intersection. I do not want to post the code as i feel it is cheating.


  1. 我找出哪个是最左边和最高的图表。

  2. 我检查一个是否与另一个完全重叠,然后反向查看另一个是否与X轴上的第一个完全重叠。

  3. 我检查X轴上的部分交叉。

  4. 我基本上重复了Y轴的第2步和第3步。

  5. 我做了一些数学并获得了基于这些条件的矩形。

  1. I figure out which is furthest left and highest on the graph.
  2. I check if one completely overlaps the other, and reverse to see if the other completely overlaps the first on the X axis.
  3. I check for partial intersection on the X axis.
  4. I basically repeat steps 2 and 3 for the Y axis.
  5. I do some math and get the points of the rectangle based on those conditions.

我可能会过度思考并编写效率低下的代码。我已经参加了一个工作计划,但我想找到最适合自己知识的方法。如果有人可以同意或指出我正确的方向,那就太棒了!

I may be over thinking this and writing inefficient code. I already turned in a working program but would like to find the best way for my own knowledge. If someone could either agree or point me in the right direction, that would be great!

推荐答案

为什么不使用JDK API来做这个给你?

Why not use JDK API to do this for you?

Rectangle rect1 = new Rectangle(100, 100, 200, 240);
Rectangle rect2 = new Rectangle(120, 80, 80, 120);
Rectangle intersection = rect1.intersection(rect2);

使用 java.awt.Rectangle class ,构造函数的参数是:x,y,width,height,其中x,y是矩形的左上角。您可以轻松地将左下角的点转换为左上角。

To use java.awt.Rectangle class, the parameters of the constructor are: x, y, width, height, in which x, y are the top-left corner of the rectangle. You can easily convert the bottom-left point to top-left.

我推荐以上内容,但如果你真的想要要自己动手,你可以按照以下步骤操作:

I recommend the above, but if you really want to do it yourself, you can follow the steps below:

(x1,y1),(x2,y2)分别是Rect1的左下角和右下角,
(x3,y3),(x4,y4)是Rect2的那些。

say (x1, y1), (x2, y2) are bottom-left and bottom-right corners of Rect1 respectively,(x3, y3), (x4, y4) are those of Rect2.


  • 找到较大的 x1 x3 x2 中较小的一个, x4 ,比如说 xL
    xR 分别为

    • 如果 xL> = xR ,然后返回没有其他交叉点

    • find the larger one of x1, x3 and the smaller one of x2, x4, say xL,xR respectively
      • if xL >= xR, then return no intersection else

      • if yT> = yB ,然后返回没有其他交叉点

      • return (xL, yB,xR-xL,yB-yT)

      • if yT >= yB, then return no intersection else
      • return (xL, yB, xR-xL, yB-yT).

      更像Java的伪代码:

      A more Java-like pseudo code:

      // Two rectangles, assume the class name is `Rect`
      Rect r1 = new Rect(x1, y2, w1, h1);
      Rect r2 = new Rect(x3, y4, w2, h2);
      
      // get the coordinates of other points needed later:
      int x2 = x1 + w1;
      int x4 = x3 + w2;
      int y1 = y2 - h1;
      int y3 = y4 - h2;
      
      // find intersection:
      int xL = Math.max(x1, x3);
      int xR = Math.min(x2, x4);
      if (xR <= xL)
          return null;
      else {
          int yT = Math.max(y1, y3);
          int yB = Math.min(y2, y4);
          if (yB <= yT)
              return null;
          else
              return new Rect(xL, yB, xR-xL, yB-yT);
      }
      

      如您所见,如果您的矩形最初是由两个对角线定义的,那么会更容易,你只需要做 //找到交叉点部分。

      As you see, if your rectangle was originally defined by two diagonal corners, it will be easier, you only need to do the // find intersection part.

      这篇关于使用左下点,宽度和高度找到两个矩形交集的矩形的Java方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:55