将 2D 阵列旋转 45 度

发布于 2024-09-10 03:03:01 字数 720 浏览 3 评论 0原文

如何将具有奇数行的二维整数矩形数组旋转 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  

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.).

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  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

庆幸我还是我 2024-09-17 03:03:01

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

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

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;
    }     
}
很酷不放纵 2024-09-17 03:03:01

您可以尝试这个库:

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

此代码似乎也很有用:

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

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

例如:

矩阵..::.旋转方法(单次、矩阵顺序)

You can try this library:

Math.NET Project for matrices operations... http://numerics.mathdotnet.com/

This code appears to be useful too:

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

Don't forget the DirectX managed and unmanaged namespaces and classes. Lots
and lots of good stuff there to check.

For example:

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

掐死时间 2024-09-17 03:03:01

我认为我们有这些规则:

  1. 将矩阵想象为一组“没有中心的框架或盒子”,就像“俄罗斯娃娃”一样。

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

  3. 角点顺时针移动到下一个中​​心。

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

我已经开始编写一些代码,但我认为这并不简单,而且我还没有时间进行测试。

I think we have these rules:

  1. Imagine the matrix as a set of "frames or boxes without centers" within each other like "Russian dolls".

  2. Elements at the center of a side (top/left/right/bottom) move towards the nearest corner clockwise.

  3. Corners move towards the next center clockwise.

  4. Elements that are not corners nor centers move to the next spot (clockwise) that is the same distance from a corner as they currently are.

I've started writing some code but I don't think it's trivial and I haven't had time to test.

∞觅青森が 2024-09-17 03:03:01

您可以在 codesignal

You can see my solution for the matrix rotation in codesignal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文