如何使用 GeneralPath 检查形状相交?爪哇
我有一个程序可以让你移动各种形状。我希望能够返回一个布尔值,如果两个形状相交,该布尔值将返回 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);
}
...其中路径是 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。我不确定它何时/为何返回 true,但这种情况非常罕见。
I have a program that allows you to move various Shapes about. I want to be able to return a boolean that returns true if two shapes are intersecting. This is what I have thus far:
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);
}
...where path is a GeneralPath (these are all straight from the API, except MyShape).
One thing I'm unsure on is how PathIterator works, which might be the problem. I've also tried this, but I was getting a similar error:
public boolean overlaps(OverlappableSceneShape s){
Rectangle2D.Double otherShapeBoundary
= new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight());
return path.intersects(otherShapeBoundary);
}
The error is that this method almost always returns false. I'm unsure when/why it is returning true, but it is very infrequent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我第二次尝试的实际上是正确的答案。需要明确的是:
What I tried second was actually the correct answer. Just to be clear: