我有一个程序,允许您移动各种形状。我希望能够返回一个布尔值,如果两个形状相交,则返回true。这就是我到目前为止所做的:
public boolean overlaps(MyShape s){
Rectangle2D.Double otherShapeBoundary
= new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight());
PathIterator pi = path.getPathIterator(null);
return path.intersects(pi,otherShapeBoundary);
}
...其中path是一个GeneralPath(这些都是直接来自API,MyShape除外)。
我不确定的一件事是PathIterator是如何工作的,这可能是问题所在。我也尝试过这个,但是我遇到了类似的错误:
public boolean overlaps(OverlappableSceneShape s){
Rectangle2D.Double otherShapeBoundary
= new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight());
return path.intersects(otherShapeBoundary);
}
错误是此方法几乎总是返回false。我不确定何时/为什么它返回真实,但它很少见。
答案 0 :(得分:2)
我尝试的第二个实际上是正确答案。只是要明确:
public boolean overlaps(OverlappableSceneShape s){
Rectangle2D.Double otherShapeBoundary
= new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight());
return path.intersects(otherShapeBoundary);
}