Friday, September 05, 2008

Circle (or Disc)

#! perl -w
die unless ($size=$ARGV[0]);
$center=($size-1)/2;
$radsquared=$size*$size/4;
print "P2\n$size $size\n1\n";

for $i(0..($size-1)) {
  $dx=$i-$center;
  for $j (0..($size-1)) {
    $dy=$j-$center;
    if ($dx*$dx+$dy*$dy<=$radsquared) {
      print "1"
    } else {
       print "0"
     }
    print " ";
  }
  print "\n";
}

circle

No comments :