Numpy tmp)==1: “这个键是一个单独的键,不是三角形的一部分” 邻居=n_tmp 其他: 邻居=np.delete(n_tmp,np.where(n_tmp==bb[dmyi-1])[0]) 角度=np.mod(np.arctan2(xy[邻域,1]-xy

Numpy tmp)==1: “这个键是一个单独的键,不是三角形的一部分” 邻居=n_tmp 其他: 邻居=np.delete(n_tmp,np.where(n_tmp==bb[dmyi-1])[0]) 角度=np.mod(np.arctan2(xy[邻域,1]-xy,numpy,matplotlib,geometry,polygons,convex-polygon,Numpy,Matplotlib,Geometry,Polygons,Convex Polygon,tmp)==1: “这个键是一个单独的键,不是三角形的一部分” 邻居=n_tmp 其他: 邻居=np.delete(n_tmp,np.where(n_tmp==bb[dmyi-1])[0]) 角度=np.mod(np.arctan2(xy[邻域,1]-xy[nxt,1],xy[邻域,0]-xy[nxt,0]).ravel()\ -np.arctan2(xy[bb[dmyi-1],1]-xy[nxt,1],xy[bb[dmyi-1],0]-xy[nxt,0]).ravel(),2*np.pi) n

tmp)==1: “这个键是一个单独的键,不是三角形的一部分” 邻居=n_tmp 其他: 邻居=np.delete(n_tmp,np.where(n_tmp==bb[dmyi-1])[0]) 角度=np.mod(np.arctan2(xy[邻域,1]-xy[nxt,1],xy[邻域,0]-xy[nxt,0]).ravel()\ -np.arctan2(xy[bb[dmyi-1],1]-xy[nxt,1],xy[bb[dmyi-1],0]-xy[nxt,0]).ravel(),2*np.pi) nxt=相邻[角度==最大(角度)][0] bb.append(nxt) #现在将当前键标记为已使用-->注意键顺序的反转以匹配BL 该键=[bb[dmyi],bb[dmyi-1]] #获取已使用匹配的索引[bb[dmyi-1],nxt] 使用的标记=np。其中((BL==此键)。全部(轴=1)) 如果len(使用标记)>0: 已使用[mark_used,1]=真 dmyi+=1 多边形追加(bb) 其他: #所有债券都已入账 完成=正确 #检查多边形中是否存在重复(最多循环置换) #请注意,我们需要忽略每个多边形的最后一个元素(也是开始pt) keep=np.one(len(多边形),dtype=bool) 对于范围内的ii(透镜(多边形)): 多边形=多边形[ii] 对于多边形[ii+1:]中的p2: 如果是循环置换(polyg[:-1],p2[:-1]): 保留[ii]=假 多边形=[polygons[i]表示np中的i。其中(保留)[0]] #移除作为整个晶格边界的多边形(悬挂键除外) 边界=从(xy,NL,KL)中提取边界 打印“边界=”,边界 keep=np.one(len(多边形),dtype=bool) 对于范围内的ii(透镜(多边形)): 多边形=多边形[ii] if是循环置换(polyg[:-1],boundary.tolist()): 保留[ii]=假 elif是循环置换(polyg[:-1],边界[::-1]。tolist()): 保留[ii]=假 多边形=[polygons[i]表示np中的i。其中(保留)[0]] #准备多边形面片集合 PPC=[] 对于多边形中的多边形索引: pp=路径(xy[polyINDs],闭合=真) ppp=patches.PathPatch(pp,lw=2) 追加购买力平价(ppp) 返回多边形,PPC
您想要一个Voronoi图(Tesselation),该图在本网站上有许多示例,其实现可以使用纯python中的Fortune算法或借助github和Elsewhere上的其他代码示例获得。Dan,这不是我想要的。我已经有了一个tesselation,但是我的tesselation通常与任何voronoi图都不相关,因为它们不是三角剖分的对偶。我上面的例子是通过voronoi制作的,但在我的应用程序中通常不是这样的,
import numpy as np
xy = np.array([[2.72,-2.976], [2.182,-3.40207],
[-3.923,-3.463], [2.1130,4.5460], [2.3024,3.4900], [.96979,-.368],
[-2.632,3.7555], [-.5086,.06170], [.23409,-.6588], [.20225,-.9540],
[-.5267,-1.981], [-2.190,1.4710], [-4.341,3.2331], [-3.318,3.2654],
[.58510,4.1406], [.74331,2.9556], [.39622,3.6160], [-.8943,1.0643],
[-1.624,1.5259], [-1.414,3.5908], [-1.321,3.6770], [1.6148,1.0070],
[.76172,2.4627], [.76935,2.4838], [3.0322,-2.124], [1.9273,-.5527],
[-2.350,-.8412], [-3.053,-2.697], [-1.945,-2.795], [-1.905,-2.767],
[-1.904,-2.765], [-3.546,1.3208], [-2.513,1.3117], [-2.953,-.5855],
[-4.368,-.9650]])

BL= np.array([[22,23], [28,29], [8,9],
[12,31], [18,19], [31,32], [3,14],
[32,33], [24,25], [10,30], [15,23],
[5,25],  [12,13], [0,24],  [27,28],
[15,16], [5,8],   [0,1],   [11,18],
[2,27],  [11,13], [33,34], [26,33],
[29,30], [7,17],  [9,10],  [26,30],
[17,22], [5,21],  [19,20], [17,18],
[14,16], [7,26],  [21,22], [3,4],
[4,15],  [11,32], [6,19],  [6,13],
[16,20], [27,34], [7,8],   [1,9]])
def extract_polygons_lattice(xy, BL, NL, KL):
    ''' Extract polygons from a lattice of points.

    Parameters
    ----------
    xy : NP x 2 float array
        points living on vertices of dual to triangulation
    BL : Nbonds x 2 int array
        Each row is a bond and contains indices of connected points
    NL : NP x NN int array
        Neighbor list. The ith row has neighbors of the ith particle, padded with zeros
    KL : NP x NN int array
        Connectivity list. The ith row has ones where ith particle is connected to NL[i,j]

    Returns
    ----------
    polygons : list
        list of lists of indices of each polygon
    PPC : list
        list of patches for patch collection
    '''
    NP = len(xy)
    NN = np.shape(KL)[1]
    # Remove dangling bonds
    # dangling bonds have one particle with only one neighbor
    finished_dangles = False
    while not finished_dangles:
        dangles = np.where([ np.count_nonzero(row)==1 for row in KL])[0]
        if len(dangles) >0:
            # Make sorted bond list of dangling bonds
            dpair = np.sort(np.array([ [d0, NL[d0,np.where(KL[d0]!=0)[0]] ]  for d0 in dangles ]), axis=1)
            # Remove those bonds from BL
            BL = setdiff2d(BL,dpair.astype(BL.dtype))
            print 'dpair = ', dpair
            print 'ending BL = ', BL
            NL, KL = BL2NLandKL(BL,NP=NP,NN=NN)
        else:
            finished_dangles = True


    # bond markers for counterclockwise, clockwise
    used = np.zeros((len(BL),2), dtype = bool)
    polygons = []
    finished = False

    while (not finished) and len(polygons)<20:
        # Check if all bond markers are used in order A-->B
        todoAB = np.where(~used[:,0])[0]
        if len(todoAB) > 0:
            bond = BL[todoAB[0]]

            # bb will be list of polygon indices
            # Start with orientation going from bond[0] to bond[1]
            nxt = bond[1]
            bb = [ bond[0], nxt ]
            dmyi = 1

            # as long as we haven't completed the full outer polygon, add next index
            while nxt != bond[0]:
                n_tmp = NL[ nxt, np.argwhere(KL[nxt]).ravel()]
                # Exclude previous boundary particle from the neighbors array, unless its the only one
                # (It cannot be the only one, if we removed dangling bonds)
                if len(n_tmp) == 1:
                    '''The bond is a lone bond, not part of a triangle.'''
                    neighbors = n_tmp
                else:
                    neighbors = np.delete(n_tmp, np.where(n_tmp == bb[dmyi-1])[0])

                angles = np.mod( np.arctan2(xy[neighbors,1]-xy[nxt,1],xy[neighbors,0]-xy[nxt,0]).ravel() \
                        - np.arctan2( xy[bb[dmyi-1],1]-xy[nxt,1], xy[bb[dmyi-1],0]-xy[nxt,0] ).ravel(), 2*np.pi)
                nxt = neighbors[angles == max(angles)][0]
                bb.append( nxt )


                # Now mark the current bond as used
                thisbond = [bb[dmyi-1], bb[dmyi]]
                # Get index of used matching thisbond
                mark_used = np.where((BL == thisbond).all(axis=1))
                if len(mark_used)>0:
                    #print 'marking bond [', thisbond, '] as used'
                    used[mark_used,0] = True
                else:
                    # Used this bond in reverse order
                    used[mark_used,1] = True

                dmyi += 1

            polygons.append(bb) 

        else:
            # Check for remaining bonds unused in reverse order (B-->A)
            todoBA = np.where(~used[:,1])[0]
            if len(todoBA) >0:
                bond = BL[todoBA[0]]

                # bb will be list of polygon indices
                # Start with orientation going from bond[0] to bond[1]
                nxt = bond[0]
                bb = [ bond[1], nxt ]
                dmyi = 1

                # as long as we haven't completed the full outer polygon, add nextIND
                while nxt != bond[1]:
                    n_tmp = NL[ nxt, np.argwhere(KL[nxt]).ravel()]
                    # Exclude previous boundary particle from the neighbors array, unless its the only one
                    # (It cannot be the only one, if we removed dangling bonds)
                    if len(n_tmp) == 1:
                        '''The bond is a lone bond, not part of a triangle.'''
                        neighbors = n_tmp
                    else:
                        neighbors = np.delete(n_tmp, np.where(n_tmp == bb[dmyi-1])[0])

                    angles = np.mod( np.arctan2(xy[neighbors,1]-xy[nxt,1],xy[neighbors,0]-xy[nxt,0]).ravel() \
                            - np.arctan2( xy[bb[dmyi-1],1]-xy[nxt,1], xy[bb[dmyi-1],0]-xy[nxt,0] ).ravel(), 2*np.pi)
                    nxt = neighbors[angles == max(angles)][0]
                    bb.append( nxt )


                    # Now mark the current bond as used --> note the inversion of the bond order to match BL
                    thisbond = [bb[dmyi], bb[dmyi-1]]
                    # Get index of used matching [bb[dmyi-1],nxt]
                    mark_used = np.where((BL == thisbond).all(axis=1))
                    if len(mark_used)>0:
                        used[mark_used,1] = True

                    dmyi += 1

                polygons.append(bb) 
            else:
                # All bonds have been accounted for
                finished = True


    # Check for duplicates (up to cyclic permutations) in polygons
    # Note that we need to ignore the last element of each polygon (which is also starting pt) 
    keep = np.ones(len(polygons),dtype=bool)
    for ii in range(len(polygons)):
        polyg = polygons[ii]
        for p2 in polygons[ii+1:]:
            if is_cyclic_permutation(polyg[:-1],p2[:-1]):
                keep[ii] = False

    polygons = [polygons[i] for i in np.where(keep)[0]]

    # Remove the polygon which is the entire lattice boundary, except dangling bonds
    boundary = extract_boundary_from_NL(xy,NL,KL)
    print 'boundary = ', boundary
    keep = np.ones(len(polygons),dtype=bool)
    for ii in range(len(polygons)):
        polyg = polygons[ii]
        if is_cyclic_permutation(polyg[:-1],boundary.tolist()):
            keep[ii] = False
        elif is_cyclic_permutation(polyg[:-1],boundary[::-1].tolist()):
            keep[ii] = False

    polygons = [polygons[i] for i in np.where(keep)[0]]


    # Prepare a polygon patch collection
    PPC = []  
    for polyINDs in polygons:
        pp = Path(xy[polyINDs],closed=True)
        ppp = patches.PathPatch(pp, lw=2)
        PPC.append(ppp)


    return polygons, PPC