# Thread: How to rotate the output of an array 90 degrees clockwise

1. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## How to rotate the output of an array 90 degrees clockwise

I have read in the string from a text file and divided it into individual characters and then stored these as elements in a 2D array. The data in the text file contains the information to be able to make a picture. When I output the picture it is rotated 90 degrees anticlockwise from what it should be, I was wondering what I have to do to rectify this?

2. ## Re: How to rotate the output of an array 90 degrees clockwise

Printing your rows/columns in the wrong order.

If you print it out with a nested loop, you need to reverse the row/col access in the 2d array in the innermost loop.

3. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

I still don't quite see how I can change it, the only way I have been able to change it so far is making it print upside down! Here is the code that when printed rotates the image 90 degrees anticlockwise:

for(int row=0;row<a.length;row++) {
for(int column=0;column<a[row].length;column++) {
if (a[row][column] == 'i') {
image.plot(row,column);

P.S. the plot method just plots a pixel on a blank grid.

4. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

Also how comes when I use the same loop to just output what is stored in the array (i.e. System.out.print) it is the correct way up but when I try and do it as a picture is is wrongly rotated?

5. ## Re: How to rotate the output of an array 90 degrees clockwise

So, when you read the data in, you're reading it one row at a time. If reading it into a 2D array designated:
Java Code:
`String[][] rowxCol = new String[3][3];`
You would load data into it one row at a time. Lets say our image was something like:

Java Code:
```***
*
*```
If we then printed it back out but crossed our row/column variables when printing like this:
Java Code:
```for(int row = 0; row < rowxCol.length; row++){
for(int col = 0; col < rowxCol[row].length; col++){
System.out.print(rowxCol[col][row]); // <--- right here (it should be [row][col]
}
System.out.println();
}```
In the above sample we get :

Java Code:
```**
* *
*```
Which is the same as the original input rotated counter clockwise 90 degrees.

In other words, either during the input part of your app, or during the printing, you have crossed your [row][col] index.

6. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

I understand what you are saying but I cannot see anywhere in my code that I have crossed rows with columns. In all loops row is the outer loop and column is the inner one and when mentioning any index of the array it is always [row][column]. I'll show you, for some reason:

public static void display(char a[][]) {
for(int row=0;row<a.length;row++) {
for(int column=0;column<a[row].length;column++) {
System.out.print(a[row][column]);
}
System.out.println();
}
}

prints what is in the array out just fine i.e. the right way up yet:

public static void displayImage(char a[][]) {
EasyGraphics picture = new EasyGraphics(656,824);

for(int row=0;row<a.length;row++) {
for(int column=0;column<a[row].length;column++) {
if (a[row][column] == 'i') {
Image.plot(row*8,column*8);
}
}
}
}

Produces an image that is rotated 90 degrees anticlockwise, and I cannot see how this can be, and why the second method rotates it when the first does not?
Last edited by Hazza; 11-16-2012 at 06:02 PM.

7. ## Re: How to rotate the output of an array 90 degrees clockwise

Probably this line:
Java Code:
`Image.plot(row*8,column*8);`

8. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

I did try to reverse the row and column in that line but it ended up displaying the image upside down!

9. ## Re: How to rotate the output of an array 90 degrees clockwise

How is the coordinate system interpreted by the Image.plot( ... ) method? Maybe it takes its arguments as (x,y) (column, row) while you pass them as (y,x) (row, column).

kind regards,

Jos

10. ## Re: How to rotate the output of an array 90 degrees clockwise

Jos makes a good point - most java stuff is cartesian lower right quad, meaning 0,0 is the top left of the screen. But other languages, frameworks, and perhaps even your package draw from the bottom left, or other corners.

11. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

Reading the documentation about the method it says that it sets a pixel at coordinates (x,y).

12. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

Yes the origin (0,0) point is at the bottom left.

13. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

Anyway I am going to try and fiddle around with it until it hopefully works thank you quad and jos I appreciate you helping me in finding where the problem was!

14. ## Re: How to rotate the output of an array 90 degrees clockwise

Originally Posted by Hazza
Reading the documentation about the method it says that it sets a pixel at coordinates (x,y).
There's the cause: x goes from left to right so it is a column coordinate; y in your Java code is a row coordinate.

kind regards,

Jos

15. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

Would this code output what I want it to output?

public static void displayPicture(char a[][]) {
EasyGraphics image = new EasyGraphics(656,824);
int rowCounterCounter = 102;

for(int row=0;row<a.length;row++) {
for(int column=0;column<a[row].length;column++) {
if (a[row][column] == 'i') {
image.plot(column*8,(row+rowCounterCounter)*8);
rowCounterCounter-=2;
}
}
}
}

17. ## Re: How to rotate the output of an array 90 degrees clockwise

Originally Posted by Hazza
Would this code output what I want it to output?

public static void displayPicture(char a[][]) {
EasyGraphics image = new EasyGraphics(656,824);
int rowCounterCounter = 102;

for(int row=0;row<a.length;row++) {
for(int column=0;column<a[row].length;column++) {
if (a[row][column] == 'i') {
image.plot(column*8,(row+rowCounterCounter)*8);
rowCounterCounter-=2;
}
}
}
}
How would we know? We don't have an image.plot( ... ) method; all we can deduce is that it uses the column coordinate as it's first parameter and the row coordinate as it's second parameter. Why don't you give it a try and see for yourself? I don't know why all that fiddling with the row coordinate is needed ...

kind regards,

Jos

18. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

I need to fiddle with the rows because when I plot it as just (column,row) it prints upside down. So I was thinking to swap the first row with the last, then the second with the second to last and then so on and so forth.

19. ## Re: How to rotate the output of an array 90 degrees clockwise

Originally Posted by Hazza
I need to fiddle with the rows because when I plot it as just (column,row) it prints upside down. So I was thinking to swap the first row with the last, then the second with the second to last and then so on and so forth.
Why don't you simply start plotting from the last row downwards? All you have to do is change the outer loop:

Java Code:
```for(int row= a.length; row-- > 0; )
...```
kind regards,

Jos

20. Member
Join Date
Oct 2012
Posts
64
Rep Power
0

## Re: How to rotate the output of an array 90 degrees clockwise

Jos I tried doing it your way but it still prints upside down!

Page 1 of 2 12 Last