shapely&非结构化网格:随机生成点产生三角形并融合

简单弄了稀疏化非结构网格的算法,再准备产生结构化网格点进行插值的时候碰要得到网格边界的问题,用shapely.union解决,下面用随机生成点进行示例。

import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import MultiPoint
from shapely.ops import triangulate
from descartes.patch import PolygonPatch
from functools import reduce

points_num = 15
x = np.random.rand(points_num)
y = np.random.rand(points_num)
points = MultiPoint(list(zip(x, y)))
triangles = triangulate(points)
plt.figure()
for triangle in triangles:
    patch = PolygonPatch(triangle, alpha=0.5, zorder=2)
    plt.gca().add_patch(patch)
for point in points:
    plt.plot(point.x, point.y, 'o', color='grey')
plt.xlim((0,1))
plt.ylim((0,1))
    
poly = reduce(lambda x, y: x.union(y), triangles)
plt.figure()
plt.gca().add_patch(PolygonPatch(poly, alpha=0.2))
outline = poly.boundary.coords.xy
plt.plot(*outline, 'r')
plt.xlim((0,1))
plt.ylim((0,1))

发表评论

电子邮件地址不会被公开。