Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 971333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:56:53+00:00 2026-05-16T02:56:53+00:00

How can I rotate a 2D rectangular array of integers that has odd number

  • 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 (3×3, 5×5, 7×7, etc.).

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 

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 

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 

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  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:56:54+00:00Added an answer on May 16, 2026 at 2:56 am

    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;
        }     
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.