90 degree Rotation

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Jan 2006
Posts
15

## 90 degree Rotation

Hi, I'm trying to create a program that rotates a figure by 90 degrees clockwise. It is run in the command prompt, is supposed to output the new position of the figure. This program can be viewed as a foundation for tetris pieces. Here is an example of how it should look:

[***]
[-*-]
[-*-]

90 rotate

[--*]
[***]
[--*]

90 rotate

[-*-]
[-*-]
[***]

This is the result I get for the first 3 or 4 times the figure is rotated, but then I get random results after that. Could someone run my code and tell me what is going on?

public class Rotation
{
public static boolean[][] isBlockPresent = new boolean[3][3];
public static boolean[][] isMatrixBuffer = new boolean[3][3];

public static void main(String args[])
{
isBlockPresent[1][0] = true;
isBlockPresent[1][1] = true;
isBlockPresent[2][1] = true;
isBlockPresent[1][2] = true;

for(int r = 0; r<3; r++)
{
System.out.print("[");

for(int c = 0; c<3; c++)
{
if(isBlockPresent[c][r])
System.out.print("*");
else
System.out.print("-");
}

System.out.println("]");
}

for(int m = 0; m<5; m++)
{
System.out.println();
RotateClockwise();
}

}

public static void RotateClockwise()
{
for(int r = 0; r<3; r++)
{
for(int c = 0; c<3; c++)
isMatrixBuffer[c][r] = isBlockPresent[r][2 - c];
}

isBlockPresent = isMatrixBuffer;

for(int r = 0; r<3; r++)
{
System.out.print("[");

for(int c = 0; c<3; c++)
{
if(isBlockPresent[c][r])
System.out.print("*");
else
System.out.print("-");
}

System.out.println("]");
}
}
}

Thanks,
Ben.

2. Registered User
Join Date
Jan 2006
Posts
15
nvm, i solved it already. thx.

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise