Jump to content
  • Advertisement
Sign in to follow this  
tppramod

Char[] Array - How to Rotate the contents?

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Below given is my Pacman image in a char array 14x14. I now want to rotate this for Down, Left and Right depending upon its direction (Default direction is UP). so i am looking for any best possible code for the rotation. Color values: 1=Transparent, 14=Yellow. I am programming in Turbo C V.3.0. char Pacman[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,14,-1,-1,-1,-1,-1,-1,-1,14,-1,-1, -1,-1,14,14,-1,-1,-1,-1,-1,-1,-1,14,14,-1, -1,-1,14,14,14,-1,-1,-1,-1,-1,14,14,14,-1, -1,14,14,14,14,-1,-1,-1,-1,-1,14,14,14,14, -1,14,14,14,14,14,-1,-1,-1,14,14,14,14,14, -1,14,14,14,14,14,-1,-1,-1,14,14,14,14,14, -1,14,14,14,14,14,14,-1,14,14,14,14,14,14, -1,14,14,14,14,14,14,-1,14,14,14,14,14,14, -1,-1,14,14,14,14,14,14,14,14,14,14,14,-1, -1,-1,14,14,14,14,14,14,14,14,14,14,14,-1, -1,-1,-1,14,14,14,14,14,14,14,14,14,-1,-1, -1,-1,-1,-1,-1,14,14,14,14,14,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,

Share this post


Link to post
Share on other sites
Advertisement
The best way of doing it is probably to have another 3 arrays with the image in the other positions, and not align the image during the game. Still if you want to do it...


unsigned int i, j;
unsigned int sizex=14, sizey=14;
int temp_packman[sizex*sizey];

for(i=0;i<sizey;++i)
{
for(j=0;j<sizex;++j)
{
//this one rotates to the right
temp_packman[(j*sizex)+(sizex-i-1)]=packman[(sizex*i)+j];

//this one rotates to the left
//temp_packman[((sizex-j-1)*sizex)+i]=packman[(sizex*i)+j];
}
}

memcpy(packman, temp_packman, sizex*sizey);

Untested.

[Edited by - xor on May 24, 2005 4:53:59 AM]

Share this post


Link to post
Share on other sites
its in Mode 13h under DOS. Two base images are there. One is start-eating mode and and another is half-eaten mode. if i use char[] arrays then i have to set 8 char[] arrays in total. for that matter i want to use rotation algorithm.

regards,

pramod

Share this post


Link to post
Share on other sites
i wrote it, so i'll post it. if you need to make it modular, i hope you can do this, otherwis eyour in over your head :)

it SHOULD work with any size and can be made to rotate more than one rotation at a go, by using a loop around the main rotation code. hope it helps.

#include "stdafx.h"

#include <iostream>

using namespace std;

#define LAST_ROW 4
#define LAST_COL 4

int _tmain(int argc, _TCHAR* argv[])
{
//define the original array
char orig_array[LAST_ROW][LAST_COL]={ 'a','b','c','d',
'e','f','g','h',
'i','j','k','l',
'm','n','o','p'};

//define an empty array to store our rotated data
char rot_array[LAST_ROW][LAST_COL];

//print the original array
cout << "Here is the original data" << endl << endl;
for (int row = 0; row < LAST_ROW ;row ++)
{
for(int col = 0; col < LAST_COL; col ++)
{
cout << orig_array[row][col];
}
cout << endl;
}
cout << endl;
//rotate it
cout << "Rotating the data..." << endl << endl;
for (int row = 0; row < LAST_ROW ;row ++)
{
for(int col = 0; col < LAST_COL; col ++)
{
rot_array[row][col] = orig_array[LAST_COL-1-col][row];
}
}

//print the rotated array
cout << "Here is the rotated data" << endl << endl;
for (int row = 0; row < LAST_ROW ;row ++)
{
for(int col = 0; col < LAST_COL; col ++)
{
cout << rot_array[row][col];
}
cout << endl;

}
cout << endl << endl;
//wait
char a;
cin >> a;
}

Share this post


Link to post
Share on other sites
Hi XOR,

wonderful. its working!!!.... actually i have put your formula in excel and tried in advance and its working. my actual code for rotation was longer but yours just in one line - thats great!!. basically i am slightly weak in maths. how u quickly formed the formula? its really impressing. thanks a tonn for your help. i am also thankful to jjcosgrove for his help too.

thanks once again.

regards,

tppramod
INDIA

Share this post


Link to post
Share on other sites
Seems complicated because i didn't use bidimentional arrays, but if you take a look at jjcosgrove's example you'll see it's simple.

Quote:
rot_array[row][col] = orig_array[LAST_COL-1-col][row];

Share this post


Link to post
Share on other sites
one small clarification. can shift operator be used instead of multiplication in this case, so that it can be more faster. if so, how to do that? i have heard about shift operator that it is faster.

Pramod

Share this post


Link to post
Share on other sites
i believe that a shift operator is SUPPOSED to be faster, because it mimicks the natural behaviour of the processors in-built instruction set (like machine code or ASM code).

I personally like programming for clarity and leave it to the compiler to optimize my code. this is not always applicable but for the simple things it should be. you could either slap some assembler in your C++ code to do the maths slightly optimized (although probably less portable), or you could just use the c++ shift (>> or <<) operators.

int Num = 8; // value is 8

Num >> 2; // value is 2

you would need to make a equation to shift the right amount for a particular operation, depending on how much of the routime you wish to try and implement using shift.

:)

Share this post


Link to post
Share on other sites
Like jjcosgrove said yeah you can use the shift, but the sizex must be a power of 2, like 4, 8, or in this case 16, as it's closer to 14, the original size. To multiply a number by 16 you just need to shift it left 4 spaces. So the code would look something like...

temp_packman[(j<<4)+(sizex-i-1)]=packman[(i<<4)+j];

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!