2016-05-29 17 views
0

私は、matplot.Delaunayを2次元の単純ポリゴンの三角形分割に使用しようとしました...ここでの問題は、正三角形が必要なことです。多角形は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、繰り返し、軸=なし)' - 'a'配列の繰り返し要素が'繰り返す ':http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/ numpy.repeat.html – Serenity

関連する問題