Open1
任意の基準座標 [x,y] から連続する複数座標 [x,y] x N点 の総接続距離をNumpyで一括算出
import numpy as np
base_point = np.asarray(
[10,7],
dtype=np.float32,
)
points = np.asarray(
[
[7,3],
[5,5],
[0,6],
[5,9],
[7,3],
],
dtype=np.float32,
)
points = np.insert(points, 0, base_point, axis=0)
print(f'every_points: {points}')
diff_val = np.diff(points, axis=0)
print(f'diff_val: {diff_val}')
every_distance = np.linalg.norm(diff_val, axis=1)
print(f'every_distance: {every_distance}')
print(f'total_distance: {np.sum(every_distance)}')
every_points: [
[10. 7.]
[ 7. 3.]
[ 5. 5.]
[ 0. 6.]
[ 5. 9.]
[ 7. 3.]
]
diff_val: [
[-3. -4.]
[-2. 2.]
[-5. 1.]
[ 5. 3.]
[ 2. -6.]
]
every_distance: [5. 2.828427 5.0990195 5.8309517 6.3245554]
total_distance: 25.08295440673828