如何使用 GeneralPath 检查形状相交?爪哇

发布于 2024-12-18 05:15:47 字数 803 浏览 4 评论 0原文

我有一个程序可以让你移动各种形状。我希望能够返回一个布尔值,如果两个形状相交,该布尔值将返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2024-12-25 05:15:47

我第二次尝试的实际上是正确的答案。需要明确的是:

public boolean overlaps(OverlappableSceneShape s){
  Rectangle2D.Double otherShapeBoundary
     = new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight());
  return path.intersects(otherShapeBoundary);
}
  • 是确定相交的最佳方法。

What I tried second was actually the correct answer. Just to be clear:

public boolean overlaps(OverlappableSceneShape s){
  Rectangle2D.Double otherShapeBoundary
     = new Rectangle2D.Double(s.getX(), s.getY(), s.getWidth(), s.getHeight());
  return path.intersects(otherShapeBoundary);
}
  • is the best method of determining intersection.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文