如何使用GeneralPath检查形状相交?JAVA
我有一个程序,可以让你移动各种形状。我希望能够返回一个布尔值,如果两个形状相交,它将返回true。这就是我迄今为止所做的:如何使用GeneralPath检查形状相交?JAVA,java,graphics,awt,path-iterator,Java,Graphics,Awt,Path Iterator,我有一个程序,可以让你移动各种形状。我希望能够返回一个布尔值,如果两个形状相交,它将返回true。这就是我迄今为止所做的: public boolean overlaps(MyShape s){ Rectangle2D.Double otherShapeBoundary = new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight()); PathIterator pi = pat
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(除了MyShape之外,所有这些都直接来自API)
我不确定的一件事是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。我不确定它何时/为什么会返回真值,但很少返回。我第二次尝试的实际上是正确的答案。我想说清楚:
public boolean overlaps(OverlappableSceneShape s){
Rectangle2D.Double otherShapeBoundary
= new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight());
return path.intersects(otherShapeBoundary);
}
- 是确定交叉点的最佳方法