- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下场景:给定 n 个 Polygon2D 节点,它们的行为类似于“阴影”(黑色,alpha 值减半),如何使用 merge_polygons_2d() 方法将所有这些节点组合成一个 Polygon2D 节点?
使用以下硬编码代码,我成功地将所有三个多边形合并为一个。但是我无法弄清楚如何通过迭代使过程自动化,以避免我的方法出现在例如要合并超过 10.. 20.. 多边形的情况下。
func Merge_Map_Shadows() -> void:
# Get all "shadow" polygons and store them in an array,
# then apply xform transform in order to retain it's position and rotation.
# After that, delete the original shadow.
if Map_Shadows.get_child_count() > 0:
var _detected_shadows: Array = []
for _shadow in Map_Shadows.get_children():
var _transformed_polygon: PoolVector2Array = []
for _vector in _shadow.polygon: _transformed_polygon.append(_shadow.transform.xform(_vector))
_detected_shadows.append(_transformed_polygon)
_shadow.call_deferred("queue_free")
# Create the "master shadow" node
var _master_shadow: Polygon2D = Polygon2D.new() ; _master_shadow.color = Color.black ; _master_shadow.color.a = 0.5
# Manually merge two "shadows" to the third and than apply the result to the master node
var _merged_shadow_1: Array = []
var _merged_shadow_2: Array = []
_merged_shadow_1 = Geometry.merge_polygons_2d(_detected_shadows[0], _detected_shadows[2])
_merged_shadow_2 = Geometry.merge_polygons_2d(_merged_shadow_1[0], _detected_shadows[1])
_master_shadow.set_polygon(_merged_shadow_2.front())
Map_Shadows.add_child(_master_shadow)
谢谢,迈克。
最佳答案
因为您要删除和添加同一节点的子节点。我会利用 child 的名单。
在我们开始之前,我们需要记住几件事:
Polygon2D
.Polygon2D
重叠。Polygon2D
节点可能应用了非身份转换。计划是就地合并多边形。我将列出需要删除的多边形。稍后会回来。
让我们从迭代子列表开始。我使用整数索引的原因稍后会有意义,所以我有:
for child_index in Map_Shadows.get_child_count():
pass
我们当然需要 child ,所以:
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
但我们需要确保它是一个 Polygon2D
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
var found_polygon:Polygon2D = child as Polygon2D
if found_polygon == null:
continue
此外,由于我们将从同一个列表中删除,我们需要考虑它可能正在排队等待删除:
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
var found_polygon:Polygon2D = child as Polygon2D
if found_polygon == null or found_polygon.is_queued_for_deletion():
continue
接下来我们需要检查 child 是否应用了非身份转换,如果有,则撤消它:
if found_polygon.transform != Transform2D.IDENTITY:
var transformed_polygon = found_polygon.transform.xform(found_polygon.polygon)
found_polygon.transform = Transform2D.IDENTITY
found_polygon.polygon = transformed_polygon
请注意,我们不需要遍历多边形的点。
现在我们将尝试将多边形与我们已经看到的所有多边形合并。为此,我们需要另一个循环。它看起来就像第一个,除了它上升到当前索引。 这就是我使用索引进行迭代的原因。
for child_subindex in child_index:
var other_child = Map_Shadows.get_child(child_subindex)
var other_found_polygon:Polygon2D = other_child as Polygon2D
if other_found_polygon == null or other_found_polygon.is_queued_for_deletion():
continue
现在我们尝试合并:
var merged_polygon = Geometry.merge_polygons_2d(found_polygon.polygon, other_found_polygon.polygon)
如果它们合并,我们得到一个包含单个项目(合并的多边形)的数组,如果不是这样,它们就不会合并。因此:
if merged_polygon.size() != 1:
continue
最后,当它们合并时,我们将删除当前的多边形(好吧,我们将把它放在一个数组中以便稍后删除它)并将另一个设置为合并后的多边形:
other_found_polygon.polygon = merged_polygon[0]
polygons_to_remove.append(found_polygon)
break
我在这里中断,因为我们已经找到一个多边形来合并当前多边形。无需继续寻找。
当然,单次遍历多边形可能不会完成所有合并。所以把整个东西放在一个 while(true)
里看起来像这样的循环:
var polygons_to_remove:Array
while(true):
polygons_to_remove = []
# the rest of the code here
if polygons_to_remove.size() == 0:
break
for polygon_to_remove in polygons_to_remove:
polygon_to_remove.queue_free()
正如我在开始时所说,我们保留了一个需要删除的多边形列表(好吧,一个数组)。
如果我们没有合并任何多边形,那么我们也不必删除任何多边形。这意味着我们完成了。这就是为什么我们 break
来自 while(true)
当要删除的多边形列表为空时循环。
当然,如果列表不为空,我们实际上需要删除那些节点。所以调用queue_free
在他们。不,我们不需要 call_deferred
,其实我们并不想要call_deferred
因为我们正在检查 is_queued_for_deletion
, 所以我们需要他们立即排队。
因为人们喜欢复制和粘贴,所以这是完整的相关代码:
var polygons_to_remove:Array
while(true):
polygons_to_remove = []
for child_index in Map_Shadows.get_child_count():
var child = Map_Shadows.get_child(child_index)
var found_polygon:Polygon2D = child as Polygon2D
if found_polygon == null or found_polygon.is_queued_for_deletion():
continue
if found_polygon.transform != Transform2D.IDENTITY:
var transformed_polygon = found_polygon.transform.xform(found_polygon.polygon)
found_polygon.transform = Transform2D.IDENTITY
found_polygon.polygon = transformed_polygon
for child_subindex in child_index:
var other_child = Map_Shadows.get_child(child_subindex)
var other_found_polygon:Polygon2D = other_child as Polygon2D
if other_found_polygon == null or other_found_polygon.is_queued_for_deletion():
continue
var merged_polygon = Geometry.merge_polygons_2d(found_polygon.polygon, other_found_polygon.polygon)
if merged_polygon.size() != 1:
continue
other_found_polygon.polygon = merged_polygon[0]
polygons_to_remove.append(found_polygon)
break
if polygons_to_remove.size() == 0:
break
for polygon_to_remove in polygons_to_remove:
polygon_to_remove.queue_free()
是的,我测试了那个东西。有用。但是,我假设这些都是表现良好的多边形。可能存在边缘情况。特别是我没有使用 Polygon2D
进行测试少于三个顶点,一个Polygon2D
与 invert_enable
, 或 Polygon2D
与 polygons
设置(参见 What does Polygon2D's polygons property do? )。
我还可以想到进一步的优化:我们只需要检查在先前 channel 中合并的多边形之间的进一步合并。如果一个多边形完成了一个 channel 而没有合并,这意味着它是孤立的,我们不需要继续检查那个。
关于geometry - (Godot 引擎)使用 Geometry 的 merge_polygons_2d() 方法合并两个以上的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67412060/
我正在尝试使用 SQLyog IDE 在 mySql 中执行复制表到不同的主机/数据库,并且在复制具有 2 个几何字段的表时遇到以下错误: Cannot get geometry object fro
我正在通过来自空间表的SQL查询创建一个新表: CREATE TABLE SomeShapes AS SELECT ash.id, ash.Geometry FROM AllShapes ash WH
考虑以下场景:给定 n 个 Polygon2D 节点,它们的行为类似于“阴影”(黑色,alpha 值减半),如何使用 merge_polygons_2d() 方法将所有这些节点组合成一个 Polygo
我最近升级到 MySQL 5.7,并试图从 5.6 master 运行复制。但是,复制失败并出现以下错误: Error 'Cannot get geometry object from data yo
我在 SQLite 数据库中存储了几何数据,列的数据类型是 BLOB。 我使用以下方法将 BLOB 数据转换为清晰的 map 几何图形 SharpMap.Converters.WellKnownBin
我有两个问题: 1.) 我刚刚找到了 boost 1.53 Polygon 实现(在 http://www.boost.org/doc/libs/1_53_0/libs/polygon 上)并且想测试
我正在尝试将一些 Geometry 加载和处理卸载到 web worker 中。要将其发送回主线程,需要对 Geometry 实例进行序列化,而且 Geometry.prototype.toJSON(
对于可定制的激光切割抽屉项目,我希望能够以编程方式在任意两个相交的垂直“2D”对象(具有宽度)之间创建连接。为此,我需要: 以某种标准格式加载和保存 2D 对象。 挤出二维对象,并对二维对象进行标
我试图找到多边形内部的线串部分。我尝试了 intersection 函数,但它似乎只是找到实际的交点,而不是与多边形重叠的线串部分。有没有办法得到这个对象? 这是一个演示情况: #include #
我正在尝试使用 NHibernate.Spatial.MySQL(版本 4.0.4.4001)创建一个简单的演示解决方案。解决方案可以在这里找到:https://github.com/andrerav
我有一些数据库使用 MySQL 中的 POINT 字段来存储几何数据。所有数据都以相同的方式添加: GeomFromText( 'POINT( lat lng )' ) 除了一个数据库外,所有数据库的
我有一个专栏让我对 MySQL Server 5.7.11 感到头疼。 它是 POINT 类型,我可以毫无问题地将其更改为 GEOMETRY 类型。 当我现在尝试将其从 GEOMETRY 更改为 PO
据我了解,您可以使用以下方法访问网格每个顶点的 uv 坐标(“纹素”): geometry.faceVertexUvs[ materialIndex ][ faceIndex ][ vertexInd
我很快就会遇到一个有趣的问题,我已经开始考虑算法了。我越想越害怕,因为我认为它会扩展得非常可怕 (O(n^4)),除非我能变聪明。我很难理解这个。这是问题的简化描述。 我有 N 个多边形(其中 N 可
给定二维平面中的 2 个点,这两个点内有多少个格点? 例如,对于 A (3, 3) 和 B (-1, -1),输出为 5。点是: (-1, -1), (0, 0), (1, 1), (2 , 2) 和
我有两个三角形,可以是任何大小。问题是,如何将坐标从一个三角形转移到另一个三角形?我知道坐标系中的两个三角形位置,是的,它们都在一个系统中。 基本上,我在triangle1中有点,我需要将它转移到tr
我有一些 线路 他们的交集描述了一个多边形,如下所示: 我知道线条的顺序,以及它们的方程。 为了找到内角,我找到了每条线的方向。但是我很困惑,因为减去两条线的方向会给出两个不同的角度,即使我是按照多边
线(x1,y1),(x2,y2)和(x3,y3),(x4,y4)是垂直的。我有点坐标 (x1, y1), (x2, y2), (x3, y3) 和线的长度 (x3, y3), (x4, y4)。我需要
在 N (~ 500) 维中,我希望找出最大的球体或矩形,使球体/矩形不包含现有的点。整个点集以轴对齐的矩形框为界(值的下限和上限)。 是否有任何已知的多项式时间方法/代码可以用来解决我的问题? 两个
我有以下代码: #include using namespace irr; using namespace core; using namespace scene; using namespace
我是一名优秀的程序员,十分优秀!