如何将图像数组旋转90度?

时间:2014-11-19 20:00:02

标签: c#

我正在创建Blokus,我正在通过创建一个数组创建游戏片段,该数组绘制单个,一个平铺图像以创建一个完整的片段(即T将由放入阵列的5个平铺图像组成,这不是总是一个完美的方形),我可以在板上移动它们,但是当涉及到旋转时,我不知道该怎么做。

“T”片按钮代码

private void TButton_Click(object sender, EventArgs e)
    {
        //Tile ID 16

        tileWidth = 3;
        tileHeight = 3;
        generateNewPiece(16);
    }

生成作品的相关部分

    public void generateNewPiece(byte tileNum)
    {
        pieceArray = new Cell[tileWidth, tileHeight];
        buttonClicked = tileNum;

        switch (tileNum)
        {
            case 16:
                pieceArray[0, 0] = new Cell(false);
                pieceArray[0, 1] = new Cell(true, currentPlayer, tileImages[currentPlayer], 40, 0);
                pieceArray[0, 2] = new Cell(false);
                pieceArray[1, 0] = new Cell(false);
                pieceArray[1, 1] = new Cell(true, currentPlayer, tileImages[currentPlayer], 40, 40);
                pieceArray[1, 2] = new Cell(false);
                pieceArray[2, 0] = new Cell(true, currentPlayer, tileImages[currentPlayer], 0, 80);
                pieceArray[2, 1] = new Cell(true, currentPlayer, tileImages[currentPlayer], 40, 80);
                pieceArray[2, 2] = new Cell(true, currentPlayer, tileImages[currentPlayer], 80, 80);
                pieceGenerated = true;

                break;
        }

细胞类

public class Cell
{

    public bool hasImage;
    public int color;
    public int x, y;
    public Image cellImage;
    //Resources.iconname

    public Cell()
    {
        this.hasImage = false;
        this.color = 0;
        this.cellImage = null;
        this.x = 0;
        this.y = 0;
    }

    public Cell(bool hasImage)
    {
        this.hasImage = hasImage;
    }

    public Cell(bool hasImage, int x, int y)
    {
        this.hasImage = hasImage;
        this.x = x;
        this.y = y;
    }

    public Cell(bool hasImage, int color, Image image, int x, int y)
    {
        this.hasImage = hasImage;
        this.color = color;
        this.cellImage = image;
        this.x = x;
        this.y = y;
    }

}

Single Tiles

Example T

1 个答案:

答案 0 :(得分:0)

您的问题实际上是如何将二维数组旋转90度(您还需要修复x,y属性)。提供了一个很好的实现here

解决方案:

int[,] array = new int[4,4] {
    { 1,2,3,4 },
    { 5,6,7,8 },
    { 9,0,1,2 },
    { 3,4,5,6 }
};

int[,] rotated = RotateMatrix(array, 4);

static int[,] RotateMatrix(int[,] matrix, int n) {
    int[,] ret = new int[n, n];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ret[i, j] = matrix[n - j - 1, i];
        }
    }

    return ret;
}