【问题标题】:Calculate distance between points in polygon计算多边形中点之间的距离
【发布时间】:2021-10-25 11:28:14
【问题描述】:

我找到了很多关于如何计算两点之间或从一个点到多边形的距离的帖子,但我根本找不到如何计算每条边的距离。 我有一个多边形,坐标是这些:

 [[[623 284]] 

 [[526 256]]

 [[532 189]]

 [[504 166]]

 [[323 175]]

 [[276 219]]

 [[119 221]]

 [[  1 272]]

 [[  0 473]]

 [[615 479]]]

我只是想计算每条边的长度。也许我应该将(math.dist(p, q))for loop 一起使用?

【问题讨论】:

  • 使用math.dist() 和for 循环当然可以。为了让您的工作更轻松,您可以考虑使用for p1, p2 in zip(points, points[1:]): 处理成对的后续点。
  • [math.dist(p, q) for (p, q) in zip(points, points[1:]+[points[0]])][math.dist(points[i], points[(i+1)%len(points)]) for i in range(len(points))](+[points[0]] 和 %len(points) 将包括 n-1 到 0 边)
  • @AKX for p1, p2 in zip(points, points[1:]+[points[0]]) 如果这是一个封闭的多边形。

标签: python list numpy math


【解决方案1】:

有一种数学方法可以轻松做到这一点。使用 2D 毕达哥拉斯定理: 对于以下几点: A(x1,y1) B(x2,x2)

两点之间的距离: D = sqrt( (x2-x1)^2 + (y2-y1)^2 )

作为一个 python 脚本,你可以创建一个这样的函数:

import math

A = [1,2]
B = [3, 3]

distance = math.sqrt( (B[0] - A[0])**2 + (B[1] - A[1])**2 )


【讨论】:

    【解决方案2】:

    如果点坐标存储在两个python列表xy中,那么你可以计算每个边的长度:

    import numpy as np
    
    x.append(x[0])
    y.append(y[0])
    
    d = [np.sqrt((x[i + 1] - x[i])**2 + (y[i + 1] - y[i])**2) for i in range(len(x) - 1)]
    

    需要append 才能闭合多边形并计算所有边长。根据您在上面提供的点,边缘距离为:

    [100.96038827183659, 67.26812023536856, 36.235341863986875, 181.22361876973983, 64.38167441127949, 157.01273833673497, 128.5496013218244, 201.0024875467963, 615.0292675962665, 195.1640335717624]
    

    【讨论】:

    • 成功了!我只想补充一点,我在问题中提供了嵌套数组,所以首先我必须像这样分隔 x 和 y:x = [element[0][0] for element in array], y = [element[ 0][1] 表示数组中的元素]
    • 在这里使用numpy.sqrt,而不是math.sqrtmath.dist,有什么好处吗?
    • 不,numpy.sqrtmath.sqrt 可以安全交换。不知道math.dist,因为我使用的是python 3.7.0,无法在我的机器上升级
    【解决方案3】:

    既然你有一个numpy数组,解决办法就变得很简单了:

    • 计算向量相邻点之间的差异
    • 使用“范数”计算这些向量的长度,这里是 L2 范数,即 欧几里得范数

    这使用整个数组操作。

    import numpy as np
    
    points = np.array([
     [[623, 284]],
     [[526, 256]],
     [[532, 189]],
     [[504, 166]],
     [[323, 175]],
     [[276, 219]],
     [[119, 221]],
     [[  1, 272]],
     [[  0, 473]],
     [[615, 479]]])
    # funny shape because OpenCV. it's a Nx1 vector of 2-channel elements
    # fix that up, remove the silly dimension
    points.shape = (-1, 2)
    
    # look at the data, how it's moving the values around
    following_points = np.roll(points, -1, axis=0)
    
    # vector: difference between following point and this point
    vectors = following_points - points
    
    # length of a vector, using the L2/euclidean norm
    lengths = np.linalg.norm(vectors, axis=1)
    
    print("distance to following point:")
    print(lengths)
    
    # this is just for printing/displaying, not useful in code
    print("point and distance to following point:")
    print(np.hstack([points, lengths.reshape((-1, 1))]))
    
    
    distance to following point:
    [100.96039  67.26812  36.23534 181.22362  64.38167 157.01274 128.5496  201.00249 615.02927 195.16403]-89.72609 -88.20969]
    point and distance to following point:
    [[623.      284.      100.96039]
     [526.      256.       67.26812]
     [532.      189.       36.23534]
     [504.      166.      181.22362]
     [323.      175.       64.38167]
     [276.      219.      157.01274]
     [119.      221.      128.5496 ]
     [  1.      272.      201.00249]
     [  0.      473.      615.02927]
     [615.      479.      195.16403]]
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2017-02-21
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2017-06-04
      • 1970-01-01
      相关资源
      最近更新 更多