Jump to content

Array Rotation

- - - - -

  • Please log in to reply
8 replies to this topic

#1
bojjenclon

bojjenclon

    Newbie

  • Members
  • Pip
  • 8 posts
I'm trying to port some Python code to C++. I want to switch from PySFML to C++ SFML. I'm having a hard time rotating this array. Whenever an object is rotated, certain variables need to be "rotated" along with it. This was fairly simple in Python, but I'm running into issue with its C++ counterpart. I'm sure I'm probably missing something simple.

Here's the original Python code:

def Rotate( self, angle ):

	rot = angle / 90

	rot %= len( self.color )

	

	self.color = self.color[rot:] + self.color[:rot]

	self.sideType = self.sideType[rot:] + self.sideType[:rot]

	

	sf.Shape.Rotate( self, angle )


Here's the C++ code I have so far.

void Base::Rotate( int angle ) {

    if( angle < 0 ) {

        angle += 360;

    }


    int rot = angle / 90;

    rot %= 4;


    sf::Color colorTemp[4] = color;

    int sideTypeTemp[4];


    for( unsigned int i = 0; i < 4; i++ ) {

        sideTypeTemp[i] = sideType[i];

    }


    for( unsigned int i = rot; i < 4; i++ ) {

        color[i-rot] = colorTemp[i];

        sideType[i-rot] = sideTypeTemp[i];

    }

    for( unsigned int i = 0; i < rot; i++ ) {

        color[3-i] = colorTemp[i];

        sideType[3-i] = sideTypeTemp[i];

    }


    sf::Shape::Rotate( angle );

}



#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
int* slice(int* array, int size, int to, int from) {
    int* temp = new int[to + (size - from)];


    for (int i = 0; i < to; ++i)
        temp[i] = array[i];


    for (int i = from; i < size; ++i)
        temp[to++] = array[i];


    return temp;
}


int main() {
    int array[] = {
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    };


    int* copy = slice(array, 10, 2, 6);


    for (int i = 0; i < 2 + 10 - 6; ++i)
        std::cout << copy[i] << ", ";


    delete [] copy;


    return 0;
}
Sometimes expressing it in code seems to be easier. :P Just modify it so it suits your needs.

Also, there's a IRC #sfml channel on irc.boxbox.org for your SFML related questions.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
bojjenclon

bojjenclon

    Newbie

  • Members
  • Pip
  • 8 posts
Thank you for your help, but I'm still having an issue and its making me feel stupid. >.<

Here's some revised code:

#include <string>

#include <map>

#include <math.h>


int* slice( int* array, int size, int to, int from ) {

    int* temp = new int[ to + ( size - from ) ];


    for( int i = 0; i < to; ++i ) {

        temp[i] = array[i];

    }


    for( int i = from; i < size; ++i ) {

        temp[to++] = array[i];

    }


    return( temp );

}


int main() {

	int test[4] = { 1, 2, 3, 4 };


	int angle = 90;


	int rot = angle / 90;

	rot %= 4;


	int* testTemp1 = slice( test, 4, 0, rot );

	int* testTemp2 = slice( test, 4, rot-1, 3 );


    for( unsigned int i = 0; i < 4; i++ ) {

        if( i < rot ) {

            test[i] = testTemp2[i];

        }

        else {

            test[i] = testTemp1[i];

        }

    }


    //std::cout << std::endl;


    delete[] testTemp1;

    delete[] testTemp2;

}



#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
testTemp1 array has only 1 element. So in main function, in for loop you should get seg fault. I'm not really sure what you're trying to do here (for loop).

Also, slice function does this part:
self.color[rot:] + self.color[:rot]

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
bojjenclon

bojjenclon

    Newbie

  • Members
  • Pip
  • 8 posts
Yes, thank you so much! I got it now. I probably still did something wrong, but here's my final code:

void Base::Rotate( int angle ) {

    if( angle < 0 ) {

        angle += 360;

    }


    int rot = angle / 90;

    rot %= 4;


    int* sideTypeTemp1 = slice( sideType, 4, 0, rot );

    sf::Color* colorTemp1 = slice( color, 4, 0, rot );


    int sideTypeTemp2[4];

    sf::Color colorTemp2[4];


    for( unsigned int i = 0; i < 4; i++ ) {

        sideTypeTemp2[i] = sideType[i];

        colorTemp2[i] = color[i];

    }


    for( unsigned int i = 0; i < 4-rot; i++ ) {

        sideType[i] = sideTypeTemp1[i];

        color[i] = colorTemp1[i];

    }


    rot--;


    for( int i = rot; i >= 0; i-- ) {

        sideType[3-i] = sideTypeTemp2[rot-i];

        color[3-i] = colorTemp2[rot-i];

    }


    delete[] sideTypeTemp1;

    delete[] sideTypeTemp2;


    delete[] colorTemp1;

    delete[] colorTemp2;


    sf::Shape::Rotate( angle );

}



#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Calling delete/delete[] on variables/objects you didn't new/new[] is bad and will most likely lead to a crash.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
bojjenclon

bojjenclon

    Newbie

  • Members
  • Pip
  • 8 posts
So like this?


void Base::Rotate( int angle ) {

    if( angle < 0 ) {

        angle += 360;

    }


    int rot = angle / 90;

    rot %= 4;


    int* sideTypeTemp1 = slice( sideType, 4, 0, rot );

    sf::Color* colorTemp1 = slice( color, 4, 0, rot );


    int sideTypeTemp2[4];

    sf::Color colorTemp2[4];


    for( unsigned int i = 0; i < 4; i++ ) {

        sideTypeTemp2[i] = sideType[i];

        colorTemp2[i] = color[i];

    }


    for( unsigned int i = 0; i < 4-rot; i++ ) {

        sideType[i] = sideTypeTemp1[i];

        color[i] = colorTemp1[i];

    }


    rot--;


    for( int i = rot; i >= 0; i-- ) {

        sideType[3-i] = sideTypeTemp2[rot-i];

        color[3-i] = colorTemp2[rot-i];

    }


    delete[] sideTypeTemp1;

    delete[] colorTemp1;


    sf::Shape::Rotate( angle );

}



#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
If it's working like Python version and doesn't crash, I'd say this is it then. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
bojjenclon

bojjenclon

    Newbie

  • Members
  • Pip
  • 8 posts
Thank you very much for all your help! I've learned a lot from this. And I really appreciate how fast you've been with your responses. You win an internet! :P




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users