0

Been stuck on this a few days now, I'm going out my mind. Essentially I have converted a 2D array of values (an image, I know there are easier ways to achieve this but I have explicit requirements) into a 1D array. I can rotate the 2D array with ease. I'm having trouble with the rotating of the 1D version of the array, and I think it's down to a single line of algorithm being incorrect. The code I'm using for rotating the array is:

cout << "\nTransfer from 2D to dynamic 1D array and print details\n\n";
myImage * p_myImage = new myImage[total];

    for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        int offset = width * y + x;
        p_myImage[offset].pixel = array2D[y][x];
        cout << p_myImage[offset].pixel << " ";
    }
  cout << "\n";
}

//new pointer to copy to
myImage * p_myImage2 = new myImage[total];
cout << "\nRotate Matrix through 1D array\n\n";

for (int x = 0; x < width; x++)
{
    for (int y = 0; y < height; y++)
    {
        int offset = height * x + y;
        //int offset = width * y + x ;
        p_myImage2[offset].pixel = p_myImage[height-1+x].pixel;
        cout << p_myImage2[offset].pixel << " ";
    }
  cout << "\n";
}
1
  • Sample input and output would be helpful. A description of what you think is wrong (beyond 'I think one line somewhere is wrong') or what you don't understand would improve this question. SO isn't a debugging service. Commented Nov 22, 2013 at 16:37

1 Answer 1

2

This should rotate it clockwise:

p_myImage2[offset].pixel = p_myImage[width * (height - 1 - y) + x].pixel;
Sign up to request clarification or add additional context in comments.

1 Comment

if it solves your problem, please set the answer as accepted. thank you

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.