在 Java 2D 中创建复合形状

发布于 2024-08-18 04:32:53 字数 870 浏览 4 评论 0原文

我使用 Java 2D 将几条贝塞尔曲线 (CubicCurve2D) 修补在一起以创建一个“blob”。我现在面临的问题是如何:

  1. 用给定的颜色有效地填充斑点。
  2. 有效地确定给定点是否位于斑点内部。

我注意到CubicCurve2D实现了Shape,它提供了许多contains方法来确定“内部”,并且Graphics2D能够通过 fill(Shape) 填充 Shape (我相信使用 ShapegetPathIterator 方法来执行此操作)。

鉴于此,我希望可以创建一个复合 Shape,其中我的 getPathIterator(AffineTransform) 方法可以简单地将底层 PathIterator 链接在一起。但是,一旦我的形状包含多个 CubicCurve2D,就会产生 NoSuchElementException。即使我确实设法实现了这一点,我也不相信它会按预期工作,因为 CubicCurve2D 总是在凸侧填充,而我的“斑点”由凹曲线和凸曲线组成。 “包含”问题更加困难,因为点可以合法地位于斑点内,但不能位于任何单独的曲线内。

  • 我是否以正确的方式解决这个问题(尝试实现Shape?)或者是否有一种我不知道的惯用方法?我本以为合成几何形状的问题会相当普遍。
  • 有人对如何解决这个问题有任何建议吗?

提前致谢。

Using Java 2D I've patched several Bezier curves (CubicCurve2D) together to create a "blob". The problem I now face is how to:

  1. Efficiently fill the blob with a given colour.
  2. Efficiently determine whether a given point lies inside the blob.

I noticed thst CubicCurve2D implements Shape which provides numerous contains methods for determining "insideness" and that Graphics2D is able to fill a Shape via the fill(Shape) (which I believe uses Shape's getPathIterator methods to do this).

Given this I was hoping I could create a composite Shape, whereby my getPathIterator(AffineTransform) method would simply link the underlying PathIterators together. However, this is producing a NoSuchElementException once my shape contains more than one CubicCurve2D. Even if I do manage to achieve this I'm not convinced it will work as expected because a CubicCurve2D is always filled on the convex side, and my "blob" is composed of concave and convex curves. The "contains" problem is even harder as a point can legitimately lie within the blob but not within any of the individual curves.

  • Am I approaching this problem in the correct way (trying to implement Shape?) or is there an idiomatic way to do this that I'm unaware of? I would have thought that the problem of compositing geometric shapes would be fairly common.
  • Does anyone have any suggestions regarding how to solve this problem?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

走过海棠暮 2024-08-25 04:32:53

我不确定我是否理解您的问题,但是可以使用类 java/awt/geom/Area.

I'm not sure I understand your question but composite shapes can be created with the class java/awt/geom/Area.

屋檐 2024-08-25 04:32:53

寻找解决方案是解决这个问题的正确方法。如果您有一组曲线想要组装成一个形状,我建议您使用 GeneralPath。只需根据需要添加曲线或直线段即可。查看界面以查看各种附加方法。另请注意,您可以通过将最后一个点连接到起点来“完成”形状。

一旦路径关闭,有许多不同版本的 contains() 可以使用,请花时间阅读它们的每个描述,因为在速度和准确性方面存在权衡,具体取决于您的应用程序。

而且很容易从路径中获取形状,并对它进行填充、变换等。

Looking to Shape for a solution is the right way to go about this. If you have a collection of curves that you are trying to assemble into a shape, I would suggest that you use a GeneralPath. Simply add your curves, or straight line segments, as required. Look to the interface to see the various append methods. Also note that you can 'complete' the shape by joining the last point to the starting point.

Once the path is closed, there are a number of different versions of contains() that can be used, please take the time to read each of their descriptions, as there are trade-offs in terms of speed and accuracy, depends on your application.

Also it is easy to get a shape from the path, and fill it, transform it, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文