调整二维地图的大小
Resize a 2D map
我正在为一款游戏开发 2D 地图编辑器,我需要一种方法来在所有方向上统一增加或减小 grid/map 的大小。
假设您有一张带有某种 "cross" 符号的 3x3 地图。
(数组是零索引的。它们从 0 开始)
像这样:
0,1,0
1,1,1
0,1,0
数组看起来像这样:
map = [0,1,0,1,1,1,0,1,0]
因此,图块索引 4 将是地图的中心。
例如,我想将尺寸从 3x3 增加到 5x5。所以我最终得到了这个:
0,0,0,0,0
0,0,1,0,0
0,1,1,1,0
0,0,1,0,0
0,0,0,0,0
新的地图数组应该这样结束:
map = [0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0]
这样做的好方法是什么?
这里有增加和减少两个函数。参数 arr 是你的一维地图,xWidth 是你的网格的宽度(当然还有高度)。
我有一个背景相似的问题 so many thanks to willywonka_dailyblah 谁在 j 和 i 索引上帮助了我。
public int[] increase_grid(int[] arr, int xWidth)
{
int newWidth = (xWidth+2);
int[] result = new int[newWidth * newWidth];
int count=0;
while(count<newWidth)
{
result[count++] = 0;
}
for (int i=0;i<xWidth;i++)
{
result[count++] = 0;
for (int j=0;j<xWidth;j++)
{
result[count++] = arr[i * xWidth + j];
}
result[count++] = 0;
}
while(count<(newWidth*newWidth))
{
result[count++] = 0;
}
return result;
}
public int[] decrease_grid(int[] arr, int xWidth)
{
int newWidth = (xWidth-2);
int[] result = new int[newWidth*newWidth];
for(int i=0; i< newWidth;i++)
{
for (int j=0;j< newWidth;j++)
{
result[i* newWidth + j] = arr[(i+1) * xWidth + (j+1)];
}
}
return result;
}
我有这个打印功能:
public void print_arr(int[] a, int xWidth)
{
for(int i=0;i<xWidth;i++)
{
for(int j=0;j<xWidth;j++)
{
System.out.print(a[i * xWidth + j]+" ");
}
System.out.println();
}
System.out.println();
}
您可以像这样调用这些函数:
int[] map = new int[]{0,1,0,1,1,1,0,1,0};
print_arr(map, 3);
map = increase_grid(map, 3);
print_arr(map, 5);
map = increase_grid(map, 5);
print_arr(map, 7);
map = decrease_grid(map, 7);
print_arr(map, 5);
因此您必须传递地图的当前大小,然后调用增加或减少。请注意,这些函数包含一个嵌套的 for 循环。因此,它们在更大的网格尺寸上的可扩展性较差。我认为可能有一种解决方案可以将其包装成一个循环序列,该循环序列无需嵌套即可运行。
我正在为一款游戏开发 2D 地图编辑器,我需要一种方法来在所有方向上统一增加或减小 grid/map 的大小。
假设您有一张带有某种 "cross" 符号的 3x3 地图。
(数组是零索引的。它们从 0 开始)
像这样:
0,1,0
1,1,1
0,1,0
数组看起来像这样:
map = [0,1,0,1,1,1,0,1,0]
因此,图块索引 4 将是地图的中心。
例如,我想将尺寸从 3x3 增加到 5x5。所以我最终得到了这个:
0,0,0,0,0
0,0,1,0,0
0,1,1,1,0
0,0,1,0,0
0,0,0,0,0
新的地图数组应该这样结束:
map = [0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0]
这样做的好方法是什么?
这里有增加和减少两个函数。参数 arr 是你的一维地图,xWidth 是你的网格的宽度(当然还有高度)。
我有一个背景相似的问题
public int[] increase_grid(int[] arr, int xWidth)
{
int newWidth = (xWidth+2);
int[] result = new int[newWidth * newWidth];
int count=0;
while(count<newWidth)
{
result[count++] = 0;
}
for (int i=0;i<xWidth;i++)
{
result[count++] = 0;
for (int j=0;j<xWidth;j++)
{
result[count++] = arr[i * xWidth + j];
}
result[count++] = 0;
}
while(count<(newWidth*newWidth))
{
result[count++] = 0;
}
return result;
}
public int[] decrease_grid(int[] arr, int xWidth)
{
int newWidth = (xWidth-2);
int[] result = new int[newWidth*newWidth];
for(int i=0; i< newWidth;i++)
{
for (int j=0;j< newWidth;j++)
{
result[i* newWidth + j] = arr[(i+1) * xWidth + (j+1)];
}
}
return result;
}
我有这个打印功能:
public void print_arr(int[] a, int xWidth)
{
for(int i=0;i<xWidth;i++)
{
for(int j=0;j<xWidth;j++)
{
System.out.print(a[i * xWidth + j]+" ");
}
System.out.println();
}
System.out.println();
}
您可以像这样调用这些函数:
int[] map = new int[]{0,1,0,1,1,1,0,1,0};
print_arr(map, 3);
map = increase_grid(map, 3);
print_arr(map, 5);
map = increase_grid(map, 5);
print_arr(map, 7);
map = decrease_grid(map, 7);
print_arr(map, 5);
因此您必须传递地图的当前大小,然后调用增加或减少。请注意,这些函数包含一个嵌套的 for 循环。因此,它们在更大的网格尺寸上的可扩展性较差。我认为可能有一种解决方案可以将其包装成一个循环序列,该循环序列无需嵌套即可运行。