gpt4 book ai didi

Java AWT 矩形交集

转载 作者:行者123 更新时间:2023-11-29 05:23:08 24 4
gpt4 key购买 nike

我有两个矩形:

Rectangle rectZR = new Rectangle(513, 163, 38, 39); //(x,y,width,height)
Rectangle rectCorrect = new Rectangle(76, 206, 44, 44);

我这样做:

Rectangle intersect = rectZR.intersection(rectCorrect);

如坐标所示,这两个矩形实际上不相交,但 Java 仍然返回我一个交集并且 intersect 具有以下维度(x,y,宽度,高度):

513, 206, -393, -4

这是一个错误,还是我做错了什么?

最佳答案

JavaDoc关于相交方法状态:

Computes the intersection of this Rectangle with the specified Rectangle. Returns a new Rectangle that represents the intersection of the two rectangles. If the two rectangles do not intersect, the result will be an empty rectangle.

虽然整个类的 JavaDoc 指定:

A Rectangle whose width or height is negative has neither location nor dimension along those axes with negative dimensions. Such a Rectangle is treated as non-existant along those axes. Such a Rectangle is also empty with respect to containment calculations and methods which test if it contains or intersects a point or rectangle will always return false. Methods which combine such a Rectangle with a point or rectangle will ignore the Rectangle entirely in generating the result. If two Rectangle objects are combined and each has a negative dimension, the result will have at least one negative dimension.

因此,由两个负维度相交创建的矩形实际上意味着没有交集。您可以使用 intersects() 方法进行检查:

boolean intersects = rectZR.intersects(rectCorrect);

关于Java AWT 矩形交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23848592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com