gpt4 book ai didi

python - 匀称的 polygon_.buffer(0) "loses"我领结的一半

转载 作者:行者123 更新时间:2023-11-28 18:45:26 25 4
gpt4 key购买 nike

我在网上看到 buffer(0) 应该“修复”领结。 Shapely 找到领结的交点,但只保留右上角的部分。为了寻找解决方法,我尝试颠倒我的观点顺序。令我惊讶的是,领结右上角的部分仍然保留着。我不明白。任何帮助表示赞赏。

我想将整个领结保留为两个三角形(或一个六边形——两者都有用)。寻找解决此“问题”的方法。

#!/usr/bin/env python3

from shapely.geometry.polygon import Polygon

bowtie_plot = [(1, 0), (0, 1), (0, -1), (-1, 0)]

bowties = [
Polygon(bowtie_plot),
Polygon(bowtie_plot[::-1])
]

cleaned = [
bowties[0].buffer(0),
bowties[1].buffer(0)
]

print('cleaned[0] exterior = {}'.format(list(cleaned[0].exterior.coords)))
# cleaned[0] exterior = [(0.0, 0.0), (-1.0, 1.0), (1.0, 1.0), (0.0, 0.0)]

print('cleaned[1] exterior = {}'.format(list(cleaned[1].exterior.coords)))
# cleaned[1] exterior = [(0.0, 0.0), (-1.0, 1.0), (1.0, 1.0), (0.0, 0.0)]

# ADDITIONAL INFORMATION BELOW
# here's what shapely *can* do with intersecting lines:
# a star shape made of five intersecting lines and five points

from math import sin, cos, pi

star = Polygon(
[(cos(x*pi*4/5), sin(x*pi*4/5)) for x in range(5)]
).buffer(0)

# after buffering, becomes a star shape made out of ten lines and ten points
# shapely found all intersections and corrected the polygon.
print('list exterior = {}'.format(list(star.exterior.coords)))

经过思考,我可以理解为什么领结与星星的待遇不同,但我有兴趣找到解决方法。

最佳答案

你的领结不是一个有效的形状 Polygon .阅读该文档,以及 LinearRing 的文档(就在 Polygon 文档的上方)。特别要注意有效和无效 LinearRing 的示例。

如果你像这样创建领结:

In [46]: bt = [(1,0), (0,1), (0,0), (-1,0), (0, -1), (0,0)]

In [47]: poly = Polygon(bt)

然后 buffer(0) 返回 MultiPolygon :

In [48]: poly.buffer(0)
Out[48]: <shapely.geometry.multipolygon.MultiPolygon at 0x4a40050>

关于python - 匀称的 polygon_.buffer(0) "loses"我领结的一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21058314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com