PDA

View Full Version : Swapping 2D Vector Pointer Axis



squeegedog
20th March 2014, 07:10
I have created several different 2D vectors which hold data, and using a single pointer, can pass a particular vector to a function that makes necessary modifications. Also see the attached URL for clarification if necessary

http://www.qtcentre.org/threads/58323-Pointer-to-2D-Vector

The problem is, that although all of the 2D vectors are laid out in the exact same manner with the same size specifications, etc., some of them are x*y in size and others are y*x in size, if that makes sense. For instance, I have the function working properly on a 12*5 matrix, but the 5*12 matrix does not properly modify in the function. When originally writing the function, I thought this was an easy work-around, by simply making x=y and y=x, but unfortunately, some of the necessary logic doesn't perform properly since it doesn't actually treat x as y and y as x.

I can fix the problem by simply creating another version of the code that operates upon the matrices that are laid out in the opposite fashion, but it seems redundant to create another 500 lines of code whilst having to go through and modify everything along the way when a simply translation of the way the pointer works would solve everything.

So, before going to that length to solve my issue, I decided to ask and see if someone knows a way to create my vector pointer in a manner like:

(*vector_pointer)[y][x] //where [x][y] is the actual layout of the vector

Sorry if this is a lot of typing, but last time I asked a question I wasn't specific enough and made solving a simple problem much more difficult than need be.

Thanks!

ChrisW67
20th March 2014, 09:14
You want to transpose the matrix. You can do this crudely by making an element by element copy into a new set of nested vectors.

anda_skoa
20th March 2014, 09:22
Maybe just use swap (qSwap, std::swap) to change the values of x and y if you have the other situation?

Cheers,
_

squeegedog
29th March 2014, 12:19
You want to transpose the matrix. You can do this crudely by making an element by element copy into a new set of nested vectors.

I had actually been trying to do this before I posted here. Its so hard to keep track of exactly what is going on when you have pointers to pointers to pointers so I couldn't tell if I was doing it right, etc. Ended up going out of town and just now getting back to working on this again, but here is the code that I've been trying to use to transpose the matrix, see anything that looks incorrect?



QVector< QVector<MapSpace> > temp;
QVector< QVector<MapSpace> >* builder;

temp.resize(gamemap.grid_x);
for (int i = 0; i <= gamemap.grid_x-1; i++) //this for loop just making the size of temp right. gamemap.grid_x is the size of the x axis, subtract 1 for 0th row
temp[i].resize(gamemap.grid_y); //gamemap.grid_y is the size of the y axis
for (int i = 0; i <= gamemap.grid_y-1; i++)
{
MapSpace* temp2 = (*builder)[i].data(); //builder is already assigned to 2D matrix previously
for (int j = 0; j <= gamemap.grid_x-1; j++)
temp[i][j] = temp2[j];
}
builder = &temp; //reassigning builder to the newly transposed matrix


@anda_skoa
I had originally built everything thinking that would work just fine, but the called function works in a column by column fashion that cannot properly execute when the x and y values are simply swapped. The rows of the flipped matrices need to be operated on one at a time, which cannot occur by simply swapping the x and y values.