了解如何用最少数量的矢量线填充多边形
假设我有一个带孔的矢量多边形。我需要通过绘制连接的线段来填充它。当然,由于存在漏洞,我无法使用单个连续的折线来填充它:有时我需要中断我的路径,然后移动到被跳过的区域并在那里开始另一条折线。
我的目标是找到填充整个多边形所需的一组折线。如果我能找到最小集合(也就是说,我可以用最少的中断数填充多边形的方式),那就更好了。
额外问题:对于部分密度填充,我该如何做到这一点?比如说,我不想以 100% 的密度填充,但我想要 50% 的密度(这将要求填充线,假设它们彼此平行并且具有单一单位宽度,则放置在两个单位的距离处)。
我在这里找不到类似的问题,尽管有很多与洪水填充算法相关的问题。
有什么想法或指示吗?
更新:这张图片来自维基百科 显示了一个良好的假设洪水路径。我相信我可以使用位图来做到这一点。不过我有一个矢量多边形。我应该光栅化它吗?
Say I have a vector polygon with holes. I need to flood fill it by drawing connected segments. Of course, since there are holes, I can't fill it using a single continous polyline: I'll need to interrupt my path sometimes, then move to an area which was skipped and start another polyline there.
My goal is to find a set of polylines needed to fill the whole polygon. Better if I can find the smallest set (that is, the way I can fill the polygon with the minimum number of interruptions).
Bonus question: how could I do that for partial density fills? Say, I don't want to fill at 100% density but I want a 50% (this will require that fill lines, supposing they're parallel each other and have a single-unit width, are put at a distance of two units).
I couldn't find a similar question here, although there are many related to flood-fill algorithms.
Any ideas or pointers?
Update: this picture from Wikipedia shows a good hypotetical flood path. I believe I could do that using a bitmap. However I've got a vector polygon. Should I rasterize it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里假设线之间的距离是 1 个单位。
不保证找到最少数量的折线的粗略实现是:
将新线段合并到现有多段线的最佳算法应该很容易找到(在 y 上散列),或者强力算法可能就足够了:
添加注释 (1):为了涵盖多边形具有几乎垂直边缘的情况,合并过程不应仅查看 y-delta,而应允许在任何两个 y 范围重叠时进行合并(即表示折线 y 范围重叠线段 y 范围的末端)。
I'm assuming here that the distance between lines is 1 unit.
A crude implementation, with no guarantee to find the minimum number of polyline, is:
Optimal algorithm for merging new segments to existing polylines should be easy to find (hashing on y), or a brute force algorithm may suffice:
Added note (1): To cover the case where your polygon has nearly-vertical edges, the merge process should not look only at y-delta, but allow a merge if any two y range overlaps (that means end of polyline y-range overlap segment y-range).