【问题标题】:Find all points below a line on a map查找地图上一条线以下的所有点
【发布时间】:2018-07-11 04:02:21
【问题描述】:

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

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()

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

我知道您可以使用cross product of vectors 来做到这一点

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

【问题讨论】:

  • 您的问题确实与matplotlib或绘图无关。您有一个点数组和一条由两个点定义的线。这是一个典型的计算几何问题。它可以单独使用 NumPy 解决(因为你使用它,无论如何)。
  • 我说这个问题取决于matplotlibplotting
  • 你做到了——用matplotlib标记它。
  • 它指的是 matplotib,因此是标签。在实际问题中,我使用了“上下文”这个词,它指的是 matplotlib。无论如何,很抱歉误导了您,先生。
  • 看看this 是否有帮助。它实际上可能是重复的......

标签: python matplotlib computational-geometry cross-product


【解决方案1】:

每个叉积运算仍然是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.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2020-06-20
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多