File: mandelbrot.pl

package info (click to toggle)
gambas3 3.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,984 kB
  • sloc: ansic: 197,178; cpp: 124,076; sh: 18,999; javascript: 7,761; sql: 5,399; makefile: 2,354; perl: 1,397; xml: 490; python: 335
file content (75 lines) | stat: -rwxr-xr-x 1,022 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl -w

use strict;

my $MAXITER = 50;
my $LIMIT = 4;

sub mandelbrot($$)
{
  my $w = $_[0];
  my $h = $_[1];
  my $xmin = -1.5;
  my $ymin = -1;
  my $invN = 2/$w;

  my $checknext=1;

  for my $y (0..$h-1) {

    my $Ci = $y * $invN + $ymin;

    X:
    for my $x (0..$w-1) {

      my $Zr = 0;
      my $Zi = 0;
      my $Tr = 0;
      my $Ti = 0;

      my $Cr = $x * $invN + $xmin;

      if ($checknext) {

	  for (1..$MAXITER) {
	    $Zi = 2 * $Zr * $Zi + $Ci;
	    $Zr = $Tr - $Ti + $Cr;
	    $Ti = $Zi * $Zi;
	    $Tr = $Zr * $Zr;

	    if ($Tr + $Ti > $LIMIT) {
	      print "0";
	      next X;
	    }
	  }

	  print "1";
	  $checknext = 0;

      } else {

	for (1..$MAXITER) {
	  $Zi = 2 * $Zr * $Zi + $Ci;
	  $Zr = $Tr - $Ti + $Cr;
	  $Ti = $Zi * $Zi;
	  $Tr = $Zr * $Zr;
	}

	if ($Tr+$Ti < $LIMIT) {
	  print "1";
	} else { # $Tr+$Ti is a large number or overflow ('nan' or 'inf')
	  print "0";
	  $checknext = 1;
	}

      }

    }

    print "\n";
  }
}

for(1..20) {
  mandelbrot(200, 200);
}