Rotate non square matrix 180... Not working...

Discussion in 'C++' started by JustSomeGuy, Jun 3, 2004.

  1. JustSomeGuy

    JustSomeGuy Guest

    mx = Cols/2;
    if ((mx * 2) != Cols) // Odd.
    ++mx;
    }

    for (y1=0; y1 < Rows; ++y1)
    {
    y2 = Rows-1 - y1;

    for (x1=0; x1 < mx; ++x1)
    {
    x2 = Cols-1 - x1;

    p = t[y1][x1];
    t[y1][x1] = t[y2][x2];
    t[y2][x2] = p;
    }
    }


    This code doesn't seem to do the center column of
    a matrix whos dimensions are odd on the x dimensions...
    Can any one see why? I tried I don't see it...
     
    JustSomeGuy, Jun 3, 2004
    #1
    1. Advertising

  2. "JustSomeGuy" <> wrote...
    > mx = Cols/2;
    > if ((mx * 2) != Cols) // Odd.
    > ++mx;
    > }


    This closing curly brace seems misplaced. Besides, you could
    simply write

    mx = (Cols+1)/2;

    instead of three lines above, no?

    >
    > for (y1=0; y1 < Rows; ++y1)
    > {
    > y2 = Rows-1 - y1;
    >
    > for (x1=0; x1 < mx; ++x1)
    > {
    > x2 = Cols-1 - x1;
    >
    > p = t[y1][x1];
    > t[y1][x1] = t[y2][x2];
    > t[y2][x2] = p;
    > }
    > }
    >
    >
    > This code doesn't seem to do the center column of
    > a matrix whos dimensions are odd on the x dimensions...
    > Can any one see why? I tried I don't see it...


    You probably rotate the middle twice. Try doing it by
    hand and see where you screw up.

    Victor
     
    Victor Bazarov, Jun 3, 2004
    #2
    1. Advertising

  3. JustSomeGuy

    Howard Guest

    "JustSomeGuy" <> wrote in message
    news:FEwvc.654789$Ig.380269@pd7tw2no...
    > mx = Cols/2;
    > if ((mx * 2) != Cols) // Odd.
    > ++mx;
    > }
    >
    > for (y1=0; y1 < Rows; ++y1)
    > {
    > y2 = Rows-1 - y1;
    >
    > for (x1=0; x1 < mx; ++x1)
    > {
    > x2 = Cols-1 - x1;
    >
    > p = t[y1][x1];
    > t[y1][x1] = t[y2][x2];
    > t[y2][x2] = p;
    > }
    > }
    >
    >
    > This code doesn't seem to do the center column of
    > a matrix whos dimensions are odd on the x dimensions...
    > Can any one see why? I tried I don't see it...


    You're swapping the center column twice.

    Here's one solution:

    // rotate top half with bottom half
    int y1 = 0;
    int y2 = Rows-1;
    for (; y1 < y2; ++y1, --y2)
    {
    int x2 = Columns-1;
    // swap all columns
    for (int x1 = 0; x1 < Columns; ++x1, --x2)
    swap( t[y1,x1], t[y2,x2] );
    }
    // odd number of rows? rotate middle row
    if (y1 == y2)
    {
    int x2 = Columns-1;
    // only swap left with right (to prevent swapping twice)
    for (int x1 = 0; x1 < x2; ++x1, --x2)
    swap( t[y1,x1], t[y1,x2] );
    }

    -Howard

    "All programmers write perfect code.
    ....All other programmers write crap."

    "I'm never wrong.
    I thought I was wrong once,
    but I was mistaken."
     
    Howard, Jun 3, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,255
  2. RC
    Replies:
    1
    Views:
    912
    Bjoern Hoehrmann
    Aug 3, 2006
  3. =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=

    Vector, matrix, normalize, rotate. What package?

    =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=, Feb 27, 2007, in forum: Python
    Replies:
    5
    Views:
    6,326
  4. IanJSparks
    Replies:
    0
    Views:
    1,185
    IanJSparks
    Jan 10, 2008
  5. Leo Koolsbergen

    how to rotate an image 180 degrees

    Leo Koolsbergen, Dec 21, 2004, in forum: Javascript
    Replies:
    6
    Views:
    375
    Leo Koolsbergen
    Dec 22, 2004
Loading...

Share This Page