首页 > 解决方案 > 如何在 Quadtree [Python] 中读取和使用 CSV 文件中的坐标?

问题描述

我正在使用 python 构建四叉树,并设法为随机生成的点创建解决方案。主类是 QTree 类:

class QTree():
def __init__(self, treshold, customerCount):
    self.threshold = treshold
    self.points = [Point(random.uniform(0, 100), random.uniform(0, 100)) for x in range(customerCount)]
    self.root = Node(0, 0, 100, 100, self.points)

def add_point(x, y):
    self.points.append(Point(x, y))

def get_points(self):
    return self.points

def subdivide(self):
    recursive_subdivide(self.root, self.threshold)

def graph(self):
    fig = plt.figure(figsize=(12, 8))
    x = [point.x for point in self.points]
    y = [point.y for point in self.points]
    ax = fig.add_subplot(111)
    c = find_children(self.root)

    print("\n\nNumber of segments: %d" % len(c))
    areas = set()

    for el in c:
        areas.add(el.width * el.height)
    print("Minimum segment area: %.3f units" % min(areas))

    for n in c:
        ax.add_patch(patches.Rectangle((n.x0, n.y0), n.width, n.height, fill=False))

    plt.title("Quadtree")
    plt.plot(x, y, 'ro', markersize=3, color='b')
    plt.savefig('QuadtreeDiagram.png', dpi=1000)
    plt.show()
    return

我通过调用 Qtree 类和其他各种 def 函数来获取我的四叉树图( https://i.stack.imgur.com/ojYHO.png ):

def test(treshold, customerCount):
    qt = QTree(treshold, customerCount)
    qt.subdivide()
    qt.graph()


 # Tests
   test(1, 50)

我的问题是:如何更改随机点并使用我自己的带有坐标的 CSV 文件?谢谢 :D

标签: pythonalgorithmquadtree

解决方案


您可以添加一个新函数来从文件加载数据并设置点:

 def load_points():

    Df1 = pd.read_csv("D:\\test.txt", sep='\t' )
    self.points=[Point(Df1["Col0"],Df1["Col1"]) for x in range(len(Df1))]

注意1:您必须获得新数据的len与customerCount之间的关系注意2:我假设该文件例如:

Col0 Col1

1 2

2 4

3 5


推荐阅读