Python-numpy中线与点之间的欧几里得距离

时间:2019-05-09 13:06:38

标签: python numpy euclidean-distance

我想计算线与点之间的欧几里得距离。我的行由一个numpy矩阵表示,如下所示:

import pylab as pl
import numpy as np

pl.xlim(-1, 8)
pl.ylim(-1, 8)
pl.xlabel('X')
pl.ylabel('Y')


xx = np.array([-1.        , -0.91836735, -0.83673469, -0.75510204, -0.67346939,
       -0.59183673, -0.51020408, -0.42857143, -0.34693878, -0.26530612,
       -0.18367347, -0.10204082, -0.02040816,  0.06122449,  0.14285714,
        0.2244898 ,  0.30612245,  0.3877551 ,  0.46938776,  0.55102041,
        0.63265306,  0.71428571,  0.79591837,  0.87755102,  0.95918367,
        1.04081633,  1.12244898,  1.20408163,  1.28571429,  1.36734694,
        1.44897959,  1.53061224,  1.6122449 ,  1.69387755,  1.7755102 ,
        1.85714286,  1.93877551,  2.02040816,  2.10204082,  2.18367347,
        2.26530612,  2.34693878,  2.42857143,  2.51020408,  2.59183673,
        2.67346939,  2.75510204,  2.83673469,  2.91836735,  3.        ])

yy = np.array([ 2.50008142,  2.41844877,  2.33681611,  2.25518346,  2.17355081,
        2.09191815,  2.0102855 ,  1.92865285,  1.8470202 ,  1.76538754,
        1.68375489,  1.60212224,  1.52048958,  1.43885693,  1.35722428,
        1.27559162,  1.19395897,  1.11232632,  1.03069366,  0.94906101,
        0.86742836,  0.78579571,  0.70416305,  0.6225304 ,  0.54089775,
        0.45926509,  0.37763244,  0.29599979,  0.21436713,  0.13273448,
        0.05110183, -0.03053082, -0.11216348, -0.19379613, -0.27542878,
       -0.35706144, -0.43869409, -0.52032674, -0.6019594 , -0.68359205,
       -0.7652247 , -0.84685736, -0.92849001, -1.01012266, -1.09175531,
       -1.17338797, -1.25502062, -1.33665327, -1.41828593, -1.49991858])

pl.plot(xx, yy, 'k-')

这将产生下图: enter image description here

如何计算此线与点之间的欧几里得距离:enter image description here

使用Numpy吗?

1 个答案:

答案 0 :(得分:1)

不能将数组简化为端点(因此只有2个点),并使用this question吗?