open a ascii file and rotate the content 90 deg...

Discussion in 'Perl Misc' started by lyoute, Feb 2, 2004.

  1. lyoute

    lyoute Guest

    i got a set of files all looks like: (with fixed dimension 8x8)
    ..X..XO..
    X...XO..
    OXX.XO..
    OOOXXO..
    ....OO.O.
    ...O.....
    .........
    .........

    how can i produce output with 90 deg rotated:
    ..XOO....
    X.XO....
    ...XO.O..
    ....XO...
    XXXXO...
    OOOO....
    .....O...
    .........

    actually i got this done while i still storing the pattern in a
    @pattern[8][8].
    just doubt if i still can rotate it after the pattern was written into a
    file....
    lyoute, Feb 2, 2004
    #1
    1. Advertising

  2. lyoute <> wrote:

    > how can i produce output with 90 deg rotated:


    > actually i got this done while i still storing the pattern in a
    > @pattern[8][8].
    > just doubt if i still can rotate it after the pattern was written into a
    > file....



    Read it from the file into an 8x8 matrix and rotate it the
    way you are already doing it.

    What problem do you see with that?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 2, 2004
    #2
    1. Advertising

  3. lyoute

    lyoute Guest

    i see. so there are ways to read a file char by char?

    Tad McClellan wrote:

    > lyoute <> wrote:
    >
    >
    >>how can i produce output with 90 deg rotated:

    >
    >
    >>actually i got this done while i still storing the pattern in a
    >>@pattern[8][8].
    >>just doubt if i still can rotate it after the pattern was written into a
    >> file....

    >
    >
    >
    > Read it from the file into an 8x8 matrix and rotate it the
    > way you are already doing it.
    >
    > What problem do you see with that?
    >
    >
    lyoute, Feb 2, 2004
    #3
  4. lyoute <> writes:

    > i got a set of files all looks like: (with fixed dimension 8x8)
    > .X..XO..
    > X...XO..
    > OXX.XO..
    > OOOXXO..
    > ...OO.O.
    > ..O.....
    > ........
    > ........
    >
    > how can i produce output with 90 deg rotated:
    > .XOO....
    > X.XO....
    > ..XO.O..
    > ...XO...
    > XXXXO...
    > OOOO....
    > ....O...
    > ........


    That is not rotation, that is reflection.

    #!/usr/bin/perl
    use strict;
    use warnings;
    chomp ( my @data = <> );
    my $count;
    do {
    $count = 0;
    for ( @data ) {
    if ( s/^(.)// ) {
    print $1;
    $count ++;
    } else {
    print ' ';
    }
    }
    print "\n";
    } while $count;

    __END__

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Feb 2, 2004
    #4
  5. [ Please put your replies below the text you are replying to.
    Chronology restored. ]

    Also sprach lyoute:
    > Tad McClellan wrote:
    >
    >> lyoute <> wrote:
    >>
    >>
    >>>how can i produce output with 90 deg rotated:

    >>
    >>
    >>>actually i got this done while i still storing the pattern in a
    >>>@pattern[8][8].
    >>>just doubt if i still can rotate it after the pattern was written into a
    >>> file....

    >>
    >>
    >>
    >> Read it from the file into an 8x8 matrix and rotate it the
    >> way you are already doing it.
    >>
    >> What problem do you see with that?


    > i see. so there are ways to read a file char by char?


    Yes, sure. You can use getc() for that. It's not used often in Perl
    though since there are usually better ways to do it. I'd store the 8x8
    picture in one scalar variable and process them character-wise. For
    instance that way:

    use constant NUM_COLS => 8;

    my $pic = do { local $/; <DATA> };
    $pic =~ tr/\n//d;

    my @rot;
    my $i = 0;
    while (my $c = substr $pic, $i, 1) {
    $rot[$i++ % NUM_COLS] .= $c;
    }
    my $rotated = join "\n", @rot;

    __DATA__
    .X..XO..
    X...XO..
    OXX.XO..
    OOOXXO..
    ...OO.O.
    ..O.....
    ........
    ........

    This works unchanged even for pictures with more (or less) than 8 rows.
    I used NUM_COLS because that way you can easily make it work for input
    pictures with a different number of columns.

    There are variations on the above, such as using split() instead of
    substr(). That's mostly a matter of personal preferences.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Feb 2, 2004
    #5
  6. lyoute

    Matt Garrish Guest

    "lyoute" <> wrote in message
    news:401e2e12$-cable.com...
    >
    > i see. so there are ways to read a file char by char?
    >


    The following should at least give you a start:

    my @array;

    while (my $line = <DATA>) {

    chomp($line);
    die if length($line) > 8;
    die if $. > 8;

    my $i = $. - 1;

    foreach my $char ($line =~ /(.)/g) {

    push @{$array[$i]}, $char;

    }

    }


    __DATA__
    ..X..XO..
    X...XO..
    OXX.XO..
    OOOXXO..
    ....OO.O.
    ...O.....
    .........
    .........
    Matt Garrish, Feb 2, 2004
    #6
  7. lyoute

    kz Guest

    "lyoute" <> wrote in message
    news:-cable.com...
    > i got a set of files all looks like: (with fixed dimension 8x8)

    [snip]
    > actually i got this done while i still storing the pattern in a
    > @pattern[8][8].
    > just doubt if i still can rotate it after the pattern was written into a
    > file....


    I guess the matrix approach is the most flexible thing, you can rotate,
    mirror, flip things just playing with the indices. Comment/uncomment lines
    at your ease and compare the results.
    I illustrated it step-by-step but one might want to code it more
    efficiently. Works for any NxN matrix.

    HTH,

    Zoltan

    use strict;
    use warnings;
    my ($line,@arr);
    my $ctr = 0;
    while (my $line = <DATA>) {
    chomp($line);
    $arr[$ctr++] = $line; }

    # no transformation
    foreach my $xc (0..$#arr) {
    foreach my $yc (0..$#arr) {
    # print substr($arr[$xc],$yc,1); # no transformation
    print substr($arr[$yc],$xc,1); # rotate 90 degrees
    # print substr($arr[$xc],$#arr-$yc,1); # flip horizontally
    # print substr($arr[$#arr-$xc],$yc,1); # flip vertically, etc
    }
    print "\n";
    }

    __DATA__
    ..X..XO..
    X...XO..
    OXX.XO..
    OOOXXO..
    ....OO.O.
    ...O.....
    .........
    .........
    kz, Feb 2, 2004
    #7
  8. Brian McCauley wrote:
    > lyoute <> writes:
    >
    >
    >>i got a set of files all looks like: (with fixed dimension 8x8)
    >>.X..XO..
    >>X...XO..
    >>OXX.XO..
    >>OOOXXO..
    >>...OO.O.
    >>..O.....
    >>........
    >>........
    >>
    >>how can i produce output with 90 deg rotated:
    >>.XOO....
    >>X.XO....
    >>..XO.O..
    >>...XO...
    >>XXXXO...
    >>OOOO....
    >>....O...
    >>........

    >
    >
    > That is not rotation, that is reflection.
    >


    No, it's not even reflection. Danged if I can figure out *what*
    it's supposed to be. Any rotation or reflection would move
    those two Xs in the top left away, because there isn't anything
    like them anywhere else in the matrix. Nowhere else is there

    ..X
    X.

    or

    X.
    ..X

    Chris Mattern
    Chris Mattern, Feb 2, 2004
    #8
  9. lyoute

    Paul Lalli Guest

    On Mon, 2 Feb 2004, Chris Mattern wrote:

    > Brian McCauley wrote:
    > > lyoute <> writes:
    > >
    > >
    > >>i got a set of files all looks like: (with fixed dimension 8x8)
    > >>.X..XO..
    > >>X...XO..
    > >>OXX.XO..
    > >>OOOXXO..
    > >>...OO.O.
    > >>..O.....
    > >>........
    > >>........
    > >>
    > >>how can i produce output with 90 deg rotated:
    > >>.XOO....
    > >>X.XO....
    > >>..XO.O..
    > >>...XO...
    > >>XXXXO...
    > >>OOOO....
    > >>....O...
    > >>........

    > >
    > >
    > > That is not rotation, that is reflection.
    > >

    >
    > No, it's not even reflection. Danged if I can figure out *what*
    > it's supposed to be. Any rotation or reflection would move
    > those two Xs in the top left away, because there isn't anything
    > like them anywhere else in the matrix. Nowhere else is there
    >
    > .X
    > X.
    >
    > or
    >
    > X.
    > .X
    >


    It is reflection, but it's not reflection about the X or Y axis. Rather,
    it's reflection about the line x=y, which is just odd...
    Paul Lalli, Feb 2, 2004
    #9
  10. Paul Lalli wrote:
    > On Mon, 2 Feb 2004, Chris Mattern wrote:
    >
    >
    >>Brian McCauley wrote:
    >>
    >>>lyoute <> writes:
    >>>
    >>>
    >>>
    >>>>i got a set of files all looks like: (with fixed dimension 8x8)
    >>>>.X..XO..
    >>>>X...XO..
    >>>>OXX.XO..
    >>>>OOOXXO..
    >>>>...OO.O.
    >>>>..O.....
    >>>>........
    >>>>........
    >>>>
    >>>>how can i produce output with 90 deg rotated:
    >>>>.XOO....
    >>>>X.XO....
    >>>>..XO.O..
    >>>>...XO...
    >>>>XXXXO...
    >>>>OOOO....
    >>>>....O...
    >>>>........
    >>>
    >>>
    >>>That is not rotation, that is reflection.
    >>>

    >>
    >>No, it's not even reflection. Danged if I can figure out *what*
    >>it's supposed to be. Any rotation or reflection would move
    >>those two Xs in the top left away, because there isn't anything
    >>like them anywhere else in the matrix. Nowhere else is there
    >>
    >>.X
    >>X.
    >>
    >>or
    >>
    >>X.
    >>.X
    >>

    >
    >
    > It is reflection, but it's not reflection about the X or Y axis. Rather,
    > it's reflection about the line x=y, which is just odd...


    Yes, Brian pointed it out to me in email. I see it now. Actually,
    with standard Cartesian coordinates (positive being up and right),
    it's the line y = -x.

    Chris Mattern
    Chris Mattern, Feb 2, 2004
    #10
  11. lyoute

    Rich Guest

    Brian McCauley <> wrote in message news:<>...
    > lyoute <> writes:
    >
    > > i got a set of files all looks like: (with fixed dimension 8x8)
    > > .X..XO..
    > > X...XO..
    > > OXX.XO..
    > > OOOXXO..
    > > ...OO.O.
    > > ..O.....
    > > ........
    > > ........
    > >
    > > how can i produce output with 90 deg rotated:
    > > .XOO....
    > > X.XO....
    > > ..XO.O..
    > > ...XO...
    > > XXXXO...
    > > OOOO....
    > > ....O...
    > > ........

    >
    > That is not rotation, that is reflection.


    Yes - all that has been done are interchange the references to rows
    and columns. Like a matrix transpose.
    Rich, Feb 2, 2004
    #11
  12. Paul Lalli <> wrote:

    > On Mon, 2 Feb 2004, Chris Mattern wrote:
    >
    >> Brian McCauley wrote:
    >> > lyoute <> writes:
    >> >
    >> >
    >> >>i got a set of files all looks like: (with fixed dimension 8x8)
    >> >>.X..XO..
    >> >>X...XO..
    >> >>OXX.XO..
    >> >>OOOXXO..
    >> >>...OO.O.
    >> >>..O.....
    >> >>........
    >> >>........
    >> >>
    >> >>how can i produce output with 90 deg rotated:
    >> >>.XOO....
    >> >>X.XO....
    >> >>..XO.O..
    >> >>...XO...
    >> >>XXXXO...
    >> >>OOOO....
    >> >>....O...
    >> >>........
    >> >
    >> >
    >> > That is not rotation, that is reflection.
    >> >

    >>
    >> No, it's not even reflection. Danged if I can figure out *what*
    >> it's supposed to be. Any rotation or reflection would move
    >> those two Xs in the top left away, because there isn't anything
    >> like them anywhere else in the matrix. Nowhere else is there
    >>
    >> .X
    >> X.
    >>
    >> or
    >>
    >> X.
    >> .X
    >>

    >
    > It is reflection, but it's not reflection about the X or Y axis. Rather,
    > it's reflection about the line x=y, which is just odd...


    [y=-x, as Chris said]

    What's odd about it? As Rich pointed out, it's just the transpose of the
    matrix. I could write my own if I wanted to waste time, but it's easier to
    grab Math::Matrix and modify (a copy of) the transpose sub. (You can't use
    Math::Matrix to print it because the as_string method prints everything as
    a float.)

    Transposing doesn't even require the matrix to be square....


    use strict;
    use warnings;

    my @matrix;
    while (<DATA>) {
    chomp;
    push @matrix, [ split // ];
    }

    # stolen from Math::Matrix
    my @trans;
    push @trans, [] for @matrix;
    for my $row (@matrix) {
    my $m=0;
    for my $col (@{$row}) {
    push @{$trans[$m++]}, $col;
    }
    }

    print "Before:\n", matrix_text(\@matrix);
    print "After:\n", matrix_text(\@trans);

    sub matrix_text {
    my ($mref) = @_;
    my $text;
    for my $row (@$mref) {
    $text .= join('',@$row) . "\n";
    }
    return $text;
    }

    __DATA__
    ..X..XO..
    X...XO..
    OXX.XO..
    OOOXXO..
    ....OO.O.
    ...O.....
    .........
    .........
    David K. Wall, Feb 3, 2004
    #12
    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. msorion
    Replies:
    5
    Views:
    2,810
    Jacqui or (maybe) Pete
    Jul 22, 2003
  2. RC
    Replies:
    1
    Views:
    854
    Bjoern Hoehrmann
    Aug 3, 2006
  3. TOXiC
    Replies:
    5
    Views:
    1,216
    TOXiC
    Jan 31, 2007
  4. still just me

    &deg; - is it dependable?

    still just me, Nov 19, 2007, in forum: HTML
    Replies:
    4
    Views:
    3,859
    still just me
    Nov 20, 2007
  5. IanJSparks
    Replies:
    0
    Views:
    1,127
    IanJSparks
    Jan 10, 2008
Loading...

Share This Page