Results 1 to 4 of 4

Thread: Swapping 2D Vector Pointer Axis

  1. #1
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Swapping 2D Vector Pointer Axis

    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/5832...r-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:
    Qt Code:
    1. (*vector_pointer)[y][x] //where [x][y] is the actual layout of the vector
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Swapping 2D Vector Pointer Axis

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Swapping 2D Vector Pointer Axis

    Maybe just use swap (qSwap, std::swap) to change the values of x and y if you have the other situation?

    Cheers,
    _

  4. #4
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Swapping 2D Vector Pointer Axis

    Quote Originally Posted by ChrisW67 View Post
    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?

    Qt Code:
    1. QVector< QVector<MapSpace> > temp;
    2. QVector< QVector<MapSpace> >* builder;
    3.  
    4. temp.resize(gamemap.grid_x);
    5. 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
    6. temp[i].resize(gamemap.grid_y); //gamemap.grid_y is the size of the y axis
    7. for (int i = 0; i <= gamemap.grid_y-1; i++)
    8. {
    9. MapSpace* temp2 = (*builder)[i].data(); //builder is already assigned to 2D matrix previously
    10. for (int j = 0; j <= gamemap.grid_x-1; j++)
    11. temp[i][j] = temp2[j];
    12. }
    13. builder = &temp; //reassigning builder to the newly transposed matrix
    To copy to clipboard, switch view to plain text mode 

    @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.
    Last edited by squeegedog; 29th March 2014 at 12:24. Reason: throwing in some comments

Similar Threads

  1. Pointer to 2D Vector
    By squeegedog in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2014, 03:54
  2. Replies: 0
    Last Post: 6th November 2012, 08:44
  3. QTableWidget swapping rows with widgets in cells
    By dnnc in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2007, 14:11
  4. vector iterator as pointer problem
    By Teerayoot in forum General Programming
    Replies: 3
    Last Post: 6th May 2007, 20:36
  5. QTableWidget row swapping and insertion
    By fritz in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2006, 04:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.