旋转二维数组中的块

发布于 2025-01-08 05:03:00 字数 258 浏览 1 评论 0原文

我正在制作一个类似于著名游戏俄罗斯方块的程序,在旋转方块时遇到了一些问题。

我知道您可以使用“x = -y”和“y = x”在坐标系中旋转图形,但问题是因为我使用整数数组来表示块,所以事情变得更加困难。

我的数组看起来像:

int[][] space = new int[20][10];

如果一个坐标包含一个块,则值为 1,否则为 0。

那么如何在该空间中旋转块而不会遇到负数问题呢?

I am making a program similar to the famous game Tetris and I've run into some problems when it comes to rotating a block.

I know you are able to rotate figures in a coordinate system by using "x = -y" and "y = x" but the problem is that because I am using an array of integers to represent the block it makes things so much more difficult.

My array looks like:

int[][] space = new int[20][10];

And if a coordinate contains a block the value is 1 else it's 0.

So how can I rotate a block in that space without getting trouble with negative numbers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2025-01-15 05:03:00

这是一个示例片段(使用 0 和 1 重用 int[][],这也可以使用布尔数组来完成):

private static final int[][] piece = new int[][] {
        { 0, 1, 0, },
        { 1, 1, 1, },
};

您可以旋转一个片段来执行此操作:

private static int[][] rotate( final int[][] piece ) {
    final int[][] res = new int[piece[0].length][piece.length];
    for (int x = 0; x < piece.length; x++) {
        for (int y = 0; y < piece[0].length; y++) {
            res[(res.length-1)-y][x] = piece[x][y];
        }
    }
    return res;
}

起始片段:

010
111

这里是 < em>旋转(片):

01
11
01

这是旋转(旋转(片))

111
010

这是旋转(旋转(旋转(片)))

10
11
10

Here's a sample piece (reusing your int[][] using 0's and 1's, which might as well be done using a boolean array):

private static final int[][] piece = new int[][] {
        { 0, 1, 0, },
        { 1, 1, 1, },
};

You can rotate a piece doing this:

private static int[][] rotate( final int[][] piece ) {
    final int[][] res = new int[piece[0].length][piece.length];
    for (int x = 0; x < piece.length; x++) {
        for (int y = 0; y < piece[0].length; y++) {
            res[(res.length-1)-y][x] = piece[x][y];
        }
    }
    return res;
}

The starting piece:

010
111

Here's rotate(piece):

01
11
01

Here's rotate(rotate(piece)):

111
010

And here's rotate(rotate(rotate(piece))):

10
11
10
甚是思念 2025-01-15 05:03:00

如果我理解正确,您可以通过对坐标应用固定偏移量来做到这一点。偏移量是您要围绕其旋转的中心。

int oldSpace[][] = new int[20][10];
int newSpace[][] = new int[20][10];
int offX = 10;
int offY = 5;
for(int x = -5; x < 5; x++) {
  for(int y = -5; y < 5; y++) {
    newSpace[offX+x][offY+y] = oldSpace[offX-y][offY+x];
  }
}

我假设 20 是 X 维度,10 是 Y 维度。这将围绕坐标 (10,5) 旋转 10x10 块。请注意,我只旋转了中间的 10x10 块,因为这是两个空间之间的重叠。也许您正在将 [20][10] 数组复制到 [10][20] 数组,在这种情况下,您可以增加 y 的范围,使其从 -10 运行到 9。

编辑:抱歉,您还需要一个如果您有不同形状的输出数组,则一组不同的偏移量,因为 (10,5) 将不再位于中心。但你应该能够弄清楚这一点。

If I've understood this correctly, you can do this by applying a fixed offset to the coordinates. The offset is the center around which you want to rotate.

int oldSpace[][] = new int[20][10];
int newSpace[][] = new int[20][10];
int offX = 10;
int offY = 5;
for(int x = -5; x < 5; x++) {
  for(int y = -5; y < 5; y++) {
    newSpace[offX+x][offY+y] = oldSpace[offX-y][offY+x];
  }
}

I assumed 20 was the X dimension and 10 the Y dimension. This rotates a 10x10 block around the coordinate (10,5). Note that I've only rotated a 10x10 block in the middle as that's the overlap between the two spaces. Perhaps you are copying a [20][10] array to a [10][20] array, and in that case you can increase the range of y so it runs from -10 to 9.

EDIT: Sorry, you also need a different set of offsets if you have a different shaped output array, as (10,5) will no longer be in the center. But you should be able to figure this out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文