【问题标题】:Find neighboring hexes from center hex instead of from outside of a grouped hexes从中心十六进制而不是从分组的六边形之外查找相邻的六边形
【发布时间】:2018-03-21 07:16:17
【问题描述】:

我正在关注一些关于地图随机十六进制生成的教程。我目前正在尝试通过获取围绕该特定十六进制的其他十六进制来根据地图上的单个十六进制随机生成大陆。

这是我将所有相邻的十六进制放入数组的代码:

    public HexCell[] GetHexWithinRange (HexCell centerHex, int range)
{
    List<HexCell> hexWithinRange = new List<HexCell>();
    int x = centerHex.coordinates.X;
    int y = centerHex.coordinates.Y;
    int z = centerHex.coordinates.Z;

    int sequence = 0;
    for (int dy = -range; dy < range + 1; dy++) 
    {
        for (int dz = -range; dz < range + 1; dz++)
        {
            for (int dx = -range; dx < range + 1; dx++)
            {
                HexCell neighborHex = GetCellFromCoord(x + (float)dx,
                 y + (float)dy, z + (float)dz);

                if (neighborHex == null)
                { continue; }
                else
                {
                    neighborHex.sequenceCheck = sequence++;
                    hexWithinRange.Add(neighborHex);
                }
            }
        }
    }
    return hexWithinRange.ToArray();
}

Perfectly hexagonal shaped continent

虽然代码返回了一个形状完美的大陆,但它是将其他六角从外环对角线放入中心六角:

Sequence when the code is putting the neighboring hex into array

我真正希望它做的是从大陆内部获取相邻的十六进制并从那里移动到外部,直到它达到预期的半径,例如:

Sequence how I want the code to put the neighboring hex into array

我尝试提出一个公式,将相邻的十六进制逐个环放入数组中。但我想保持我的半径从中心十六进制成为一个变量。我怎么做?

【问题讨论】:

    标签: c# unity3d math


    【解决方案1】:

    请注意,您要按半径走每个“轴”。

    例如,如果您从 [36,-57,21] 开始,半径 == 1,则沿 X 移动 1 个 Hex,然后沿 Y 移动 1,然后沿 z 移动 1,然后沿 X、Y 和 Z 向后移动,并返回 [36, -57, 21]。

    当半径 == 2 时,它是一样的,只是你每个轴行进 2 次。等等。

    所以,我认为这样的事情会起作用(如果您的 Vector3 十六进制表示是正确的并且定义了加法)

    var center = centerHex.coordinates;
    var offset = new Vector3(0, 0, 0);
    
    int radius = 0;
    for(radius = 0; radius < 4 radius ++){
      for(i = 0; i < radius; i++){
        AddCell(center + offset);
        offset.X++;
      }
    
      for(i = 0; i < radius; i++){
        AddCell(center + offset);
        offset.Y--;
      }
    
      for(i = 0; i < radius; i++){
        AddCell(center + offset);
        offset.Z--;
      }
    
      for(i = 0; i < radius; i++){
        AddCell(center + offset);
        offset.X--;
      }
    
      for(i = 0; i < radius; i++){
        AddCell(center + offset);
        offset.Y++;
      }
    
      for(i = 0; i < radius; i++){
        AddCell(center + offset);
        offset.Z++;
      }
    
      offset.Z++;
    
    }
    

    }

    【讨论】:

    • 谢谢伙计。你的帖子让我知道如何解决这个问题。
    【解决方案2】:

    我想通了。我没有使用范围作为循环的限制,而是在原始范围内创建了一个增量范围。为了避免最初的问题再次出现,我在添加当前所在的单元格之前循环检查列表/数组中的重复单元格。

        for (int curRange = 1; curRange < range + 1; curRange++)
        {
            for (int dy = -curRange; dy < curRange + 1; dy++)
            {
                for (int dz = -curRange; dz < curRange + 1; dz++)
                {
                    for (int dx = -curRange; dx < curRange + 1; dx++)
                    {
                        HexCell neighborHex = GetCellFromCoord(x + (float)dx, y + (float)dy, z + (float)dz);
    
                        if (neighborHex == null)
                        {
                            continue;
                        }
                        else if (x == neighborHex.coordinates.X && 
                            y == neighborHex.coordinates.Y && 
                            z == neighborHex.coordinates.Z)
                        {
                            continue;
                        }
                        else
                        {
                            if (hexWithinRange.Contains(neighborHex))
                            {
                                continue;
                            }
                            else
                            {
                                hexWithinRange.Add(neighborHex);
                            }
                        }
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多