我在玩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;
我想要做的是检查,如果两个给出的矩形target
,并source
相互交叉。可以这样做
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
which 表示具有浮点精度的坐标。准确地说,它的内部类java.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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句