首页 > 解决方案 > 查找地图上一条线以下的所有点

问题描述

为了在有很多点(大约两千点)的地图上绘制两点之间的路径,我使用以下函数:

def path_between_cities(self, cities_with_coordinates, from_to):

        from matplotlib.lines import Line2D     

        # coordinates from chosen path
        x = [int(from_to[0][2]), int(from_to[1][2])]
        y = [int(from_to[0][1]), int(from_to[1][1])]

        #create line
        line = Line2D(x,y,linestyle='-',color='k')

        # create axis
        x_ = np.array((0,2000))
        y_ = np.array((0,6000))

        plt.plot(x_,y_, 'o')

        for item in cities_with_coordinates:
            name = item[0]
            y_coord = int(item[1])
            x_coord = int(item[2])
            plt.plot([x_coord], [y_coord], marker='o', markersize=1, color='blue')
            plt.axes().add_line(line)

        plt.axis('scaled')
        plt.show()

我的目标是提取画线下方的所有点(坐标)。

我知道你可以使用向量的叉积来做到这一点

给定大量向量,在上述上下文中实现这一目标的最有效方法是什么?

标签: pythonmatplotlibcomputational-geometrycross-product

解决方案


每个叉积运算仍然是O(1). 您可以对所有点运行以下函数并查看它们中的哪一个在下面,从而进行线性时间检查。

def ccw(a,b,c):
    """ Returns 1 if c is above directed line ab else returns -1"""
    return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y)
    #a and b are the vertices and c is the test point.

除非您有关于这些点的其他信息,否则您必须检查每个点以查看它是否低于特定线。


推荐阅读