本文介绍了六边形网格上的2个六边形之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个六角形网格: 如何计算两个六边形之间的距离? dist((3,3),(5,5))= 3 解决方案首先应用transform(y,x)| - > 现在,面部邻接看起来像 0 1 2 3 0 * - * - * - * | \ | \ | \ | 1 * - * - * - * | \ | \ | \ | 2 * - * - * - * 让点数为(u1,v1)和(u2,v2)。令du = u2-u1和dv = v2-v1。距离是 如果du和dv具有相同的符号:max(| du |,| dv |),对角线如果du和dv有不同的符号:| du | + | dv |,因为对角线是无效的 在Python中: def dist(p1,p2): y1,x1 = p1 y2,x2 = p2 du = x1 dv =(y2 + x2 // 2) - (y1 + x1 // 2) return max(abs(du),abs(dv))if((du> = 0 and dv> = 0)或(du I have a hexagon grid:with template type coordinates T. How I can calculate distance between two hexagons?For example:dist((3,3), (5,5)) = 3dist((1,2), (1,4)) = 2 解决方案 First apply the transform (y, x) |-> (u, v) = (x, y + floor(x / 2)).Now the facial adjacency looks like 0 1 2 30*-*-*-* |\|\|\|1*-*-*-* |\|\|\|2*-*-*-*Let the points be (u1, v1) and (u2, v2). Let du = u2 - u1 and dv = v2 - v1. The distance isif du and dv have the same sign: max(|du|, |dv|), by using the diagonalsif du and dv have different signs: |du| + |dv|, because the diagonals are unproductiveIn Python:def dist(p1, p2): y1, x1 = p1 y2, x2 = p2 du = x2 - x1 dv = (y2 + x2 // 2) - (y1 + x1 // 2) return max(abs(du), abs(dv)) if ((du >= 0 and dv >= 0) or (du < 0 and dv < 0)) else abs(du) + abs(dv) 这篇关于六边形网格上的2个六边形之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-03 15:17