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

L

lyoute

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....
 
T

Tad McClellan

lyoute said:
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?
 
L

lyoute

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

Tad said:
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?
 
B

Brian McCauley

lyoute said:
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\\
 
T

Tassilo v. Parseval

[ Please put your replies below the text you are replying to.
Chronology restored. ]

Also sprach lyoute:
Tad said:
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
 
M

Matt Garrish

lyoute said:
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.....
.........
.........
 
K

kz

lyoute said:
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.....
.........
.........
 
C

Chris Mattern

Brian said:
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
 
P

Paul Lalli

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...
 
C

Chris Mattern

Paul said:
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
 
R

Rich

Brian McCauley said:
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.
 
D

David K. Wall

Paul Lalli said:
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.....
.........
.........
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top