[英]Rectangle intersections with Java AWT
I have two rectangles, and I want to figure out which side they are intersecting on.我有两个矩形,我想弄清楚它们在哪一侧相交。 I can use
Rectangle.intersects(Rectangle)
but it only tells me that the rectangles are intersecting.我可以使用
Rectangle.intersects(Rectangle)
但它只告诉我矩形是相交的。 What I am trying to do is check whether the rectangle is intersecting on the x or y axis, but I am not sure how to do this.我想做的是检查矩形是否在 x 轴或 y 轴上相交,但我不确定该怎么做。
I am suggesting just using some basic math to determine where the rectangles are intersecting.我建议只使用一些基本数学来确定矩形相交的位置。
A rectangle has a location (upper left corner) and a width and a height.矩形具有位置(左上角)以及宽度和高度。
R1
is at (10,20)
and R2
is at (40,50)
.R1
在(10,20)
和R2
在(40,50)
。 Since R1
is to the left and above R2
, then that means that if R2
is intersecting R1
, R2
must be intersecting on the right and/or below R1
.R1
在R2
的左侧和上方,这意味着如果R2
与R1
相交,则R2
必须在R1
的右侧和/或下方相交。R2
is not as wide as R1
, it may be intersecting only on the bottom of R1
.R2
2 不如R1
宽,则它可能仅在R1
的底部相交。R2
is not as high as R1
, it may be intersecting only on the right of R1
.R2
不如R1
高,则它可能仅在R1
的右侧相交。R1
and R2
are completely overlapping each other.R1
和R2
完全重叠。 So you will need to use the combination of the rectangles' locations and their sizes to determine exactly where the rectangles are intersecting.因此,您需要结合使用矩形的位置及其大小来确定矩形相交的确切位置。
The best thing to do is try and work this out on paper.最好的办法是尝试在纸上解决这个问题。 Then write your code and if you have any problems, post it and someone will assist.
然后编写您的代码,如果您有任何问题,请发布它,有人会提供帮助。 When posting your answer, just edit your original post with the changes.
发布您的答案时,只需使用更改来编辑您的原始帖子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.