ThatGuy343
ThatGuy343

Reputation: 2424

Rotate an array 2 more times?

I have been able to rotate an array of coordinates by reversing it, but I need the other 2 possible rotations and am not sure how to do it. Here is my current code, i print the coordinates as an example.

/**
 *  Visual representation of coords.
 *
 *   xxxxx
 *   xxxxx
 *   xx+xx
 *   xxxxx
 *   xxxxx
 * 
 *  + is [0,0]
 */

public static void main(String[] args) {
    List < String > array = getCoordinates(0, 0, 2);

    int i = 0;

    System.out.println("Rotation 1: ");

    for (String line: array) {
        if (i == 4) {
            System.out.println(line);
            i = -1;
        } else {
            System.out.print(line + " ");
        }
        i++;
    }

    System.out.println(" ");
    System.out.println("Rotation 2: ");

    Collections.reverse(array);
    i = 0;

    for (String line: array) {
        if (i == 4) {
            System.out.println(line);
            i = -1;
        } else {
            System.out.print(line + " ");
        }
        i++;
    }

}

And here is the getCoordinates method.

public static List < String > getCoordinates(int x, int z, int range) {

    List < String > ids = new ArrayList < String > ();

    for (int i = -range; i <= range; i++) {
        for (int j = -range; j <= range; j++) {
            int nx = x + i;
            int nz = z + j;
            ids.add("[" + nx + "," + nz + "]");
        }
    }

    return ids;
}

I'm sorry for the large chunk of code, but this is the only way I can best illustrate what i'm attempting.

EDIT Here is the output.

Rotation 1: 
[-2,-2] [-2,-1] [-2,0] [-2,1] [-2,2]
[-1,-2] [-1,-1] [-1,0] [-1,1] [-1,2]
[0,-2] [0,-1] [0,0] [0,1] [0,2]
[1,-2] [1,-1] [1,0] [1,1] [1,2]
[2,-2] [2,-1] [2,0] [2,1] [2,2]

Rotation 2: 
[2,2] [2,1] [2,0] [2,-1] [2,-2]
[1,2] [1,1] [1,0] [1,-1] [1,-2]
[0,2] [0,1] [0,0] [0,-1] [0,-2]
[-1,2] [-1,1] [-1,0] [-1,-1] [-1,-2]
[-2,2] [-2,1] [-2,0] [-2,-1] [-2,-2]

There are two other possible rotations here since its a square. Those are what am trying to get.

Here is a relevant image i found on the web, showing 3 rotations of a square grid.

enter image description here.

Upvotes: 1

Views: 206

Answers (2)

ThatGuy343
ThatGuy343

Reputation: 2424

If anyone happens to come across this for whatever reason, here is the code I used to rotate the array.

public static void main(String[] args) {
    List<String> array = getCoordinates(0,0,2);
    List<String> rotated = new ArrayList<String>();

    prnt(array); // original

    for(int b = 0;b < 5;b++){
        int index = 20+b;
        for(int a = 0;a < 5;a++){
            rotated.add(array.get(index));
            index -= 5;
        }
    }

    prnt(rotated); // 2nd rotation

    Collections.reverse(array);

    prnt(array); // 3rd rotation

    Collections.reverse(rotated);

    prnt(rotated); // 4th rotation
}

private static void prnt(List<String> rotated) {
    int i = 0;
    for(String line : rotated){
        if(i == 4){
            System.out.println(line);
            i = -1;
        }else{
            System.out.print(line + " ");
        } 
        i++;
    }
}

The output is.

# x x x x
x x x x x
x x x x x
x x x x x
x x x x x

x x x x #
x x x x x
x x x x x
x x x x x
x x x x x

x x x x x
x x x x x
x x x x x
x x x x x
x x x x #

x x x x x
x x x x x
x x x x x
x x x x x
# x x x x

Upvotes: 0

Sharon Ben Asher
Sharon Ben Asher

Reputation: 14328

Here is the logic of the rotations. Try to code it yourself...

rotating original 90deg right:

/**
 *  original
 *   wqxxx
 *   asxxx
 *   xx+xx
 *   xxxxx
 *   xxxxx
 * 
 *  rotated
 *   xxxaw
 *   xxxsq
 *   xx+xx
 *   xxxxx
 *   xxxxx
 */

// w: -2,-2 -> -2,2
// q: -2,-1 -> -1,2
// a: -1,-2 -> -2,1
// s: -1,-1 -> -1,1

rotation right logic: rotated row = original column, rotated column = original row *-1

rotating original 90deg left:

/**
 *  original
 *   wqxxx
 *   asxxx
 *   xx+xx
 *   xxxxx
 *   xxxxx
 * 
 *  rotated
 *   xxxxx
 *   xxxxx
 *   xx+xx
 *   qsxxx
 *   waxxx
 */

// w: -2,-2 -> 2,-2
// q: -2,-1 -> 1,-2
// a: -1,-2 -> 2,-1
// s: -1,-1 -> 1,-1

rotation left logic: rotated row = original column *-1, rotated column = original row

Upvotes: 1

Related Questions