计算半径中的 2d 坐标(对于基于图块的游戏)
Calculate 2d coordinates in radius (for a tile based game)
我搜索了一段时间,没有找到满意的答案。
目前我有一个图块列表(一个表示图块 ID 的整数数组)、宽度和高度(以图块为单位)。当我渲染它时,所有的瓷砖都被正确放置,等等。但我真正的问题是我无法找出一种算法来从内到外计算给定半径(相对于另一个瓷砖)内的瓷砖。我打算用它来进行照明计算。这就是为什么我需要从内到外计算(对于梯度)。我不能在 x 和 y 上使用 + 和 - 进行静态计算,因为我计划使用可变半径大小。有谁知道这样做的好方法吗?
编辑:"from the inside out" 我的意思是照明布局的方式,靠近光源的照明应该比远离光源的更强烈。我打算用下面的 ASCII 代码来直观地展示它:
0 0 0 0 0 0 0
0 0 --------- 0 0
0 | * + * | 0
0 | + x + | 0
0 | * + * | 0
0 0 --------- 0 0
0 0 0 0 0 0 0
提前致谢:)
关于如何实施的细节当然有许多不同的选择。您没有确切说明您如何表示 "tiles"(例如,关于接口)。但这是一种方法,也许它已经有所帮助:
想法是将图块的坐标存储为 Point
个对象的列表。 (将这些点转换为一维索引是可能的,但应该完全独立于实际问题)。
这些点是通过沿着相关区域的 "edges" 步行计算的:
- 从右下角开始,向上(
dx=0, dy=-1
)
- 从右上角开始,向左走(
dx=-1, dy=0
)
- 从左上角开始,向下(
dx=0, dy=1
)
- 从左下角开始,向右走(
dx=1, dy=0
)
所有这些点都放在一个列表中,列表中包含所有与中心点有一定 Manhattan Distance 的点,按逆时针顺序排列。
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
public class TileDistances
{
public static void main(String[] args)
{
int sizeX = 11;
int sizeY = 11;
int centerX = 5;
int centerY = 5;
for (int radius=1; radius<5; radius++)
{
System.out.println(
"Radius "+radius+" around "+centerX+","+centerY);
List<Point> points = coordinates(centerX, centerY, radius);
char c = (char)('0'+radius);
System.out.println(createString(points, sizeX, sizeY, c));
}
}
private static String createString(
List<Point> points, int sizeX, int sizeY, char c)
{
StringBuffer sb = new StringBuffer();
for (int y=0; y<sizeY; y++)
{
for (int x=0; x<sizeX; x++)
{
Point p = new Point(x,y);
if (points.contains(p))
{
sb.append(c);
}
else
{
sb.append(".");
}
}
sb.append("\n");
}
return sb.toString();
}
private static List<Point> coordinates(
int cx, int cy, int r)
{
List<Point> coordinates = new ArrayList<Point>();
int steps = r + r;
addAll(cx + r, cy + r, 0, -1, steps, coordinates);
addAll(cx + r, cy - r, -1, 0, steps, coordinates);
addAll(cx - r, cy - r, 0, 1, steps, coordinates);
addAll(cx - r, cy + r, 1, 0, steps, coordinates);
return coordinates;
}
private static void addAll(
int x0, int y0, int dx, int dy, int steps,
List<Point> coordinates)
{
int x = x0;
int y = y0;
for (int i=0; i<steps; i++)
{
coordinates.add(new Point(x,y));
x += dx;
y += dy;
}
}
}
在这个例子中,将打印半径 1 到 4,例如:
Radius 3 around 5,5
...........
...........
..3333333..
..3.....3..
..3.....3..
..3.....3..
..3.....3..
..3.....3..
..3333333..
...........
...........
(可能有更高效 and/or 更优雅的解决方案,这取决于图块的精确表示方式,但我认为这个很容易理解并且普遍适用,因为它只是提供了一个坐标集,并且不对底层数据结构做出假设)。
我搜索了一段时间,没有找到满意的答案。
目前我有一个图块列表(一个表示图块 ID 的整数数组)、宽度和高度(以图块为单位)。当我渲染它时,所有的瓷砖都被正确放置,等等。但我真正的问题是我无法找出一种算法来从内到外计算给定半径(相对于另一个瓷砖)内的瓷砖。我打算用它来进行照明计算。这就是为什么我需要从内到外计算(对于梯度)。我不能在 x 和 y 上使用 + 和 - 进行静态计算,因为我计划使用可变半径大小。有谁知道这样做的好方法吗?
编辑:"from the inside out" 我的意思是照明布局的方式,靠近光源的照明应该比远离光源的更强烈。我打算用下面的 ASCII 代码来直观地展示它:
0 0 0 0 0 0 0
0 0 --------- 0 0
0 | * + * | 0
0 | + x + | 0
0 | * + * | 0
0 0 --------- 0 0
0 0 0 0 0 0 0
提前致谢:)
关于如何实施的细节当然有许多不同的选择。您没有确切说明您如何表示 "tiles"(例如,关于接口)。但这是一种方法,也许它已经有所帮助:
想法是将图块的坐标存储为 Point
个对象的列表。 (将这些点转换为一维索引是可能的,但应该完全独立于实际问题)。
这些点是通过沿着相关区域的 "edges" 步行计算的:
- 从右下角开始,向上(
dx=0, dy=-1
) - 从右上角开始,向左走(
dx=-1, dy=0
) - 从左上角开始,向下(
dx=0, dy=1
) - 从左下角开始,向右走(
dx=1, dy=0
)
所有这些点都放在一个列表中,列表中包含所有与中心点有一定 Manhattan Distance 的点,按逆时针顺序排列。
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
public class TileDistances
{
public static void main(String[] args)
{
int sizeX = 11;
int sizeY = 11;
int centerX = 5;
int centerY = 5;
for (int radius=1; radius<5; radius++)
{
System.out.println(
"Radius "+radius+" around "+centerX+","+centerY);
List<Point> points = coordinates(centerX, centerY, radius);
char c = (char)('0'+radius);
System.out.println(createString(points, sizeX, sizeY, c));
}
}
private static String createString(
List<Point> points, int sizeX, int sizeY, char c)
{
StringBuffer sb = new StringBuffer();
for (int y=0; y<sizeY; y++)
{
for (int x=0; x<sizeX; x++)
{
Point p = new Point(x,y);
if (points.contains(p))
{
sb.append(c);
}
else
{
sb.append(".");
}
}
sb.append("\n");
}
return sb.toString();
}
private static List<Point> coordinates(
int cx, int cy, int r)
{
List<Point> coordinates = new ArrayList<Point>();
int steps = r + r;
addAll(cx + r, cy + r, 0, -1, steps, coordinates);
addAll(cx + r, cy - r, -1, 0, steps, coordinates);
addAll(cx - r, cy - r, 0, 1, steps, coordinates);
addAll(cx - r, cy + r, 1, 0, steps, coordinates);
return coordinates;
}
private static void addAll(
int x0, int y0, int dx, int dy, int steps,
List<Point> coordinates)
{
int x = x0;
int y = y0;
for (int i=0; i<steps; i++)
{
coordinates.add(new Point(x,y));
x += dx;
y += dy;
}
}
}
在这个例子中,将打印半径 1 到 4,例如:
Radius 3 around 5,5
...........
...........
..3333333..
..3.....3..
..3.....3..
..3.....3..
..3.....3..
..3.....3..
..3333333..
...........
...........
(可能有更高效 and/or 更优雅的解决方案,这取决于图块的精确表示方式,但我认为这个很容易理解并且普遍适用,因为它只是提供了一个坐标集,并且不对底层数据结构做出假设)。