Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Rotate non square matrix 180... Not working...

Reply
Thread Tools

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

 
 
JustSomeGuy
Guest
Posts: n/a
 
      06-03-2004
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...


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-03-2004
"JustSomeGuy" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      06-03-2004
"JustSomeGuy" <(E-Mail Removed)> 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."



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIL rotate : Rotate By Shear / Paeth Rotation? IanJSparks Python 0 01-10-2008 04:50 PM
Vector, matrix, normalize, rotate. What package? =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?= Python 5 03-01-2007 05:57 PM
Rotate the graphics without rotate the text in SVG RC XML 1 08-03-2006 07:45 AM
Rotate a matrix of any size lovecreatesbeauty C Programming 8 07-26-2006 03:04 PM
SRT DIvision, Square root and reciprocal square root alghazo@siu.edu VHDL 0 05-27-2004 06:23 AM



Advertisments