我在网上看到 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)))
经过思考,我可以理解为什么领结与明星的待遇不同,但我有兴趣找到解决方法。