context.triangles
どのドローネ三角形の入力点が参加するかを示します。各三角形はボロノイ頂点に関連付けられています。三角形と頂点はtriangles
、vertices
配列と並列に格納されます。
指定された点のボロノイ セルを見つけるp
には、入力点が使用されているすべての頂点 (三角形) を見つける必要があります。そして、これらの頂点がedges
配列によってどのように接続されているかを見つけます。
これを行うための簡単な (十分にテストされていない) コードを次に示します。
from voronoi import voronoi
import random
from collections import defaultdict
num_points = 50
points = [(random.uniform(0,10), random.uniform(0,10)) for i in xrange(num_points)]
c = voronoi(points)
# For each point find triangles (vertices) of a cell
point_in_triangles = defaultdict(set)
for t_ind, ps in enumerate(c.triangles):
for p in ps:
point_in_triangles[p].add(t_ind)
# Vertex connectivity graph
vertex_graph = defaultdict(set)
for e_ind, (_, r, l) in enumerate(c.edges):
vertex_graph[r].add(l)
vertex_graph[l].add(r)
def cell(point):
if point not in point_in_triangles:
return None
vertices = set(point_in_triangles[point]) # copy
v_cell = [vertices.pop()]
vertices.add(-1) # Simulate infinity :-)
while vertices:
neighbours = vertex_graph[v_cell[-1]] & vertices
if not neighbours:
break
v_cell.append(neighbours.pop())
vertices.discard(v_cell[-1])
return v_cell
for p in xrange(num_points):
print p, cell(p)