2016-05-29 143 views
0

我試着用matplot.Delaunay對2D中的一個簡單多邊形進行三角剖分......這裏的問題是我需要規則的三角形。多邊形是由numpy隨機創建的,也許Delaunay不是要走的路。使用matplotlib對多邊形進行三角剖分

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() 

回答

1

我猜測,對於隨機多邊形而言,用正三角形進行三角測量並不是一件容易的事情。但是,如果只想使用常規三角形,則必須手動定義點的座標。就像這個例子:

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() 

enter image description here

+0

只是一個問題,什麼是 'n_radii' 呢? – PrintName

+0

'numpy.repeat(a,repeatats,axis = None)' - 'a'數組的重複元素'重複次數:http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/ numpy.repeat.html – Serenity