#!/usr/local/bin/perl -w # # hexagon.p writes a pbm in hexagonal format given rectangular input # # usage: # /home/mackay/_courses/comput/newising_mc> hexagon.p < states/tmp ; xv states/hexagon.pgm # # see /home/mackay/_courses/comput/newising_mc/README2 # see also /home/mackay/tmp/ising/ryan $ryan = 0 ; $pbmplot = 1 ; $pbmfile = "states/hexagon.pgm" ; # $ybordertop = 1 ; $xborderleft = 1 ; $yborder=1 ; $xborder = 0 ; $grey = 1 ; $black = 0 ; $white = 2 ; $h=3 ; $w = 4 ; $dw = 2 ; # always half w $cross = 1 ; $square = 0 ; # overrides everything. $fat = 1 ; # if fat, increment xborder and yborder. below $bigger = 0 ; # go up to bigger size $maxwhite=2; $wrap = 1 ; # left right wrap around $reverse = 0 ; $verbose = 0 ; # eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # if ( $square ) { $xborderleft=0; $ybordertop=0; $xborder=0;$yborder=0; $wrap=0; $h = 1 ; $w = 1 ; $dw = 0 ; $cross = 0 ; $bigger = 0 ; } else { if ( $bigger ) { $h +=2 ; $w +=2 ;} if ( $fat ) { $xborder ++ ; $yborder ++ ; } } @a = <> ; if($ryan) { foreach $r ( 0..$#a ) { $a[$r] =~ s/\-/0 /g; $a[$r] =~ s/\#/1 /g; print $a[$r] if ($verbose) ; } } $rows=$#a + 1; $a[0] =~ s/^\s+//g ; # remove initial space @b = split( '\s+' , $a[0] ); $cols= $#b + 1 ; print STDERR "$rows rows, $cols cols\n" ; $pbmy = $h * $rows + $yborder + $ybordertop ; $pbmx = $w * $cols + $xborder + $xborderleft + $dw * ($rows -1); $P = $pbmy * $pbmx ; foreach $i ( 1 .. $P ) { $c[$i] = $grey ; } $i = 1 + $ybordertop * $pbmx ; $ii = 0 + $xborderleft ; foreach $row ( @a ) { $row =~ s/^\s+//g ; @b = split('\s+', $row) ; if ( $reverse ) { @b = reverse( @b ) ; } $j = $ii ; foreach $t ( @b ) { &doit ; $j += $w ; } if ( $wrap ) { foreach $t ( @b ) { if ( $j + $w -1 <= $pbmx - $xborder ) { &doit ; } $j += $w ; } $j = $ii - ( $#b + 1 ) * $w ; foreach $t ( @b ) { if ( $j >= 1 ) { &doit ; } $j += $w ; } } $i += $pbmx * $h ; $ii += $dw ; } if ( $pbmplot ) { # note I actually write a pgm file . open (PBM,"> $pbmfile"); print PBM "P2\n# CREATOR: hexagon.p DJCM 97 02 15\n" ; print PBM "$pbmx $pbmy\n" ; print PBM "$maxwhite\n" ; } if ( $pbmplot ) { foreach $i ( 1 .. $P ) { print PBM "$c[$i] " ; if ( !($i%$pbmx) ) { print PBM "\n" ;} } close ( PBM ) ; } print STDERR "\n# xv $pbmfile\n" ; sub doit { $s = ($t) ? $white : $black ; if ( $square ) { $c[$i+$j] = $s ; } else { $c[$i+$j+1] = $s ; if ( !$bigger ) { if ( $cross ) { $c[$i+$j+$pbmx] = $s ; $c[$i+$j+$pbmx+1] = $s ; $c[$i+$j+$pbmx+2] = $s ; $c[$i+$j+2*$pbmx+1] = $s ; } if ( $fat ) { $c[$i+$j+2] = $s ; $c[$i+$j+$pbmx+3] = $s ; $c[$i+$j+2*$pbmx+3] = $s ; $c[$i+$j+2*$pbmx+2] = $s ; $c[$i+$j+2*$pbmx] = $s ; $c[$i+$j+3*$pbmx+1] = $s ; $c[$i+$j+3*$pbmx+2] = $s ; } } else { $c[$i+$j+2] = $s ; $c[$i+$j+$pbmx+3] = $s ; $c[$i+$j+2*$pbmx+3] = $s ; $c[$i+$j+2*$pbmx+2] = $s ; $c[$i+$j+2*$pbmx] = $s ; $c[$i+$j+3*$pbmx+1] = $s ; $c[$i+$j+3*$pbmx+2] = $s ; $c[$i+$j+$pbmx] = $s ; $c[$i+$j+$pbmx+1] = $s ; $c[$i+$j+$pbmx+2] = $s ; $c[$i+$j+2*$pbmx+1] = $s ; } } }