旋转二维数组45度 [英] Rotate 2D Array by 45 degrees

查看:468
本文介绍了旋转二维数组45度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何旋转的整数的二维矩形阵列,通过45度具有奇数行?

因此​​,像

  INT [] myArray的=新的INT [,]
{
    {1,0,1},
    {0,1,0},
    {0,0,0},
}

  INT [] = rotatedArray新INT [,]
{
    {0,1,0},
    {0,1,1},
    {0,0,0},
}

为任何尺寸(3×3,5×5,7×7等)。通过这个公式 http://yfrog.com/n6matrix45p

5×5

  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

5×5

  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


解决方案

这是一个code。通过我和一个朋友写了解决这个:

 公共静态类ArrayExtensions
{
    公共静态点RoundIndexToPoint(INT指数,诠释半径)
    {
        如果(半径== 0)
            返回新点(0,0);
        点结果=新的点(-radius,-radius);        而(指数℃下)指数+ =半径* 8;
        指数=指数%(半径* 8);        INT edgeLen =半径* 2;        如果(指数< edgeLen)
        {
            result.X + =指数;
        }
        否则,如果((指数 - = edgeLen)LT; edgeLen)
        {
            result.X =半径;
            result.Y + =指数;
        }
        否则,如果((指数 - = edgeLen)LT; edgeLen)
        {
            result.X =半径 - 指数;
            result.Y =半径;
        }
        否则,如果((指数 - = edgeLen)LT; edgeLen)
        {
            result.Y =半径 - 指数;
        }        返回结果;
    }    公共静态T [,] Rotate45< T>(这件T [,]数组)
    {
        诠释暗淡= Math.Max​​(array.GetLength(0),array.GetLength(0));        T [,]结果=新的T [朦胧,朦胧]        点中心=新点((result.GetLength(0) - 1)/ 2,(result.GetLength(1) - 1)/ 2);
        点中心2 =新点((array.GetLength(0) - 1)/ 2,(array.GetLength(1) - 1)/ 2);
        对于(INT R = 0;为r =(点心 - 1)/ 2; R ++)
        {
            的for(int i = 0; I< = R * 8;我++)
            {
                点源= RoundIndexToPoint(I,R);
                点目标= RoundIndexToPoint(I + R,R);                如果((center2.X + source.X&下;!0 || center2.Y + source.Y℃,|| center2.X + source.X&GT = array.GetLength(0)|| center2.Y + source.Y&GT = array.GetLength(1)))
                    结果[center.X + target.X,center.Y + target.Y] =阵列[center2.X + source.X,center2.Y + source.Y];
            }
        }
        返回结果;
    }
}

How can I rotate a 2D rectangular array of integers that has odd number of rows by 45 degrees?

So something like

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

into

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

for any dimension (3x3, 5x5, 7x7, etc.). By this formula http://yfrog.com/n6matrix45p

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 

into

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 

into

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  

解决方案

This is a code written by me and a friend that solves this:

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;
    }     
}

这篇关于旋转二维数组45度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆