当前位置: 首页 > web >正文

六边形棋盘格(Hexagonal Grids)的坐标

1. 二位坐标转六边形棋盘的方式

1-1这是“波动式”的

这种就是把【方格子坐标】“左右各错开半个格子”做到的

具体来说有如下几种情况

具体到庙算平台上,是很巧妙的用一个4位整数,前两位为x、后两位为y来进行表示 

附上计算距离的代码

def get_hex_distance(x1, y1, x2, y2):"""计算六边形网格中两个点之间的曼哈顿距离。假设使用奇数行偏移坐标系(odd-r offset)。"""dx = x2 - x1dy = y2 - y1return max(abs(dx), abs(dy), abs(dx + dy))

有多种计算方式的话可以用以下代码测试【可以对照着上面的图一个个格子数需要走几步】


def get_hex_distance(x1, y1, x2, y2):"""计算六边形网格中两个点之间的曼哈顿距离。假设使用奇数行偏移坐标系(odd-r offset)。"""dx = x2 - x1dy = y2 - y1return max(abs(dx), abs(dy), abs(dx + dy))def get_grid_distance(row1, col1, row2, col2): # TODO: not sure"""计算两个坐标间距离:param pos1::param pos2::return:"""'''转换为立方坐标'''q1 = col1 - (row1 - (row1 & 1)) // 2r1 = row1s1 = 0 - q1 - r1q2 = col2 - (row2 - (row2 & 1)) // 2r2 = row2s2 = 0 - q2 - r2'''输出距离为曼哈顿距离的1/2'''return (abs(q1 - q2) + abs(r1 - r2) + abs(s1 - s2)) // 2def test_hex_distance():"""测试 get_hex_distance 和 get_grid_distance 函数的正确性。"""# 测试用例:六边形网格中的点及其预期距离test_cases = [# 格式: (x1, y1, x2, y2, expected_distance)((0, 0, 0, 0), 0),  # 同一个点((0, 0, 1, 0), 1),  # 相邻点((0, 0, 1, 1), 2),  # ((0, 0, 2, 0), 2),  # 两步距离((0, 0, 2, 2), 4),  # ((0, 0, 3, 0), 3),  # 三步距离((0, 0, 3, 3), 6),  # ((1, 1, 4, 4), 6),  # ]print("Testing get_hex_distance:")for (x1, y1, x2, y2), expected in test_cases:try:result = get_hex_distance(x1, y1, x2, y2)print(f"get_hex_distance({x1}, {y1}, {x2}, {y2}) = {result}, expected = {expected}")if result != expected:raise ValueError(f"Test failed for get_hex_distance({x1}, {y1}, {x2}, {y2})")except Exception as e:print(f"Test failed: {result} {(x1, y1, x2, y2, expected, 'get_hex_distance')}")print("\nTesting get_grid_distance:")for (row1, col1, row2, col2), expected in test_cases:try:result = get_grid_distance(row1, col1, row2, col2)print(f"get_grid_distance({row1}, {col1}, {row2}, {col2}) = {result}, expected = {expected}")if result != expected:raise ValueError(f"Test failed for get_grid_distance({row1}, {col1}, {row2}, {col2})")except Exception as e:print(f"Test failed: {result} {(row1, col1, row2, col2, expected, 'get_grid_distance')}")if __name__ == "__main__":test_hex_distance()

1-2这是斜的

此外还有几种,可以看看外国Red Blob Games的博客(链接在最下方)

2.三维立方体转三维方式

用三个坐标来唯一确定一个格子,这样也更加直观,也是一种方式

参考资料:

6边形网格地图,格子间的距离计算 | indienova 独立游戏https://indienova.com/u/npc233/blogread/11298

Hexagonal Gridshttps://www.redblobgames.com/grids/hexagons/

http://www.xdnf.cn/news/497.html

相关文章:

  • Git 命令速查手册
  • 每日一记:CRT和图论
  • 分布式系统核心原理
  • 【第1-3章】PLC电工电子基础
  • Linux驱动开发--阻塞、非阻塞I/O
  • 【树莓派Pico FreeRTOS】-中断服务与二值信号量
  • STM32 单片机调试过程中的注意事项
  • dns作业
  • 关于Redis中的事务的理解
  • ping: socket: Operation not permitted
  • JAVAEE(网络原理—UDP报头结构)
  • 嵌入式软件--stm32 DAY 2
  • 鼠标移动操作
  • GNU,GDB,GCC,G++是什么?与其他编译器又有什么关系?
  • 4.17--4.19刷题记录(贪心)
  • swagger的简介及使用方法
  • docker基本使用命令
  • Google Agent 2 Agent 协议深度研究报告
  • Java 编译与反编译深度解析
  • Yarn基本介绍
  • 当我使用uview datetime-picker日期事件插件出行Invalid array length异常时
  • stack,queue和priority_queue
  • gis系统中如何提高shp大文件加载效率
  • 【Datawhale AI春训营】Java选手初探数据竞赛
  • 3D打印加速产品试制
  • 在RK3588上使用ZLMediaKit
  • rabbitmq 面试题
  • 体积虽小,却很好用
  • WSL+Ubuntu+miniconda环境配置
  • 淘宝淘口令解析 Api接口