【发布时间】:2016-04-24 20:49:33
【问题描述】:
问题
我要做的是从指定点获取 x 个环,并将这些环存储在 List<List<HexCoordinate>> 中,其中内部列表是该环中所有十六进制的列表,而 HexCoordinate 是下面定义的结构
理想情况下,我希望能够指定坐标,以及我想搜索多少环,并让算法为我抓取图块。
图像和尝试
在代码中,每个图块都由一个简单的HexCoordinate结构GitHub表示
public struct HexCoordinate : IEquatable<HexCoordinate>
{
public int X { get; private set; }
public int Y { get; private set; }
public HexCoordinate(int x, int y)
{
X = x;
Y = y;
}
public HexCoordinate North() => this + new HexCoordinate(0, -1);
public HexCoordinate South() => this + new HexCoordinate(0, 1);
public HexCoordinate West() => this + new HexCoordinate(-1, 0);
public HexCoordinate East() => this + new HexCoordinate(1, 0);
public HexCoordinate NorthWest() => this + new HexCoordinate(-1, -1);
public HexCoordinate NorthEast() => this + new HexCoordinate(1, -1);
public HexCoordinate SouthWest() => this + new HexCoordinate(-1, 1);
public HexCoordinate SouthEast() => this + new HexCoordinate(1, 1);
public bool Equals(HexCoordinate other)
{
return X == other.X &&
Y == other.Y;
}
public static HexCoordinate operator +(HexCoordinate one, HexCoordinate two)
{
return new HexCoordinate(one.X + two.X, one.Y + two.Y);
}
}
我的尝试
因为我需要展示我已经尝试过的东西,所以这是我迄今为止尝试过的
public const int MAX_RINGS = 3;
public List<List<HexCoordinate> GetsRingsWithinHex(HexCoordinate coordinate, int maxRings = MAX_RINGS)
{
// Attempt One Pseudocode
// reference: http://gamedev.stackexchange.com/questions/51264/get-ring-of-tiles-in-hexagon-grid
// int ring = 1
// Travel around the ring by traversing N,SE,S,SW,NW,N,NE multiplied by the ring number
// ring++
// Travel Around ring again
// cont until desired ring...
var hexRings = new List<List<HexCoordinate>>();
// Add in the current hex to the list
var currentHex = new List<HexCoordinate>();
currentHex.add(coordinate);
hexRings.Add(currentHex);
// Now go through and add the other rings
while (hexRings.Count <= maxRings)
{
var ring = new List<HexCoordinate>();
HexCoordinate tempCoordinate = coordinate;
int currentRingNumber = hexRings.Count;
// We start off by going north to the correct ring, and then adding it to our list
for (int i = 0; i < currentRingNumber; i++)
{
tempCoordinate = tempCoordinate.North();
}
ring.add(tempCoordinate);
// After that, we proceed to go clockwise around the ring until we come back to the start
for (int i = 0; i < currentRingNumber; i++)
{
tempCoordinate = tempCoordinate.SouthEast();
// If the ring is an odd number, you need to re-align the coordinates back to where whey should be
if (IntExtensions.IsOdd(i)) tempCoordinate = tempCoordinate.North();
ring.add(tempCoordinate);
}
// The rightmost segment is east because we can go straight down the required number of times
for (int i = 0; i < currentRingNumber; i++)
{
tempCoordinate = tempCoordinate.South();
ring.add(tempCoordinate);
}
// We utilize Current Ring - 1 because we only want this to run on rings that are greater than 1
for (int i = 0; i < currentRingNumber - 1; i++)
{
tempCoordinate = tempCoordinate.SouthWest();
ring.add(tempCoordinate);
}
// Coming into this statement, we are now at the bottom 3 coordinates.
// Since our grid is laid out vertically, we can assume that these three hexes will be directly west of eachother
// So we only have to go west twice to make our way to the next north segment
for (int i = 0; i < 2; i++)
{
tempCoordinate = tempCoordinate.West();
ring.add(tempCoordinate);
}
// We utilize Current Ring - 1 because we only want this to run on rings that are greater than 1
for (int i = 0; i < currentRingNumber - 1; i++)
{
tempCoordinate = tempCoordinate.NorthWest();
ring.add(tempCoordinate);
}
// The left most segment is easy because we can just go straight up
for (int i = 0; i < currentRingNumber; i++)
{
tempCoordinate = tempCoordinate.North();
ring.add(tempCoordinate);
}
// We utilize Current Ring - 1 because we only want this to run on rings that are greater than 1
for (int i = 0; i < currentRingNumber - 1; i++)
{
tempCoordinate = tempCoordinate.NorthEast();
// If the ring is an even number, you need to re-align the coordinates back to where whey should be
if (IntExtensions.IsEven(i)) tempCoordinate = tempCoordinate.South();
ring.add(tempCoordinate);
}
// Finally, we add the ring to our system rings and loop until we no longer fit the criteria
hexRings.Add(ring);
}
return hexRings;
}
如果需要,这里是我的 IntExtensions
public static class IntExtensions
{
public static bool IsBetween(this int num, int low, int high)
{
return num >= low && num <= high;
}
public static bool IsOdd(this int value)
{
return value % 2 != 0;
}
public static bool IsEven(this int value)
{
return value % 2 == 0;
}
}
我目前的问题是该算法适用于第 1 和第 2 环,但是一旦到达第三环(如果我将其运行超过 3 环,可能会更远),沿底部和角落的坐标开始偏移 1 ...从下面我的控制台中的输出可以看出(应该手动手动编辑)
Ring 0 - System 5, 5
Ring 1 - System 5, 4
Ring 1 - System 6, 5
Ring 1 - System 6, 6
Ring 1 - System 5, 6
Ring 1 - System 4, 6
Ring 1 - System 4, 5
Ring 2 - System 5, 3
Ring 2 - System 6, 4
Ring 2 - System 7, 4
Ring 2 - System 7, 5
Ring 2 - System 7, 6
Ring 2 - System 6, 7
Ring 2 - System 5, 7
Ring 2 - System 4, 7
Ring 2 - System 3, 6
Ring 2 - System 3, 5
Ring 2 - System 3, 4
Ring 2 - System 4, 4
Ring 3 - System 5, 2
Ring 3 - System 6, 3
Ring 3 - System 7, 3
Ring 3 - System 8, 4
Ring 3 - System 8, 5
Ring 3 - System 8, 6
Ring 3 - System 8, 7
Ring 3 - System 7, 8 //(Should be 7, 7)
Ring 3 - System 6, 9 //(Should be 6, 8)
Ring 3 - System 5, 9 //(Should be 5, 8)
Ring 3 - System 4, 9 //(Should be 4, 8)
Ring 3 - System 3, 8 //(Should be 3, 7)
Ring 3 - System 2, 7
Ring 3 - System 2, 6
Ring 3 - System 2, 5
Ring 3 - System 2, 4
Ring 3 - System 3, 4 //(Should be 3, 3)
Ring 3 - System 4, 3
有没有人可以帮助我找到正确的方向,或者为我提供一个算法来让我得到六角环?我个人在这个问题上被困了大约一天半,我似乎无法弄清楚这个问题。
【问题讨论】:
-
我首先想到的是:为什么不对网格使用不同的编号方案?双分辨率。第一列的十六进制以 0 开头,步长为 2,第二列的十六进制以 1 开头,步长为 2。因此,您的十六进制位于具有双分辨率的规则方形网格上。然后,您也许可以使用 bresenham circle 算法来快速确定一个六边形是否与半径为 2 * ring 的圆相交。
-
我的维基百科 bresenham C 例程的 F# 端口看起来不太好 :) 但从我目前的探索来看,我倾向于认为我上面描述的方法基本上是可行的。在这里,您预期的 ring3 导致了我的“双分辨率”方法。没有渲染六边形而是圆圈;)imgur.com/kZtWw8S
-
您需要考虑
x和y的LSB坐标并相应地修正位置。你不想要圆圈而是六边形,所以 Bresenham 是不可用的。相反,您正在搜索距离原点 1,2,3,4... 单元格的所有十六进制,因此A*会更好。看看Improving performance of click detection on a staggered column isometric grid,尤其是cell2scr和scr2cell,了解我所说的x,y坐标校正的LSB(它是方形网格,但问题是一样的)。顺便说一句漂亮的草图 -
@BitTickler 我得到的印象是 OP 需要所有六边形层直到距离
N而不仅仅是单个层。因此,从起点到 N 值的简单 A* 填充将简单快速地获得所需的所有内容。我的意思是基于网格的 A* 不是图形方法!!! -
@killerrin 你只需要填充物......见Backtracking in A star
标签: c# algorithm hexagonal-tiles