Dou*_*g B 12 java logic intersection
我找到了解决方案,但希望确保我的逻辑是最有效的.我觉得有更好的方法.我有左下角的(x,y)坐标,2个矩形的高度和宽度,我需要返回第三个矩形,它们是它们的交点.我不想发布代码,因为我觉得它是作弊.
我可能会过度思考并编写效率低下的代码.我已经参加了一个工作计划,但我想找到最适合自己知识的方法.如果有人可以同意或指出我正确的方向,那将是伟大的!
shu*_*why 17
为什么不使用JDK API为您执行此操作?
Rectangle rect1 = new Rectangle(100, 100, 200, 240);
Rectangle rect2 = new Rectangle(120, 80, 80, 120);
Rectangle intersection = rect1.intersection(rect2);
Run Code Online (Sandbox Code Playgroud)
要使用java.awt.Rectangle
类,构造函数的参数是:x,y,width,height,其中x,y是矩形的左上角.您可以轻松地将左下角点转换为左上角.
我推荐以上内容,但如果您真的想自己动手,可以按照以下步骤操作:
比如(x1, y1), (x2, y2)
分别(x3, y3), (x4, y4)
是Rect1的左下角和右下角,
是Rect2的角.
x1
,x3
和中较小的一个x2
,x4
比方说xL
,
xR
分别
xL >= xR
,然后返回没有其他交集y1
,y3
和中较小的一个y2
,y4
比方说yT
,
yB
分别
yT >= yB
,然后返回没有其他交集(xL, yB, xR-xL, yB-yT)
.一个更像Java的伪代码:
// 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);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,如果您的矩形最初由两个对角线定义,那么它将更容易,您只需要执行该// find intersection
部分.
Wil*_*son 12
接受的答案是不正确的.这是我的版本,这是正确的.
不要使用接受的答案.
//returns true when intersection is found, false otherwise.
//when returning true, rectangle 'out' holds the intersection of r1 and r2.
private static boolean intersection2(Rectangle r1, Rectangle r2,
Rectangle out) {
float xmin = Math.max(r1.x, r2.x);
float xmax1 = r1.x + r1.width;
float xmax2 = r2.x + r2.width;
float xmax = Math.min(xmax1, xmax2);
if (xmax > xmin) {
float ymin = Math.max(r1.y, r2.y);
float ymax1 = r1.y + r1.height;
float ymax2 = r2.y + r2.height;
float ymax = Math.min(ymax1, ymax2);
if (ymax > ymin) {
out.x = xmin;
out.y = ymin;
out.width = xmax - xmin;
out.height = ymax - ymin;
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您还可以使用 Rectangle 源代码与您自己的算法进行比较:
/**
* Computes the intersection of this <code>Rectangle</code> with the
* specified <code>Rectangle</code>. Returns a new <code>Rectangle</code>
* that represents the intersection of the two rectangles.
* If the two rectangles do not intersect, the result will be
* an empty rectangle.
*
* @param r the specified <code>Rectangle</code>
* @return the largest <code>Rectangle</code> contained in both the
* specified <code>Rectangle</code> and in
* this <code>Rectangle</code>; or if the rectangles
* do not intersect, an empty rectangle.
*/
public Rectangle intersection(Rectangle r) {
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);
}
Run Code Online (Sandbox Code Playgroud)