3

Lets start out with an simple 16 x 16 array of ints.
How would I insert the 'SomeValue' into the array in a 90 degree clockwise order.

int[] image = new int[16 * 16];

for (int x = 0; x < 16; x++)
{
    for (int y = 0; y < 16; y++)
    {
        int someValue = x * y;

        // This is the line I think is wrong
        image[x + (y * 16)] = someValue; 
    }
}

The result should be like the Rotated Array below.

Normal order:
0, 1, 2,
3, 4, 5,
6, 7, 8,

Rotated Clockwise:
6, 3, 0,
7, 4, 1,
8, 5, 2,

5
  • 1
    Your code does not compile, z is not declared. You should also simplify your life by using a 2 dimensional array like int[,] image = new int[16, 16]; Commented Nov 4, 2010 at 21:51
  • What's z? Do you want to rotate the matrix or insert values into it? Those are two different things. Commented Nov 4, 2010 at 21:51
  • Woops, changed the z for a y, Hehe my fault ;) Commented Nov 4, 2010 at 21:53
  • now it compiles, what do you expect the code to do? Generate the "Rotated Clockwise" array? Commented Nov 4, 2010 at 21:56
  • I want to insert the 'SomeValue' in the image array in a way the image becomes rotated 90 degrees Clockwise. Commented Nov 4, 2010 at 22:00

3 Answers 3

6

Are you looking for something like this?

0 0 0 1 1 1 2 2 2   x
0 1 2 0 1 2 0 1 2   y
= = = = = = = = =
6 3 0 7 4 1 8 5 2   m*(m-1-y)+x

for m=3.


const int m = 16;
int[] image = new int[m * m];

for (int x = 0; x < m; x++)
{
    for (int y = 0; y < m; y++)
    {
        int someValue = x * y;

        image[m*(m-1-y)+x] = someValue; 
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

And what about rectangle arrays? what if instead of [16*16] we need to rotate a [5*2] array ?
For rectangles: int[] copy = new int[source.Length]; int i = 0; for(int x=0; x < width; x++) { for(int y=height-1; y >= 0; y--) {copy[i++] = source[y * width + x];}}. Note that you now assume width becomes height and vice versa in the "copy" array
2

Follow @Albin Sunnanbos suggestion and use a two-dimensional array. Then have a look at this related question.

2 Comments

Do you know how to rotate it in a counter clockwise way??
My sense of symmetry tells me to use [3-j,i] instead of [j,3-i]. In your case it's 15 instead of 3.
1

If you want to generate the rotated array you can do like this

int[,] image = new int[16 , 16];

int current = 0;
for (int x = 15; x >= 0; x--)
{
    for (int y = 0; y < 16; y++)
    {
        image[x, y] = current;
        current++;
    }
}

// Output

for (int y = 0; y < 16; y++)
{
    for (int x = 0; x < 16; x++)
    {
        Console.Write(image[x,y] + ", ");
    }
    Console.WriteLine();
}

3 Comments

Well it outputs in a two dimensional array but I can convert that easily :D
The two dimensional array is not a bug, it is a feature :-) (it improves the readability and intent of the code)
You're right! The two dimensional array actual makes it easier :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.