【问题标题】:How to check for Shape intersection using GeneralPath? Java如何使用 GeneralPath 检查形状相交?爪哇
【发布时间】:2011-11-28 00:15:58
【问题描述】:

我有一个程序可以让你移动各种形状。如果两个形状相交,我希望能够返回一个返回 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。我不确定它何时/为什么返回 true,但这种情况很少见。

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • ^^Kk。我会努力去做的

标签: java graphics awt path-iterator


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-11-21
    • 2021-03-31
    • 2015-11-15
    • 2012-06-19
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    相关资源
    最近更新 更多