精确检查两个具有双坐标的矩形是否相交

Check precisely if two rectangles with double coordinates intersect

我正在玩 hitboxes 并最终得到以下数据:

final double targetSmallestX = targetCenter.getX() - targetHalfWidth;
final double targetSmallestY = targetCenter.getY() - targetHalfHeight;
final double targetHighestX = targetCenter.getX() + targetHalfWidth;
final double targetHighestY = targetCenter.getY() + targetHalfHeight;
final double sourceSmallestX = sourceCenter.getX() - sourceHalfWidth;
final double sourceSmallestY = sourceCenter.getY() - sourceHalfHeight;
final double sourceHighestX = sourceCenter.getX() + sourceHalfWidth;
final double sourceHighestY = sourceCenter.getY() + sourceHalfHeight;

我想做的是检查给定的两个矩形 targetsource 是否相互交叉。可以这样做

Rectangle target = new Rectangle(targetSmallestX, target.width, targetSmallestY, target.height);
Rectangle source = new Rectangle(sourceSmallestX, source.width, sourceSmallestY, source.height);
target.intersect(source);

但这需要我使用整数。
对于这样一个看似简单的任务,我想出的所有算法似乎都太长太复杂了。有没有人对此有一个聪明的方法?

编辑:
当前方法

(targetSmallestX < sourceSmallestX + this.width)
&& (sourceSmallestX < (targetSmallestX + target.width))
&& (targetSmallestY < sourceSmallestY + this.height)
&& (sourceSmallestY < targetSmallestY + target.height);

此检查是否留下任何可能无法正常工作的星座?

在Java中,可以使用java.awt.geom.Rectangle2D表示浮点精度的坐标。准确的说,它里面的classjava.awt.geom.Rectangle2D.Double是用double来表示坐标的

Rectangle2D target = new Rectangle2D.Double(targetSmallestX, targetSmallestY, target.width, target.height);
Rectangle2D source = new Rectangle2D.Double(sourceSmallestX, sourceSmallestY, source.width, source.height);
target.intersect(source);