简体   繁体   English

如何计算python中两点之间连线的坐标?

[英]How to calculate the coordinates of the line between two points in python?

How can I get all the coordinate points between two points in python?如何获取python中两点之间的所有坐标点? For example: I have a point with the coordinates of x1, y1 and an other with x10, y10.例如:我有一个坐标为 x1、y1 的点和另一个坐标为 x10、y10 的点。 I need all the points between them (in this case for instance x2, y2 ... x9, y9).我需要它们之间的所有点(在这种情况下,例如 x2, y2 ... x9, y9)。 Huge thanks for your help!非常感谢您的帮助!

"All of them"? “他们都”? There are an infinite number.有无穷多个。

You can calculate the slope and intercept of the line between those two points .您可以计算这两个点之间的线的斜率和截距 Knowing those you can calculate the value for y at every value of x you wish using the equation for the line.知道这些后,您可以使用直线方程计算您希望的每个 x 值处的 y 值。

This is high school algebra.这是高中代数。 What's the problem?有什么问题?

Given two points (x1, y1) and (x2, y2) the equation for the line between them is:给定两个点(x1, y1)(x2, y2) ,它们之间的直线方程为:

y = m*x + b

where在哪里

m = slope = (y1-y2)/(x1-x2)

and

b = y-intercept = (x1*y2 - x2*y1)/(x1-x2)

If you mean "draw the circle passing between the two points and find all the points inside", I'd calculate the center point as the midpoint of that line and radius equal to half the length of that line.如果您的意思是“绘制经过两点之间的圆并找到里面的所有点”,我会将中心点计算为该线的中点,半径等于该线长度的一半。 You calculate whether or not a point is inside or outside the circle by determining the distance from the center and comparing it to the radius.您可以通过确定到圆心的距离并将其与半径进行比较来计算点是在圆内还是圆外。

There are an infinite numbers of points both inside and outside the circle.圆内外都有无数个点。 What are you really trying to do here?你真的想在这里做什么?

Seems you want to generate a list of integer points for the line segment between given points.似乎您想为给定点之间的线段生成整数点列表。 This problem is solved in computer graphics, for example, using Bresenham algorithm or DDA algo这个问题在计算机图形学中得到解决,例如使用Bresenham 算法DDA 算法

def intermediates(p1, p2, nb_points=8):
    """"Return a list of nb_points equally spaced points
    between p1 and p2"""
    # If we have 8 intermediate points, we have 8+1=9 spaces
    # between p1 and p2
    x_spacing = (p2[0] - p1[0]) / (nb_points + 1)
    y_spacing = (p2[1] - p1[1]) / (nb_points + 1)

    return [[p1[0] + i * x_spacing, p1[1] +  i * y_spacing] 
            for i in range(1, nb_points+1)]

print(intermediates([1, 2], [10, 6.5], nb_points=8))

# [[2.0, 2.5], [3.0, 3.0], [4.0, 3.5], [5.0, 4.0], 
#  [6.0, 4.5], [7.0, 5.0], [8.0, 5.5], [9.0, 6.0]]

Elaborating on the answer provided by Thierry Lathuille, you could try to make a vectorized version as the code below.详细说明 Thierry Lathuille 提供的答案,您可以尝试制作一个矢量化版本,如下面的代码。 This code was written to insert extra points in a wkt geometry that is why it has the z_v points, with two options, either insert a fix quantity of points (n_p) between existing coordinates points or insert points at a given fixed distance (d) from each other between existing coordinates points .编写此代码是为了在 wkt 几何中插入额外的点,这就是为什么它具有 z_v 点,有两个选项,在现有坐标点之间插入固定数量的点 (n_p) 或在给定的固定距离 (d) 处插入点现有坐标点之间相互远离。

Also, "BORRAR - REVISION GRAFICA DE LOS PUNTOS" it is just a way to see the code is working properly in a visual way, comment this out once you are sure your point placement works.此外,“BORRAR - REVISION GRAFICA DE LOS PUNTOS”只是一种以可视方式查看代码是否正常工作的方法,一旦您确定您的点放置有效,请将此注释掉。

v range function was found here in stackoverflow but cannot seem to find the post to refer, I just modified it a bit. v range 函数在 stackoverflow 中找到,但似乎找不到要引用的帖子,我只是对其进行了一些修改。

On the same WKT geometry my machine gets a ~25X time reduction.在相同的 WKT 几何结构上,我的机器减少了大约 25 倍的时间。

 def vrange(stops):
    """Create concatenated ranges of integers for multiple [1]/stop

    Parameters:
        starts (1-D array_like): starts for each range
        stops (1-D array_like): stops for each range (same shape as starts)

    Returns:
        numpy.ndarray: concatenated ranges
    """
    starts = np.array([1] * len(stops))
    stops = np.asarray(stops) + 1
    L_p = stops - starts
    return np.array(np.split(np.repeat(stops - L_p.cumsum(), L_p) + np.arange(L_p.sum()), np.cumsum(stops - 1)[:-1]))

def get_points_v(x_v, y_v, z_v, d=None, n_p= None):
        "DESEMPACAR ARRAY DE WKT"
        x_v, y_v, z_v = np.asarray(x_v), np.asarray(y_v), np.asarray(z_v)

        "DISTANCIAS ENTRE X(n) - X(n-1), Y(n) - Y(n-1)"
        Lx, Ly = np.array(x_v[1:] - x_v[:-1]), np.array(y_v[1:] - y_v[:-1])

        "EXCLUIR LINEAS DE LONGITUD MENOR A LA DISTANCIA 'd'"
        if d and np.sum(np.asarray(((x_v[1:] - x_v[:-1]) ** 2 + (y_v[1:] - y_v[:-1]) ** 2) ** 0.5)) < d:
            print(np.sum(Lx), np.sum(Ly))
            pass
        else:
            "NUMERO DE PUNTOS ENTRE VERTICES"
            if n_p is None:
                nx, ny = np.array(np.around(np.abs(Lx / d), decimals=0)), np.array(np.around(np.abs(Ly / d), decimals=0))
                nx, ny = np.where(nx <= 0, 1, nx).astype(np.int), np.where(ny <= 0, 1, ny).astype(np.int)
                n_points = np.maximum(nx, ny)
            else:
                n_points = np.array([1] * len(Lx)) * np.array(n_p)

            "LONGUITUD DE SEGMENTOS ENTRE PUNTOS"
            x_space, y_space = Lx / (n_points + 1), Ly / (n_points + 1)

            "CREAR 2D ARRAY DE VALORES INICIALES"
            x_init, y_init = np.array(np.split(np.repeat(x_v[:-1], n_points), np.cumsum(n_points)[:-1])), np.array(np.split(np.repeat(y_v[:-1], n_points), np.cumsum(n_points)[:-1]))

            "CREAR RANGO DE NUMERO DE SEGMENTOS (n_points)"
            range_n = vrange(n_points)

            "CALCULO DE PUNTOS INTERMEDIOS ENTRE SEGMENTOS DE X_V y Y_v"
            if n_p is None:
                points_x, points_y = x_init + (range_n * x_space).T, y_init + (range_n * y_space).T
            else:
                points_x, points_y = x_init + (range_n * x_space[:, None]), y_init + (range_n * y_space[:,None])

            "GENERAR ARRAY DE VALORES z_v"
            points_z = np.split(np.repeat(np.array([z_v[0]] * len(points_x)), n_points), np.cumsum(n_points)[:-1])

            "BORRAR - REVISION GRAFICA DE LOS PUNTOS"
            import matplotlib.pyplot as plt
            [plt.plot(m, n, marker='o', color='red') for m,n in zip(points_x, points_y)]
            plt.plot(line.T[0], line.T[1], linewidth=1, color='C0',marker='>')
            plt.show()
            return points_x, points_y, points_z

Figure n_p=3图 n_p=3 图 n_p=3

Figure d= 0.5m图 d=0.5m 图 d=0.25m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在谷歌地球上一条线的两个已知点之间生成坐标 - How to generate coordinates in between two known points of a line on Google earth 如何计算 python 中两点数组之间的重叠长度? - how to calculate overlap length between two points array in python? 如何使用python中的返回方法计算两点之间的距离? - How to calculate the distance between two points using return methods in python? 如何计算 python 中线上两点之间的距离 - How to calculate the distance between two points on lines in python 计算两个坐标之间的角度? Python - Calculate angle between two coordinates? Python Python:获取两个3D点之间的所有坐标或绘制3D线 - Python: Get all coordinates between two 3D points or draw a 3D Line Python - 计算两点之间的步数 - Python - Calculate the number of steps between two points Python代码计算三点之间的角度(lat长坐标) - Python code to calculate angle between three points (lat long coordinates) Python - 计算两个点列表(坐标)的最小欧氏距离 - Python - calculate minimum euclidean distance of two lists of points (coordinates) 当两点之间有一条视线时,如何计算父节点与邻居节点之间的距离? - How could I calculate the distance between the parent and neighbors node when there is a line of sight between two points?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM