Python 使用matplotlib对多边形进行三角剖分

Python 使用matplotlib对多边形进行三角剖分,python,matplotlib,2d,polygon,triangulation,Python,Matplotlib,2d,Polygon,Triangulation,我试着使用matplot.Delaunay对二维简单多边形进行三角剖分。。。这里的问题是我需要规则三角形。多边形是由numpy随机创建的,可能是Delaunay,这不是正确的方法 import matplotlib.delaunay as triang import pylab import numpy # 10 random points (x,y) in the plane x,y = numpy.array(numpy.random.standard_normal((2,10))) c

我试着使用matplot.Delaunay对二维简单多边形进行三角剖分。。。这里的问题是我需要规则三角形。多边形是由numpy随机创建的,可能是Delaunay,这不是正确的方法

import matplotlib.delaunay as triang
import pylab
import numpy

# 10 random points (x,y) in the plane
x,y =  numpy.array(numpy.random.standard_normal((2,10)))
cens,edg,tri,neig = triang.delaunay(x,y)

for t in tri:
 # t[0], t[1], t[2] are the points indexes of the triangle
 t_i = [t[0], t[1], t[2], t[0]]
 pylab.plot(x[t_i],y[t_i])


pylab.plot(x,y,'^')
pylab.show()

我想,对于一个随机多边形来说,用规则三角形进行三角剖分并不是一项简单的任务。但是,如果只想使用规则三角形,则必须手动定义点的坐标。如本例所示:

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

# Creating a Triangulation without specifying the triangles results in the
# Delaunay triangulation of the points.

# First create the x and y coordinates of the points.
n_angles = 36
n_radii = 8
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi/n_angles

x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()

# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
triang.set_mask(mask)

# Plot the triangulation.
plt.figure()
plt.gca().set_aspect('equal')
plt.triplot(triang, 'bo-')
plt.title('triplot of Delaunay triangulation')

plt.show()
导入matplotlib.pyplot作为plt
将matplotlib.tri导入为tri
将numpy作为np导入
输入数学
#在不指定三角形的情况下创建三角剖分将导致
#点的Delaunay三角剖分。
#首先创建点的x和y坐标。
n_角度=36
n_半径=8
最小半径=0.25
半径=np.linspace(最小半径,0.95,n_半径)
角度=np.linspace(0,2*math.pi,n_角度,端点=False)
角度=np.重复(角度[…,np.新轴],n_半径,轴=1)
角度[:,1::2]+=math.pi/n_角度
x=(半径*np.cos(角度)).flatten()
y=(半径*np.sin(角度)).flatte()
#创建三角剖分;没有三角形,因此创建了Delaunay三角剖分。
三角形=三角剖分(x,y)
#遮住不需要的三角形。
xmid=x[三角形].平均值(轴=1)
ymid=y[三角形].平均值(轴=1)
掩码=np,其中(xmid*xmid+ymid*ymid

我想,对于一个随机多边形来说,用规则三角形进行三角剖分并不是一项简单的任务。但是,如果只想使用规则三角形,则必须手动定义点的坐标。如本例所示:

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

# Creating a Triangulation without specifying the triangles results in the
# Delaunay triangulation of the points.

# First create the x and y coordinates of the points.
n_angles = 36
n_radii = 8
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi/n_angles

x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()

# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = tri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0)
triang.set_mask(mask)

# Plot the triangulation.
plt.figure()
plt.gca().set_aspect('equal')
plt.triplot(triang, 'bo-')
plt.title('triplot of Delaunay triangulation')

plt.show()
导入matplotlib.pyplot作为plt
将matplotlib.tri导入为tri
将numpy作为np导入
输入数学
#在不指定三角形的情况下创建三角剖分将导致
#点的Delaunay三角剖分。
#首先创建点的x和y坐标。
n_角度=36
n_半径=8
最小半径=0.25
半径=np.linspace(最小半径,0.95,n_半径)
角度=np.linspace(0,2*math.pi,n_角度,端点=False)
角度=np.重复(角度[…,np.新轴],n_半径,轴=1)
角度[:,1::2]+=math.pi/n_角度
x=(半径*np.cos(角度)).flatten()
y=(半径*np.sin(角度)).flatte()
#创建三角剖分;没有三角形,因此创建了Delaunay三角剖分。
三角形=三角剖分(x,y)
#遮住不需要的三角形。
xmid=x[三角形].平均值(轴=1)
ymid=y[三角形].平均值(轴=1)
掩码=np,其中(xmid*xmid+ymid*ymid

只是一个问题,“n_radi”做什么?
numpy.repeat(a,repeats,axis=None)
-重复
a
array
repeats
次:只是一个问题,“n_radi”做什么?
numpy.repeat(a,repeats,axis=None)
-重复
a
array
repeats
次: