Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default 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. #3
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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. #4
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default 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. #6
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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 05:02 PM.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

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

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

  8. #8
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default 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. #11
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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. #12
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

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

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

  13. #13
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

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

    Quote Originally Posted by Hazza View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

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

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

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

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

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

    Quote Originally Posted by Hazza View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

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

    Quote Originally Posted by Hazza View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    Hazza is offline Member
    Join Date
    Oct 2012
    Posts
    64
    Rep Power
    0

    Default 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 LastLast

Similar Threads

  1. Help to rotate a pic 180 degrees (upside down)
    By Track13 in forum New To Java
    Replies: 7
    Last Post: 11-16-2012, 04:27 AM
  2. Math.atan --- To Degrees Help
    By GregoryNeal in forum New To Java
    Replies: 1
    Last Post: 03-07-2012, 06:54 AM
  3. Rotating an image 90 Degrees
    By cvillejin in forum New To Java
    Replies: 4
    Last Post: 04-03-2011, 07:56 PM
  4. Radians to Degrees issue?
    By Joey Gobert in forum Advanced Java
    Replies: 5
    Last Post: 12-15-2010, 08:41 PM
  5. help w/ conversion of degrees(CtoF, FtoC)
    By clemsontigers in forum New To Java
    Replies: 11
    Last Post: 11-08-2010, 09:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •