You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java:如何查找起始坐标(0,0)的相邻六边形?

解决六边形相邻坐标判断的问题

嘿,我来帮你搞定这个六边形相邻查找的难题!首先得明确:六边形的相邻判断核心在于坐标系统的选择,不同的坐标系统有不同的相邻规则,这里给你讲最常用、最直观的轴向坐标方案,完美适配你的需求。

第一步:明确六边形的轴向坐标系统

我们用**(q, r)**来表示每个六边形的坐标,第三个隐含坐标s = -q - r,满足q + r + s = 0。这种系统下,中心六边形(0,0)的六个相邻六边形坐标是固定的六个方向:

  • (1, 0)
  • (1, -1)
  • (0, -1)
  • (-1, 0)
  • (-1, 1)
  • (0, 1)

第二步:两种判断相邻的方法

针对你手里的7个六边形,你可以用以下两种方法筛选出相邻的:

方法1:直接匹配预设的相邻坐标

把上述6个相邻坐标做成一个基准列表,遍历你现有的六边形,排除中心(0,0)后,检查当前坐标是否在基准列表里即可。

方法2:用距离公式判断

轴向坐标中,两个六边形的距离计算公式是:

distance = (abs(q1 - q2) + abs(q1 + r1 - q2 - r2) + abs(r1 - r2)) / 2

如果计算结果等于1,说明两个六边形相邻(中心到相邻六边形的距离就是1)。

第三步:Java代码实现(适配ArrayList需求)

这里给你写一段完整的示例代码,直接就能用:

import java.util.ArrayList;
import java.util.List;

// 定义六边形坐标类,重写equals和hashCode方便列表判断
class Hex {
    int q;
    int r;

    public Hex(int q, int r) {
        this.q = q;
        this.r = r;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Hex hex = (Hex) o;
        return q == hex.q && r == hex.r;
    }

    @Override
    public int hashCode() {
        return 31 * q + r;
    }
}

public class HexAdjacencyFinder {
    public static void main(String[] args) {
        // 中心六边形
        Hex centerHex = new Hex(0, 0);
        
        // 预设6个相邻的坐标偏移
        List<Hex> neighborOffsets = List.of(
                new Hex(1, 0),
                new Hex(1, -1),
                new Hex(0, -1),
                new Hex(-1, 0),
                new Hex(-1, 1),
                new Hex(0, 1)
        );

        // 模拟你现有的7个六边形列表
        List<Hex> allHexes = new ArrayList<>();
        allHexes.add(centerHex);
        allHexes.add(new Hex(1, 0));
        allHexes.add(new Hex(0, 1));
        allHexes.add(new Hex(-1, 0));
        allHexes.add(new Hex(0, -1));
        allHexes.add(new Hex(1, -1));
        allHexes.add(new Hex(-1, 1));

        // 收集相邻六边形的ArrayList
        List<Hex> adjacentHexes = new ArrayList<>();
        for (Hex hex : allHexes) {
            // 跳过中心自己
            if (hex.equals(centerHex)) continue;
            
            // 方法1:匹配预设相邻坐标
            if (neighborOffsets.contains(hex)) {
                adjacentHexes.add(hex);
            }
            
            // 方法2(可选):用距离公式判断
            // int distance = (Math.abs(hex.q - centerHex.q) + 
            //                 Math.abs(hex.q + hex.r - centerHex.q - centerHex.r) + 
            //                 Math.abs(hex.r - centerHex.r)) / 2;
            // if (distance == 1) {
            //     adjacentHexes.add(hex);
            // }
        }

        // 输出结果
        System.out.println("找到的相邻六边形:");
        for (Hex hex : adjacentHexes) {
            System.out.printf("坐标:(%d, %d)%n", hex.q, hex.r);
        }
    }
}

补充说明

如果你用的是其他坐标系统(比如矩形网格的偏移坐标),规则会稍微复杂一点,但轴向坐标是六边形网格计算的首选,因为它的相邻、距离计算都更简洁。

内容的提问来源于stack exchange,提问作者Ali Öztürk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:12:17