Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # Mandelbrot Set
- # Language: Perl
- # Author: SiD
- use Math::Complex;
- use Tk;
- $mx = -2; $my = -2;
- $nx = 2; $ny = 2;
- $mw = MainWindow -> new(-background => "Black");
- $mw -> title("Mandelbrot Set Fractal ~ Author: SiD");
- $mw -> minsize(215, 195);
- $mw -> Label(
- -background => "Black",
- -foreground => "#00bfff",
- -text => "Output image (name.ppm):"
- ) -> pack(-anchor => "n");
- $mw -> Entry(
- -background => "Black",
- -foreground => "White",
- -textvariable => \$out
- ) -> pack(-anchor => "n");
- $mw -> Label(
- -background => "Black",
- -foreground => "#00bfff",
- -text => "Height:"
- ) -> pack(-anchor => "n");
- $mw -> Entry(
- -background => "Black",
- -foreground => "White",
- -textvariable => \$alt
- ) -> pack(-anchor =>"n");
- $mw -> Label(
- -background => "Black",
- -foreground => "#00bfff",
- -text => "Width:"
- ) -> pack(-anchor => "n");
- $mw -> Entry(
- -background => "Black",
- -foreground => "White",
- -textvariable => \$lar
- ) -> pack(-anchor =>"n");
- $mw -> Label(
- -background => "Black",
- -foreground => "#00bfff",
- -text => "Precision (ex -> 150):"
- ) -> pack(-anchor => "n");
- $mw -> Entry(
- -background => "Black",
- -foreground => "White",
- -textvariable => \$precision
- ) -> pack(-anchor => "n");
- $mw -> Label(
- -background => "Black",
- -foreground => "#00bfff",
- -text => "Number of set (2-12):"
- ) -> pack(-anchor => "n");
- $mw -> Entry(
- -background => "Black",
- -foreground => "White",
- -textvariable => \$sets
- ) -> pack(-anchor => "n");
- $mw -> Label(
- -background => "Black",
- -foreground => "White",
- -text => "\nMandelbrot Set Fractal ~ Options\n"
- ) -> pack(-anchor => "n");
- $vert = $mw -> Checkbutton(
- -text => "Vertical Fractal?",
- -onvalue => "Yes",
- -offvalue => "No",
- -activebackground => "Black",
- -activeforeground => "Orange",
- -background => "Black",
- -foreground => "Orange",
- -variable => \$how
- ) -> pack(-anchor => "sw");
- $mw -> Radiobutton(
- -text => "Red Background",
- -value => "Red",
- -activebackground => "Black",
- -activeforeground => "Orange",
- -background => "Black",
- -foreground => "Orange",
- -variable => \$background
- ) -> pack(-anchor => "sw");
- $mw -> Radiobutton(
- -text => "Black Background",
- -value => "Black",
- -activebackground => "Black",
- -activeforeground => "Orange",
- -background => "Black",
- -foreground => "Orange",
- -variable => \$background
- ) -> pack(-anchor => "sw");
- $mw -> Button(
- -text => "Generate Mandelbrot Set!",
- -font => "Arial 8",
- -activebackground => "Black",
- -activeforeground => "Green",
- -background => "Black",
- -foreground => "Green",
- -command =>
- sub {
- if($out !~ /.ppm/) {
- $out = "mandelbrot.ppm";
- }
- if(!$background) {
- $background = "Black";
- }
- if(!$sets) {
- $sets = 2;
- }
- if($sets < 2 or $sets > 12) {
- $sets = 2;
- }
- open(IMG, ">", $out) or $mw -> destroy;
- print IMG "P3\n# Mandelbrot Set Fractal (Generator) ~ By SiD\n$alt $lar\n255\n";
- for($y=0; $y<$lar; $y++) {
- for($x=0; $x<$alt; $x++) {
- if($how) {
- $a = $mx+($y*($ny-$my)/$alt);
- $b = $my+($x*($nx-$mx)/$lar);
- }
- else {
- $a = $mx+($x*($ny-$my)/$alt);
- $b = $my+($y*($nx-$mx)/$lar);
- }
- $comp = Math::Complex -> make($a, $b);
- $comp1 = $comp;
- for($p=0; $p<$precision; $p++) {
- $comp1 = $comp1**$sets+$comp;
- if(abs($comp1) > 2) {
- last;
- }
- }
- _color($p);
- }
- if($^O =~ /MSWin32/) {
- system("cls");
- print $y+1 ." -> $lar";
- }
- else {
- system("clear");
- print $y+1 ." -> $lar";
- }
- }
- sub _color() {
- my $element = shift;
- if(abs($comp1) <= 2) {
- print IMG "0 0 0\n";
- }
- else {
- if($background eq "Black") {
- $c1 = int((100+$p/4)/3);
- }
- else {
- $c1 = int(100+$p/4);
- }
- $c2 = int($p/2)*8;
- $c3 = int(4*$p)*6;
- if ($c1 > 116) {
- $c1 = 116;
- }
- if ($c2 > 205) {
- $c2 = 205;
- }
- if ($c3 > 255) {
- $c3 = 255;
- }
- print IMG "$c1 $c2 $c3\n";
- }
- }
- sleep(1);
- $mw -> messageBox(
- -message => "Done. \"$out\" created successfully!",
- -type => "Ok"
- );
- $mw -> destroy();
- }) -> pack(-side => "bottom");
- MainLoop;
Advertisement
Add Comment
Please, Sign In to add comment