python - Python:如何计算细胞之间的距离?
问题描述
假设我想计算方形网格中单元格之间的距离5x5
。两个细胞之间的距离是100m
。
网格的每个单元格都是介于0
和之间的数字24
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
例如:
distance between cell 0 and 3 is 300
distance between cell 2 and 7 is 100
distance between cell 11 and 19 is 400
我必须计算细胞之间的距离x
和位置的不同。y
gs = 5 ## Cells per side
S = gs*gs ## Grid Size
r0 = 100 ## distance between two cells
for i in range(0, S):
for j in range(0, S):
if i == j: continue
x = int(floor(i/gs))
y = int(floor(j/gs))
dist = x*r0 + abs(j-i)*r0
但这不是正确的解决方案
解决方案
# n1, n2 = cell numbers
cellsize = 100.0
x1,x2 = n1%gs, n2%gs
y1,y2 = n1//gs, n2//gs
dist = sqrt( float(x1-x2)**2 + float(y1-y2)**2) # pythagoras theorem
dist *= cellsize
推荐阅读
- rust - 为什么这个 TextureCreator 实例没有
活得够长吗? - c++ - 使用带有指针的 new 时创建指定长度的数组
- javascript - 如何在javascript中设置上午/下午
- android - 如何将 CheckBox buttonDrawable 重置为默认值
- regex - 如何在 Perl 中合并两个以不同键开头的哈希?
- html - 光标位置
- 元素
- glsl - 如何以不同的颜色绘制立方体每个面的每个角?
- asp.net-core-3.0 - 在 .Net core 3.0 控制台应用程序中使用 HttpClient 上传文件
- python - 如何查找一行不包含指定字符串短语的时间?
- python - 试图连接多个 csv 文件,目录错误?