We don't bite newbies here... much PerlMonks

### Optimized Mandelbrot set generator

 on Jun 14, 2015 at 22:30 UTC Need Help??

I wrote a Mandelbrot set generator. It works on Windows ActivePerl 5.20.2 x64, but I haven't tried it on *nix. It outputs a smoothed, anti-aliased fractal image to 24-bit .bmp file. For optimizations, it takes advantage of symmetry; it skips the cardoid, the main bulb, and the line across y=0; and it checks for cycles when the iteration count gets high. On my computer (2.40GHz i5-2430M), it takes about 10 minutes to generate a 1500x1000 image.

```
+      (

+ \$w,
\$a)
+=@ARGV
;\$w-
+=\$w%12
;op
+en(\$f,
'
+>',
\$a    );binmod
+e\$f;\$|=1;\$z=
16384;@g=(" Do
+ne!\n","Gene".  "r"
."ating fractal
+ with dimensions "
.  \$w." by ".2/3*\$w
+."...\n","\nSavin".
"g fractal to ".\$a
+."...");print\$f 'BM'
.pack('VVVVvvvv',\$w
+*\$w*2+26,\$n,26,12,\$w,\$w
*2/3,1,24);sub a{(\$
+r,\$i,\$x,\$y,\$j,@s)=@_;
;;  return        \$z if((\$r+1)**2+\$i*\$
+i<1/16||((\$r-1/4)**2+
\$i*\$i)*(((\$r    -1/4)**2+\$i*\$i)+(\$r-1
+/4))<1/4*\$i*\$i or!\$i&&
abs(\$r+1)<1);do{  return \$j-log(log(\$x*\$
+x+\$y*\$y)*0.5/log(2))/
log(2)if((push@s,(\$x,\$y))&&\$x*\$x+\$y*\$y>3
+6);\$t=1;(\$x,\$y)=(\$r+
\$x* \$x-\$y*\$y,\$i+2*\$x*\$y);if(\$j%int(\$z/11)==0&
+&\$j){for(@s){return
\$z if(\$s[\$j-\$t++]eq\$s[\$j])}}}while(\$j++<\$z);return\$z}\$u.=' 'while(\$o++
+<51);print\$g[1].
\$u. "]\r[";do{if((\$k-1)/(\$w*(\$w/3+1))<\$n&&\$k/
+(\$w*(\$w/3+1))>=\$n){
\$n+=0.02;print'='}\$h=4;\$v[\$k]+=a(3.7*(\$k
+%\$w+int(\$h/3)/3)/\$w-
2.6,3.7*(int(\$k/  \$w)+(\$h--%3-0)/3)/\$w)/
+9 while(\$h>-5)}while(
\$k++<\$w*(\$w/    3+1));print\$g[2];prin
+t\$f pack('ccc',((\$z-\$p
)/  \$z-2**        -(\$p-2.8))*64*(sin(1
+.1+log(\$p)*4)-sin(log
(\$p)*8+1.1)/3+5/3),
+((\$z-\$p)/\$z-2**-(\$p-
2.8))*64*(sin(log(
+\$p)*4-1.1)+sin(log(\$p)
*8-1.1)/3+5/3),2*
+((\$z-\$p)/\$z-2**-(\$p
-  2.8))*63.5*(sin(
+log(\$p)*4-1.25)+1)
)while((\$p=\$v[
+abs(\$w/3-int(\$l/\$w
))*\$w+\$l++%\$w]
++2.8)-2.8);;;;  ;;;
;;    close\$f;
+;#MANDELBROT
#
+SET
#K.
+STREHL
06/1
+4/2015
;;p
+rint\$g

+ [0

+      ]

The code's designed to look like the set, of course. It takes user input to determine the size of the image and the outfile, i.e.:

```C:\Users\Kevin>mandelgen.pl 1200 mandel.bmp
Generating fractal with dimensions 1200 by 800...
[==================================================]
Saving fractal to mandel.bmp... Done!

If the given pixel width isn't divisible by 12, the program subtracts until it is.

```for\$b(-25..25){for\$a(-50..29){\$x=\$a/21;\$y=\$b/15;print\$b?chr:chr^chr
ord(substr'<6C}'.1x29 .'[FDEq2?@E96Cqa6C=q924',\$a)-49;\$_=30;(\$y,\$x)
=(2*\$x*\$y+\$b/15,\$x*\$x-\$y*\$y+\$a/21)while\$x*\$x+\$y*\$y<9&\$_++<95}\$_=10}

Replies are listed 'Best First'.
Re: Optimized Mandelbrot set generator
by Discipulus (Monsignor) on Jun 15, 2015 at 12:19 UTC
Oh compiments! Anyway, just for curiosity thisisdada means 'this is dada'? the powerful win32 italian Perl hacker?
In that case well come back, otherwise welcome thisisdada

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Thanks! And no, that's not me. This is just a Reddit name I started using a couple of years ago.

Create A New User
Node Status?
node history
Node Type: obfuscated [id://1130397]
Front-paged by Athanasius
help
Chatterbox?
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2018-06-18 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (109 votes). Check out past polls.

Notices?