[LON-CAPA-cvs] cvs: modules /raeburn waterimages.pl

raeburn lon-capa-cvs@mail.lon-capa.org
Tue, 14 Oct 2003 19:13:52 -0000


raeburn		Tue Oct 14 15:13:52 2003 EDT

  Added files:                 
    /modules/raeburn	waterimages.pl 
  Log:
  Script to generate randomized versions of distribution of water molecules and ions in solid, liquid and gaseous states. Created for use in isp203 by D. Sibley.
  
  

Index: modules/raeburn/waterimages.pl
+++ modules/raeburn/waterimages.pl
#!/usr/bin/perl
use GD;

# Setting random seed
srand (time() ^ ($$ + ($$  << 15)) );

# Global parameters
my $framewidth = 360;
my $frameheight = 270;
my $leftindent = 20;
my $rightindent = 20;
my $topindent = 20;
my $botindent = 20;
my $rowspace = 20;
my $colspace = 20;

my @environments = ("Ice in a glacier","Sea Water","Surface Water","Water within clouds","Water vapor");
my %correct_img = (
             0 => 0,
             1 => 2,
             2 => 4,
             3 => 5,
             4 => 6
             );
open(PROB,">./waterimages_xml.problem");

my $output = qq|<problem>
<allow src="/res/msu/isp203/chap10/problems/graphics/waterimages_*.gif" />
<randomlist show="1">
|;


my $total = 20;
  
for (my $j=0; $j<$total; $j++)
{

# Choose environment

  my $env = int(rand(5));
  my $correctone; 

# Choose image to leave out.
  my $dropimg;
  if ($env == 0)
  {
    $dropimg = 2 + int(rand(4));
  }
  else
  {
    $dropimg = int(rand(7));
    if ($env == 1)
    {
      while (($dropimg == 2) || ($dropimg == 3))
      {
        $dropimg = int(rand(6));
      }
    }
    elsif ($env > 2)
    {
      while (($dropimg == 5) || ($dropimg == 6))
      {
        $dropimg = int(rand(7));
      }
    }
  }
  
  my $imagename = "waterimages_".$j.".gif";
  open(FILE,">sibley/waterimages_$j.png");
# create a new image
  $im = new GD::Image(780,870);
    
# allocate some colors
  my $white = $im->colorAllocate(255,255,255);
  my $black = $im->colorAllocate(0,0,0);
  my $blue = $im->colorAllocate(0,102,255);
  my @colors = ($black,$white);

# make the background transparent and interlaced
  $im->transparent($white);
  $im->interlaced('true');
# Put a black frame around the picture
  $im->rectangle(0,0,779,869,$black);

  my $imgcount = 0;
  my %imgid = ();
  my @shortletts = ("a","b","c","d","e","f");
  my @letts = ("(a)","(b)","(c)","(d)","(e)","(f)");
  my @picked = ();

  for (my $row=0; $row<3; $row++)
  {
    for (my $col=0; $col<2; $col++)
    { 
      my $ytop = $topindent+($colspace+$frameheight)*$row;
      my $ybot = $ytop + $frameheight;
      my $xleft = $leftindent+($rowspace+$framewidth)*$col;
      my $xright = $xleft + $framewidth;
      $im->string(gdMediumBoldFont,$xleft+20,$ytop+20,$letts[$imgcount],$blue);
      
      my $num;
      if ( ($imgcount == 4) && ($env == 2) && (!grep/^4$/,@picked) )
      {
        $num = 4;
      }
      else
      { 
        $num = int(rand(7));
        while ( (grep/^$num$/,@picked) || ($num == $dropimg))
        { 
          $num = int(rand(7));
        }
      }
      if ($num == $correct_img{$env})
      {
        $correctone = $imgcount;
      }
      push @picked,$num;
      my $file = "waterimage".$num.".png";
      open(WATERFILE,"<images/$file");
      my $img = GD::Image->newFromPng(\*WATERFILE);
      $img->transparent($img->getPixel(0,0));
      $im->copy($img,$xleft,$ytop,0,0,360,270);
      close(WATERFILE);
      
      $imgcount ++;
    }
  }

# Convert the image to PNG and print it on standard output
# make sure we are writing to a binary stream
  binmode FILE;
  print FILE $im->png;
  close(FILE);

  $output .= qq| 
<radiobuttonresponse max="6" randomize="no">
<startouttext />
<table>
<tr>
<td>
<img src="../images/$imagename" />
</td>
</tr>
</table>
<br/>
The images above show several different arrangements of water molecules (black circles) and dissolved solids (white circles). 
In this question you must choose which of the images best represents the expected arrangement and concentration of water molecules +/- dissolved solids 
in the following environment: |;
 $output .= $environments[$env]."
<endouttext />
 <foilgroup>
 ";
  for (my $k=0; $k<$imgcount; $k++)
  {
    my $foilname = "foil".$shortletts[$k];
    if ($k == $correctone)
    {
      $output .= qq|<foil value="true" name="$foilname">\n|;
    }
    else
    {
      $output .= qq|<foil value="false" name="$foilname">\n|;
    }
    $output .= qq|<startouttext />$letts[$k]
  <endouttext />
 </foil>
    |;
  }
  $output .= "  </foilgroup>
 </radiobuttonresponse>\n";
}
$output .= "</randomlist>
</problem>";
print PROB  $output;
close(PROB);
exit;