从数组 python 中找到每个点之间的长度
find length between each point from array python
假设我们有 8 个点,其 x,y 坐标给定为
[[[224 64]]
[[ 62 381]]
[[224 661]]
[[568 661]]
[[733 348]]
[[650 205]]
[[509 204]]
[[509 64]]]
假设这是一个多边形的 8 个点,想求出每条边的长度。对于两点,我可以找到长度
dx = math.abs(x2 - x1)
dy = math.abs(y2 - y1)
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
如何计算以上 x, y
坐标的多边形每边的长度?
import math
points = [
[224, 64],
[ 62, 381],
[224, 661],
[568, 661],
[733, 348],
[650, 205],
[509, 204],
[509, 64]]
for x,y in zip(points,points[1:]):
d = math.sqrt((x[1]-y[1])*(x[1]-y[1]) + (x[0]-y[0])*(x[0]-y[0]))
print(x, y, d)
输出:
[224, 64] [62, 381] 355.99578649191903
[62, 381] [224, 661] 323.4872485894923
[224, 661] [568, 661] 344.0
[568, 661] [733, 348] 353.82764165621654
[733, 348] [650, 205] 165.3420696616563
[650, 205] [509, 204] 141.00354605470034
[509, 204] [509, 64] 140.0
这是它的样子:
跟进
这是使用您的确切结构的代码:
import numpy as np
import math
points = [
[224, 64],
[ 62, 381],
[224, 661],
[568, 661],
[733, 348],
[650, 205],
[509, 204],
[509, 64]]
points = np.array(points).reshape(8,1,2)
print(points)
for pt in zip(points,points[1:]):
print( pt )
x = pt[0][0]
y = pt[1][0]
d = math.sqrt((x[1]-y[1])*(x[1]-y[1]) + (x[0]-y[0])*(x[0]-y[0]))
print(x, y, d)
输出:
[[[224 64]]
[[ 62 381]]
[[224 661]]
[[568 661]]
[[733 348]]
[[650 205]]
[[509 204]]
[[509 64]]]
(array([[224, 64]]), array([[ 62, 381]]))
[224 64] [ 62 381] 355.99578649191903
(array([[ 62, 381]]), array([[224, 661]]))
[ 62 381] [224 661] 323.4872485894923
(array([[224, 661]]), array([[568, 661]]))
[224 661] [568 661] 344.0
(array([[568, 661]]), array([[733, 348]]))
[568 661] [733 348] 353.82764165621654
(array([[733, 348]]), array([[650, 205]]))
[733 348] [650 205] 165.3420696616563
(array([[650, 205]]), array([[509, 204]]))
[650 205] [509 204] 141.00354605470034
(array([[509, 204]]), array([[509, 64]]))
[509 204] [509 64] 140.0
假设您的列表名为 coords
。你有一个 numpy
标签,所以我假设你想要一个使用 numpy 的快速解决方案。
coords = [
[224, 64],
[ 62, 381],
[224, 661],
[568, 661],
[733, 348],
[650, 205],
[509, 204],
[509, 64]]
您需要对连续的元素调用 np.diff
,因此要获得最后一面,您需要在最后复制第一个点。您可以在将数组转换为 numpy 的同时执行此操作:
vertices = np.concatenate((coords, coords[:1]), axis=0)
现在求边长:
sides = np.linalg.norm(np.diff(vertices, axis=0), axis=-1)
对于您的 2D 案例,您也可以使用 np.hypot
而不是 np.linalg.norm
:
sides = np.hypot(*np.diff(vertices, axis=0).T)
假设我们有 8 个点,其 x,y 坐标给定为
[[[224 64]]
[[ 62 381]]
[[224 661]]
[[568 661]]
[[733 348]]
[[650 205]]
[[509 204]]
[[509 64]]]
假设这是一个多边形的 8 个点,想求出每条边的长度。对于两点,我可以找到长度
dx = math.abs(x2 - x1)
dy = math.abs(y2 - y1)
dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
如何计算以上 x, y
坐标的多边形每边的长度?
import math
points = [
[224, 64],
[ 62, 381],
[224, 661],
[568, 661],
[733, 348],
[650, 205],
[509, 204],
[509, 64]]
for x,y in zip(points,points[1:]):
d = math.sqrt((x[1]-y[1])*(x[1]-y[1]) + (x[0]-y[0])*(x[0]-y[0]))
print(x, y, d)
输出:
[224, 64] [62, 381] 355.99578649191903
[62, 381] [224, 661] 323.4872485894923
[224, 661] [568, 661] 344.0
[568, 661] [733, 348] 353.82764165621654
[733, 348] [650, 205] 165.3420696616563
[650, 205] [509, 204] 141.00354605470034
[509, 204] [509, 64] 140.0
这是它的样子:
跟进
这是使用您的确切结构的代码:
import numpy as np
import math
points = [
[224, 64],
[ 62, 381],
[224, 661],
[568, 661],
[733, 348],
[650, 205],
[509, 204],
[509, 64]]
points = np.array(points).reshape(8,1,2)
print(points)
for pt in zip(points,points[1:]):
print( pt )
x = pt[0][0]
y = pt[1][0]
d = math.sqrt((x[1]-y[1])*(x[1]-y[1]) + (x[0]-y[0])*(x[0]-y[0]))
print(x, y, d)
输出:
[[[224 64]]
[[ 62 381]]
[[224 661]]
[[568 661]]
[[733 348]]
[[650 205]]
[[509 204]]
[[509 64]]]
(array([[224, 64]]), array([[ 62, 381]]))
[224 64] [ 62 381] 355.99578649191903
(array([[ 62, 381]]), array([[224, 661]]))
[ 62 381] [224 661] 323.4872485894923
(array([[224, 661]]), array([[568, 661]]))
[224 661] [568 661] 344.0
(array([[568, 661]]), array([[733, 348]]))
[568 661] [733 348] 353.82764165621654
(array([[733, 348]]), array([[650, 205]]))
[733 348] [650 205] 165.3420696616563
(array([[650, 205]]), array([[509, 204]]))
[650 205] [509 204] 141.00354605470034
(array([[509, 204]]), array([[509, 64]]))
[509 204] [509 64] 140.0
假设您的列表名为 coords
。你有一个 numpy
标签,所以我假设你想要一个使用 numpy 的快速解决方案。
coords = [
[224, 64],
[ 62, 381],
[224, 661],
[568, 661],
[733, 348],
[650, 205],
[509, 204],
[509, 64]]
您需要对连续的元素调用 np.diff
,因此要获得最后一面,您需要在最后复制第一个点。您可以在将数组转换为 numpy 的同时执行此操作:
vertices = np.concatenate((coords, coords[:1]), axis=0)
现在求边长:
sides = np.linalg.norm(np.diff(vertices, axis=0), axis=-1)
对于您的 2D 案例,您也可以使用 np.hypot
而不是 np.linalg.norm
:
sides = np.hypot(*np.diff(vertices, axis=0).T)