旋转二维数组中的块
我正在制作一个类似于著名游戏俄罗斯方块的程序,在旋转方块时遇到了一些问题。
我知道您可以使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个示例片段(使用 0 和 1 重用 int[][],这也可以使用布尔数组来完成):
您可以旋转一个片段来执行此操作:
起始片段:
这里是 < em>旋转(片):
这是旋转(旋转(片)):
这是旋转(旋转(旋转(片))):
Here's a sample piece (reusing your int[][] using 0's and 1's, which might as well be done using a boolean array):
You can rotate a piece doing this:
The starting piece:
Here's rotate(piece):
Here's rotate(rotate(piece)):
And here's rotate(rotate(rotate(piece))):
如果我理解正确,您可以通过对坐标应用固定偏移量来做到这一点。偏移量是您要围绕其旋转的中心。
我假设 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.
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.