Share Pastebin
Guest
Public paste!

SiD

By: a guest | Nov 23rd, 2008 | Syntax: Perl | Size: 3.81 KB | Hits: 108 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/perl
  2.  
  3. # Mandelbrot Set
  4. # Language: Perl
  5. # Author: SiD
  6.  
  7. use Math::Complex;
  8. use Tk;
  9.  
  10. $mx = -2; $my = -2;
  11. $nx = 2; $ny = 2;
  12.  
  13. $mw = MainWindow -> new(-background => "Black");
  14. $mw -> title("Mandelbrot Set Fractal ~ By SiD");
  15. $mw -> minsize(185, 165);
  16. $mw -> Label(
  17.                 -background   => "Black",
  18.                 -foreground   => "#00bfff",
  19.                 -text         => "Output image (name.ppm):"
  20.         ) -> pack(-anchor => "n");
  21. $mw -> Entry(
  22.                 -background   => "Black",
  23.                 -foreground   => "White",
  24.                 -textvariable => \$out
  25.         ) -> pack(-anchor => "n");
  26. $mw -> Label(
  27.                 -background   => "Black",
  28.                 -foreground   => "#00bfff",
  29.                 -text         => "Height:"
  30.         ) -> pack(-anchor => "n");
  31. $mw -> Entry(
  32.                 -background   => "Black",
  33.                 -foreground   => "White",
  34.                 -textvariable => \$alt
  35.         ) -> pack(-anchor =>"n");
  36. $mw -> Label(
  37.                 -background   => "Black",
  38.                 -foreground   => "#00bfff",
  39.                 -text         => "Width:"
  40.         ) -> pack(-anchor => "n");
  41. $mw -> Entry(
  42.                 -background   => "Black",
  43.                 -foreground   => "White",
  44.                 -textvariable => \$lar
  45.         ) -> pack(-anchor =>"n");
  46. $mw -> Label(
  47.                 -background   => "Black",
  48.                 -foreground   => "#00bfff",
  49.                 -text         => "Precision (ex -> 256):"
  50.         ) -> pack(-anchor => "n");
  51. $mw -> Entry(
  52.                 -background   => "Black",
  53.                 -foreground   => "White",
  54.                 -textvariable => \$precision
  55.         ) -> pack(-anchor => "n");
  56.        
  57. $mw -> Radiobutton(
  58.                 -text             => "Vertical Fractal?",
  59.                 -value            => "Vertical Fractal?",
  60.                 -activebackground => "Black",
  61.             -activeforeground => "Orange",
  62.                 -background       => "Black",
  63.                 -foreground       => "Orange",
  64.         -variable         => \$how
  65.         ) -> pack(-anchor     => "sw");
  66. $mw -> Radiobutton(
  67.                 -text             => "Red Background",
  68.                 -value            => "Red",
  69.                 -activebackground => "Black",
  70.             -activeforeground => "Orange",
  71.                 -background       => "Black",
  72.                 -foreground       => "Orange",
  73.         -variable         => \$background
  74.         ) -> pack(-anchor     => "sw");
  75. $mw -> Radiobutton(
  76.                 -text             => "Black Background",
  77.                 -value            => "Black",
  78.                 -activebackground => "Black",
  79.             -activeforeground => "Orange",
  80.                 -background       => "Black",
  81.                 -foreground       => "Orange",
  82.         -variable         => \$background
  83.         ) -> pack(-anchor     => "sw");
  84.  
  85. $mw -> Button(
  86.         -text             => "Generate Mandelbrot Set!",
  87.         -font             => "Arial 8",
  88.         -activebackground => "Black",
  89.         -activeforeground => "Green",
  90.         -background       => "Black",
  91.         -foreground       => "Green",
  92.         -command          =>
  93. sub {
  94.  
  95.         if($out !~ /.ppm/) {
  96.                 $out = "mandelbrot.ppm";
  97.         }
  98.         if(!$background) {
  99.                 $background = "Black";
  100.         }
  101.  
  102.         open(IMG, ">", $out) or $mw -> destroy;
  103.         print IMG "P3\n# Mandelbrot Set Fractal (Generator) ~ By SiD\n$alt $lar\n255\n";
  104.  
  105.         for($y=0; $y<$lar; $y++) {
  106.                 for($x=0; $x<$alt; $x++) {
  107.                         if($how) {
  108.                                 $a = $mx+($y*($ny-$my)/$alt);
  109.                                 $b = $my+($x*($nx-$mx)/$lar);
  110.                         }
  111.                         else {
  112.                                 $a = $mx+($x*($ny-$my)/$alt);
  113.                                 $b = $my+($y*($nx-$mx)/$lar);
  114.                         }
  115.  
  116.                         $comp = Math::Complex -> make($a, $b);
  117.                         $comp1 = $comp;
  118.  
  119.                         for($p=0; $p<$precision; $p++) {
  120.                                 $comp1 = $comp1*$comp1+$comp;
  121.                                 if(abs($comp1) > 2) {
  122.                                         last;
  123.                                 }
  124.                         }
  125.                         _color($p);
  126.                 }
  127.                 if($^O =~ /MSWin32/) {
  128.                         system("cls");
  129.                         print $y+1 ." -> $lar";
  130.                 }
  131.                 else {
  132.                         system("clear");
  133.                         print $y+1 ." -> $lar";
  134.                 }
  135.         }
  136.  
  137.         sub _color() {
  138.                 my $element = shift;
  139.        
  140.                 if(abs($comp1) <= 2) {
  141.                         print IMG "0 0 0\n";
  142.                 }
  143.                 else {
  144.                         if($background eq "Black") {
  145.                                 $c1 = int((100+$p/4)/3);
  146.                         }
  147.                         else {
  148.                                 $c1 = int(100+$p/4);
  149.                         }
  150.                         $c2 = int($p/2)*8;
  151.                         $c3 = int(4*$p)*6;
  152.                         if ($c1 > 116) {
  153.                                 $c1 = 116;
  154.                         }
  155.                         if ($c2 > 205) {
  156.                                 $c2 = 205;
  157.                         }
  158.                         if ($c3 > 255) {
  159.                                 $c3 = 255;
  160.                         }
  161.                         print IMG "$c1 $c2 $c3\n";
  162.                 }
  163.         }
  164.  
  165.         sleep(3);
  166.         $mw -> destroy;
  167.  
  168. }) -> pack(-anchor => "s");
  169.  
  170. MainLoop;