如何在使用几何形状的几何形状中围绕多个多边形绘制大纲。
因此,目前,我正在使用一系列的多边形,我想勾勒出一个轮廓并将其放入“ in”循环中,然后将其发送一个轮廓抽屉scipt。鳕鱼看起来像是如此:
func _ready():
var provs = [$Polygon2D4,$Polygon2D,$Polygon2D3]
var outline_drawer = $outline_drawer
outline_drawer.color = Color(0.06,0.71,0.74) / Color(2,2,2)
var merge := []
var merged_provs := []
for p in range(provs.size()):
var poly = provs[p]
var points = poly.global_transform.xform(poly.polygon)
merged_provs.append(points)
var outlines_local := []
var global_to_local: Transform2D = outline_drawer.global_transform.affine_inverse()
for outline_global in merged_provs:
outlines_local.append(global_to_local.xform(outline_global))
outline_drawer.outlines = outlines_local
这是轮廓抽屉的代码:
export(Color) var color = Color(0,0,0) setget set_color
export(float) var width = 1.5 setget set_width
var outlines = [] setget set_outlines
func _draw():
for outline in outlines:
for i in outline.size():
draw_line(outline[i-1] , outline[i], color, width)
func set_color(value):
color = value
update()
func set_width(value):
width = value
update()
func set_outlines(value):
outlines = value
update()
结果结果是这样的,所以我想知道是否有一种方法可以调整此代码以合并多边形,因此没有线路触摸的行盘旋的区域?所有的帮助都非常感谢! 结果的图片
So currently I'm taking the array of polygons I want to have an outline around and putting it in a "for in" loop, then sending it an outline drawer scipt. The cod looks like so:
func _ready():
var provs = [$Polygon2D4,$Polygon2D,$Polygon2D3]
var outline_drawer = $outline_drawer
outline_drawer.color = Color(0.06,0.71,0.74) / Color(2,2,2)
var merge := []
var merged_provs := []
for p in range(provs.size()):
var poly = provs[p]
var points = poly.global_transform.xform(poly.polygon)
merged_provs.append(points)
var outlines_local := []
var global_to_local: Transform2D = outline_drawer.global_transform.affine_inverse()
for outline_global in merged_provs:
outlines_local.append(global_to_local.xform(outline_global))
outline_drawer.outlines = outlines_local
And here is the code for the outline drawer:
export(Color) var color = Color(0,0,0) setget set_color
export(float) var width = 1.5 setget set_width
var outlines = [] setget set_outlines
func _draw():
for outline in outlines:
for i in outline.size():
draw_line(outline[i-1] , outline[i], color, width)
func set_color(value):
color = value
update()
func set_width(value):
width = value
update()
func set_outlines(value):
outlines = value
update()
The results turn out like this so I'm wondering if there is a way to adapt this code to merge the polygons so there is no line where they touch like in the circled area? All help is much appreciated!
Picture of the results
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法
geomy.merge_polygons_2d
将返回多边形的array
(每个多边形都是pool> poolvector2array
)。合并两个多边形可能不会导致多边形的原因。有两种情况要注意:
geome.merge_polygons_2d
将返回原始边界。孔是多边形,其点顺时针顺时针序。边界是多边形,其点逆时针排序。
要正确合并多个多边形,我们需要保持跟踪边界和孔。考虑到第三个多边形可能覆盖或部分覆盖一个孔,我们必须……
对于每个多边形:
当然,也存在第三个多边形在现有边界之间弥合差距的情况。我们也需要处理该案例。为此,我们需要检查它是否合并,我将通过检查结果中只有一个边界来做到这一点。
为了完成这项工作,我们需要从边界开始。因此,我们可以直接采用我们将合并的第一个……但是我将使用一个空的多边形。
再一次。
我们有:
对于每个多边形:
- 从孔组中取下孔。
- 用结果孔更换孔。
- 什么都不做(我们将多边形添加到末端的边界)。
嘿,这几乎看起来像源代码。我们要到达那里。
最大的变化是,我们将与循环的操作不同。因为:
我们在迭代时不应从集合中删除它们。因此,我们将添加到一个数组中要删除的元素,因此我们可以在末端删除它们。
同样,我们也不会完全替换孔。我们将删除旧的,然后添加新的。
另外,由于边界部分可以增加孔,因此,如果我们在做孔部分之前添加这些孔,我们会浪费努力。
。
在进行操作之前,我将多边形转换为全局坐标。
我们将使用
geome.is_polygon_clockwise
告诉结果中边界的孔。当然,我们不需要编写“什么都不做”的分支。
除了形式外,这与上述相同:
啊,对。我们应该画大纲。嗯...我想孔和边界都会有它们,对吗?这应该会这样做:
您可能拥有您认为处于同一行的事物,但实际上不是。而且我相信浮点错误会导致这一点。我希望这不是问题。
The method
Geometry.merge_polygons_2d
is going to return anArray
of polygons (each polygon is aPoolVector2Array
).The reason that merging two polygons might not result in a polygon. There are two cases to note:
Geometry.merge_polygons_2d
will return the original boundaries.The holes are polygons with their points ordered clockwise. The boundaries are polygons with their points ordered counterclockwise.
To merge multiple polygons properly, we need to keep track boundaries and holes. Considering that a third polygon might cover, or partially cover a hole, we have to…
For each polygon:
Of course, there is also the case in which the third polygon bridges the gap between existing boundaries. We need to handle that case too. To do so, we will need to check if it merged or not, which I'll do by checking that there is only one boundary in the result.
To make this work we need to start with a boundary. So we could directly take the first one we will merge… But I'll use an empty polygon.
One more time.
We have:
For each polygon:
- Remove the hole from the set of holes.
- Replace the hole with the result hole.
- Do nothing (we are adding the polygon to the boundaries at the end).
Hey, that almost looks like source code. We are getting there.
The big change is that we will be differing the operating past the loops. Because:
We should not remove from the sets while we are iterating over them. So we will instead add to an array the elements we want to remove, so we can remove them at the end.
Similarly, we will not exactly replace the hole either. We will remove the old one, and add the new one.
Also, since the boundaries part can add holes, we would be wasting effort comparing those holes if we add them before doing the holes part.
I'm converting the polygons to global coordinates before doing the operations.
We will be using
Geometry.is_polygon_clockwise
to tell holes from boundaries in the results.And of course, we don't need to write the "Do nothing" branches.
Aside form that, this is the same as above:
Ah, right. We should draw the outlines. Hmm… I guess both holes and boundaries would have them, right? This should do:
You may have things that you believe are on the same line, but in practice aren't. And I believe floating point errors can cause that. I hope that is not an issue.