I was trying to explain random-dot stereograms to a friend today, and wrote a little script that generates simple ASCII stereograms as a stepping stone along the way. The code isn't terribly exciting, but the whole 'scalars floating in space' thing is kinda pretty:
+ $step+ $step + $step +
$Co$W $S $Co$W $S $Co$W $S $Co$W
$z $L $z $L $z $L $z
$W $L $W $L $W $L $W
+ + + +
The code (and a larger tableau) is below the fold. There's also a node about generating random-string stereograms from a height field here.
#!/usr/bin/perl
use Strict;
my $W = 18; # column width
my $H = 31; # column height
my $C = 3; # number of column repeats
my $L = 5; # number of layers
my $S = 4; # step between items
my @E = ();
seek (DATA,0,0);
for (<DATA>) {
push @E, /(\$\w+)/g; # harvest scalar names from the source
}
my @grid = (' 'x($W * $C))x$H; # build the basic array
for my $i (0..$C) { # add the registration marks
for my $j (0..($#grid / 5)) {
substr ($grid[ $j*5 ], $i*$W, 1) = '+';
}
}
for my $layer (1..$L) { # add items from back to front
my $step = $W - $layer; # set the offset for this layer
for (my $z=0 ; $z < $H ; $z+=2+rand $S) {
my $x = rand ($W);
my $y = rand ($H); # choose a random position/string
my $e = $E[ rand @E ];
for (my $pos=$x ; $pos<length($grid[$y]) ; $pos+=$step) {
substr ($grid[$y], $pos, length $e) = $e;
}
}
}
print join ("\n", @grid), "\n";
__DATA__
----- END OF FILE -----
+$W $W + $W + $W +
$j $j $j
$j $j $j
$grid $$eid $erid $egrid
+ + + +
$pos $posC $pos $C
$pos $pos $pos $pos
$H $W $W $W$H $W$W $W $H$$W $W $$W
+ + + +
$layer $elayer $e $layer $e $lay$e
$x $j $x $j $x $j $x
$pos $pos $pos $pos
+ $pos+ $pos + $pos $pos
$grid $C $grid $C $gridpos $C $grid $pos
$i $i $i $i
+ $step+ $step + $step +
$Co$W $S $Co$W $S $Co$W $S $Co$W
$z $L $z $L $z $L $z
$W $L $W $L $W $L $W
+ + + +
$z $H$H $z$H $H $H $H $H$z
$j$step $pos $j $ste$pos$H $j $s$pos $H $j
$j $j $j
$W $pos $W $pos $W $p$W
+ + + +