【问题标题】:Rotate 2D Array by 45 degrees将二维阵列旋转 45 度
【发布时间】:2010-07-19 07:28:22
【问题描述】:

如何将具有奇数行的二维整数矩形数组旋转 45 度?

类似

int[] myArray = new int[,]   
{  
    {1, 0 ,1},  
    {0, 1 ,0},  
    {0, 0 ,0},  
} 

进入

int[] rotatedArray = new int[,]   
{  
    {0, 1 ,0},  
    {0, 1 ,1},  
    {0, 0 ,0},  
}  

适用于任何尺寸(3x3、5x5、7x7 等)。

5x5

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

进入

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

5x5

0 0 0 3 0  
0 0 0 3 0  
0 0 0 3 0  
0 0 0 3 0  
0 0 0 3 0 

进入

0 0 0 0 0  
0 0 0 0 3  
0 0 0 3 0  
0 0 3 3 0  
0 3 0 0 0  

【问题讨论】:

    标签: c# arrays matrix rotation


    【解决方案1】:

    这是我和朋友写的解决这个问题的代码:

    public static class ArrayExtensions
    {
        public static Point RoundIndexToPoint(int index, int radius)
        {
            if (radius == 0)
                return new Point(0, 0);
            Point result = new Point(-radius, -radius);
    
            while (index < 0) index += radius * 8;
            index = index % (radius * 8);
    
            int edgeLen = radius * 2;
    
            if (index < edgeLen)
            {
                result.X += index;
            }
            else if ((index -= edgeLen) < edgeLen)
            {
                result.X = radius;
                result.Y += index;
            }
            else if ((index -= edgeLen) < edgeLen)
            {
                result.X = radius - index;
                result.Y = radius;
            }
            else if ((index -= edgeLen) < edgeLen)
            {
                result.Y = radius - index;
            }
    
            return result;
        }
    
        public static T[,] Rotate45<T>(this T[,] array)
        {
            int dim = Math.Max(array.GetLength(0), array.GetLength(0));
    
            T[,] result = new T[dim, dim];
    
            Point center = new Point((result.GetLength(0) - 1) / 2, (result.GetLength(1) - 1) / 2);
            Point center2 = new Point((array.GetLength(0) - 1) / 2, (array.GetLength(1) - 1) / 2);
            for (int r = 0; r <= (dim - 1) / 2; r++)
            {
                for (int i = 0; i <= r * 8; i++)
                {
                    Point source = RoundIndexToPoint(i, r);
                    Point target = RoundIndexToPoint(i + r, r);
    
                    if (!(center2.X + source.X < 0 || center2.Y + source.Y < 0 || center2.X + source.X >= array.GetLength(0) || center2.Y + source.Y >= array.GetLength(1)))
                        result[center.X + target.X, center.Y + target.Y] = array[center2.X + source.X, center2.Y + source.Y];
                }
            }
            return result;
        }     
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试这个库:

      Math.NET Project 用于矩阵运算...http://numerics.mathdotnet.com/

      这段代码似乎也很有用:

      http://www.drunkenhyena.com/cgi-bin/view_net_article.pl?chapter=2;article=28#Rotation

      不要忘记 DirectX 托管和非托管命名空间和类。很多 还有很多好东西要检查。

      例如:

      Matrix..::.Rotate Method (Single, MatrixOrder)

      【讨论】:

      • 那些矩阵只有 4x4 或 3x3,我会试试 math.net,但我担心这种旋转过于具体
      • 这些是用于变换的旋转矩阵。完全不同的东西。
      【解决方案3】:

      我认为我们有这些规则:

      1. 将矩阵想象成一组“没有中心的框架或盒子”,它们相互之间就像“俄罗斯娃娃”。

      2. 位于一侧中心的元素(上/左/右/下)顺时针向最近的角移动。

      3. 拐角顺时针移向下一个中心。

      4. 既不是角也不是中心的元素移动到下一个点(顺时针),与它们当前的距离相同。

      我已经开始编写一些代码,但我认为这不是微不足道的,我还没有时间测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 2020-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多