2013-08-25 113 views
0

我想用下面的函數在我的計劃:如何創建點對象列表?

def computeVoronoiDiagram(points): 
    """ Takes a list of point objects (which must have x and y fields). 
Returns a 3-tuple of: 

(1) a list of 2-tuples, which are the x,y coordinates of the 
Voronoi diagram vertices 
(2) a list of 3-tuples (a,b,c) which are the equations of the 
lines in the Voronoi diagram: a*x + b*y = c 
(3) a list of 3-tuples, (l, v1, v2) representing edges of the 
Voronoi diagram. l is the index of the line, v1 and v2 are 
the indices of the vetices at the end of the edge. If 
v1 or v2 is -1, the line extends to infinity. 
""" 
    siteList = SiteList(points) 
    context = Context() 
    voronoi(siteList,context) 
    return (context.vertices,context.lines,context.edges) 

它說拿點對象(其中有X & Y字段)的列表。它與Python列表數據結構不同嗎?我如何創建這樣的對象? 編輯:我應該提到的列表將包含約百萬個隨機點。

+0

訂購點被表示爲元組:'[(1,2),(3,4)]'。 – Blender

+0

@moooeeeep這是圖書館:https://github.com/rougier/gallery/blob/master/voronoi/voronoi/voronoi.py – rishiag

+1

我已經回答了[此其他圖書館]的解決方案(https:// svn .osgeo.org/qgis/branches/Release-1_6_0/python/plugins/fTools/tools/voronoi.py),這似乎是你的問題之一。爲何差異?這個來自github的不包括你發佈的代碼。 –

回答

1

事情是這樣的:

#!/usr/bin/python 

class Point: 
    def __init__(self, x, y): 
     self.x = x; 
     self.y = y; 

def main(): 
    pointslist = [Point(0, 0)] * 10 
    mytuple = computeVoronoiDiagram(pointslist) 

if __name__ == "__main__": 
    main() 

顯然你需要的代碼的其餘部分computeVoronoiDiagram()和支持代碼,以及聽起來像你想隨機各點的xy COORDS,而不是將它們全部設置爲0

+0

@moooeeeep:呃,是的,也許。編輯。 –

2

您使用的庫是否包含Point類?

如果不是:

from collections import namedtuple 
Point = namedtuple('Point', ['x','y'])