簡體   English   中英

旋轉對象以放置在二維數組中

[英]Rotate an object for placement in a 2d array

我正在嘗試創建一個基本的RTS樣式網格。 網格工作正常,我可以通過將數字設置為0以外的任何值來放置對象。

那是容易的部分。 目前,我試圖允許放置的每個對象旋轉。 可以旋轉的對象可以是任意大小,例如1x1、2x1、3x4等,並且所有對象都有一個需要隨該對象一起旋轉的入口塊。

例如。

我的網格是空的

0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

我可以放置一個如下所示的對象:

1 2 1
1 1 1

它將像

0 0 0 0 0 0 0
0 1 2 1 0 0 0       1 2 1
0 1 1 1 0 0 0       1 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

但是旋轉時應該放在

1 2 1 0 1 1 0     1 2 1   1 1  
1 1 1 0 1 2 0     1 1 1   1 2  
0 0 0 0 1 1 0             1 1  
0 1 1 0 0 0 0       1 1        
0 2 1 0 1 1 1       2 1   1 1 1
0 1 1 0 1 2 1       1 1   1 2 1

考慮到對象可以具有不同的形狀,我試圖找出如何在代碼中實現? :(

1 1 2 1    1 1 1    1 1 1 1    1 1 1
1 1 1 1    1 1 1    1 1 1 1    2 1 1
1 1 1 1    1 1 2    1 2 1 1    1 1 1
           1 1 1               1 1 1

我想出了如何從這里的另一塊板上完成這項工作。

我要做的第一件事是將對象存儲在二維數組中,例如

1 2 1 1
1 1 1 1

然后我轉置數組,剩下這個數組

1 1
2 1
1 1
1 1

然后我旋轉每一行,剩下最后一個旋轉的對象

1 1
1 2
1 1
1 1

對於180+旋轉,我再次使用此技術即可達到所需的旋轉:)

我在Unity3D C#中的最終數組類

using UnityEngine;
using System.Collections;

namespace Arrays {

    public class Array {

        public static int[,] Rotate(int[,] array)
        {
            return Rotate90 (array);
        }

        public static int[,] Rotate90(int[,] array)
        {
            return RotateArrayRow( Transpose(array) );
        }

        public static int[,] Rotate180(int[,] array)
        {
            return Rotate90(Rotate90(array));;
        }

        public static int[,] Rotate270(int[,] array)
        {
            return Rotate90(Rotate180(array));;
        }

        private static int[,] RotateArrayRow(int[,] array){
            int x = array.GetLength(1);
            int y = array.GetLength(0);
            int[,] temp = new int[y,x];
            for(int i=0; i<y; i++)
            {
                for(int j=0; j<x; j++)
                {
                    temp[i,x-j-1] = array[i,j];
                }
            }
            return temp;
        }

        public static int[,] Transpose(int[,] array){
            int x = array.GetLength(1);
            int y = array.GetLength(0);
            int[,] temp = new int[x,y];

            for(int i=0; i<y;i++){
                for(int j=0; j<x; j++){
                    temp[j,i] = array[i,j];
                }
            }

            return temp;
        }

        public static void Log(int[,] array){
            for(int i=0; i<array.GetLength(0); i++)
            {
                string line = "";
                for(int j=0; j<array.GetLength(1); j++)
                {
                    line += array[i,j] + " ";
                }
                Debug.Log(line);
            }
        }
    }
}

首先要考慮的一件事是旋轉對象只需要實現一個旋轉功能:在一個方向上旋轉90度。 其他旋轉僅重復此操作2或3次,從而簡化了邏輯。

您需要考慮從原始數組到旋轉數組的兩種映射:

  1. 尺寸如何映射?
  2. 索引如何映射?

1很簡單:交換尺寸。 旋轉的1x4變為4x1。

2取決於您的實現,但是如果您使用2d坐標[x, y] => [y, -x] or [-y, x]取決於旋轉方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM