Mandelbrot set: Difference between revisions

m
Replace deprecated functions
m (Undo revision 353546 by Majow (talk))
m (Replace deprecated functions)
 
(32 intermediate revisions by 8 users not shown)
Line 1,856:
!!!!!!!!!!!!!!!""""""""""""#####################################""""""""""""""""
</pre>
 
=={{header|bc}}==
 
[[File:Mandelbrot-bc.jpg|thumb|right]]
Producing a [https://fr.wikipedia.org/wiki/Portable_pixmap PGM] image.
 
To work properly, this needs to run with the environment variable BC_LINE_LENGTH set to 0.
 
<syntaxhighlight lang=bc>max_iter = 50
width = 400; height = 401
scale = 10
xmin = -2; xmax = 1/2
ymin = -5/4; ymax = 5/4
 
define mandelbrot(c_re, c_im) {
auto i
 
# z = 0
z_re = 0; z_im = 0
z2_re = 0; z2_im = 0
 
for (i=0; i<max_iter; i++) {
# z *= z
z_im = 2*z_re*z_im
z_re = z2_re - z2_im
# z += c
z_re += c_re
z_im += c_im
# z2 = z.*z
z2_re = z_re*z_re
z2_im = z_im*z_im
if (z2_re + z2_im > 4) return i
}
return 0
}
 
print "P2\n", width, " ", height, "\n255\n"
 
for (i = 0; i < height; i++) {
y = ymin + (ymax - ymin) / height * i
for (j = 0; j < width; j++) {
x = xmin + (xmax - xmin) / width * j
tmp_scale = scale
scale = 0
m = (255 * mandelbrot(x, y) + max_iter + 1) / max_iter
print m
if ( j < width - 1 ) print " "
scale = tmp_scale
 
}
print "\n"
}
 
quit</syntaxhighlight>
 
=={{header|BASIC}}==
Line 5,003 ⟶ 5,057:
=={{header|Dc}}==
===ASCII output===
{{works with|GNU Dcdc}}
{{works with|OpenBSD Dcdc}}
 
This can be done in a more Dc-ish way, e.g. by moving the loop macros' definitions to the initialisations in the top instead of saving the macro definition of inner loops over and over again in outer loops.
Line 5,171 ⟶ 5,225:
=={{header|EasyLang}}==
 
[https://easylang.onlinedev/apps/mandelbrot.html Run it]
 
<syntaxhighlight lang="easylang">
# Mandelbrot
center_x = 220
#
center_y = 150
scaleres = 1504
maxiter = 200
#
# better but slower:
# res = 8
# maxiter = 300
#
#
mid = res * 50
center_x = 3 * mid / 2
center_y = mid
scale = mid
#
background 000
textsize 2
#
fastfunc iter cx cy maxiter .
while xx + yy < 4 and it < maxiter
y = 2 * x * y + cy
x = xx - yy + cx
xx = x * x
yy = y * y
it += 1
.
return it
.
proc draw . .
clear
for scr_y = 0 to 2992 * mid - 1
cy = (scr_y - center_y) / scale
for scr_x = 0 to 2992 * mid - 1
cx = (scr_x - center_x) / scale
xit = 0iter ;cx y = 0 ; iter =cy 0maxiter
repeatif it < maxiter
xxcolor3 =it x/ *20 xit / 100 it / 150
yymove =scr_x y/ *res yscr_y / res
untilrect xx1 +/ yyres >=1 4/ or iter = 128res
h = xx - yy + cx
y = 2 * x * y + cy
x = h
iter += 1
.
if iter < 128
color3 iter / 32 iter / 128 0
move scr_x / 3 scr_y / 3
rect 0.4 0.4
.
.
Line 5,211 ⟶ 5,278:
.
on mouse_up
center_x += 150mid - mouse_x * 3res
center_y += 150mid - mouse_y * 3res
if systime - time0 < 0.3
center_x -= 150mid - center_x
center_y -= 150mid - center_y
scale *= 2
else
center_x += (150mid - center_x) * 3 / 24
center_y += (150mid - center_y) * 3 / 24
scale /= 24
.
call draw
.
call draw
</syntaxhighlight>
 
Line 6,426 ⟶ 6,493:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Mandelbrot_set}}
 
'''Solution'''
 
We need first to generate a color palette, this is, a list of colors:
 
[[File:Fōrmulæ - Julia set 01.png]]
 
[[File:Fōrmulæ - Julia set 02.png]]
 
[[File:Fōrmulæ - Julia set 03.png]]
 
The following function draw the Mandelbrot set:
 
[[File:Fōrmulæ - Mandelbrot set 01.png]]
 
'''Test Case 1. Grayscale palette'''
 
[[File:Fōrmulæ - Mandelbrot set 02.png]]
 
[[File:Fōrmulæ - Mandelbrot set 03.png]]
 
'''Test case 2. Black & white palette'''
 
[[File:Fōrmulæ - Mandelbrot set 04.png]]
 
[[File:Fōrmulæ - Mandelbrot set 05.png]]
 
=={{header|GLSL}}==
Line 7,406 ⟶ 7,499:
 
[[File:Mandelbrot-Inform7.png]]
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(function mandelbrot width height depth
(.. str
(for yy (range height)
xx (range width)
(let c_re (/ (* (- xx (/ width 2)) 4) width)
c_im (/ (* (- yy (/ height 2)) 4) width)
x 0 y 0 i 0)
(while (and (<= (+ (** x) (** y)) 4)
(< i depth))
(let x2 (+ c_re (- (** x) (** y)))
y (+ c_im (* 2 x y))
x x2
i (inc i)))
(strn ((zero? xx) "\n") (i "ABCDEFGHIJ ")))))
 
(mandelbrot 48 24 10)
</syntaxhighlight>
 
{{out}}
 
<pre>
 
BBBBCCCDDDDDDDDDEEEEFGJJ EEEDDCCCCCCCCCCCCCCCBBB
BBBCCDDDDDDDDDDEEEEFFH HFEEEDDDCCCCCCCCCCCCCCBB
BBBCDDDDDDDDDDEEEEFFH GFFEEDDDCCCCCCCCCCCCCBB
BBCCDDDDDDDDDEEEEGGHI HGFFEDDDCCCCCCCCCCCCCCB
BBCDDDDDDDDEEEEFG HIGEDDDCCCCCCCCCCCCCB
BBDDDDDDDDEEFFFGH IEDDDDCCCCCCCCCCCCB
BCDDDDDDEEFFFFGG GFEDDDCCCCCCCCCCCCC
BDDDDDEEFJGGGHHI IFEDDDDCCCCCCCCCCCC
BDDEEEEFG J JI GEDDDDCCCCCCCCCCCC
BDEEEFFFHJ FEDDDDCCCCCCCCCCCC
BEEEFFFIJ FEEDDDCCCCCCCCCCCC
BEEFGGH HFEEDDDCCCCCCCCCCCC
JGFEEDDDDCCCCCCCCCCC
BEEFGGH HFEEDDDCCCCCCCCCCCC
BEEEFFFIJ FEEDDDCCCCCCCCCCCC
BDEEEFFFHJ FEDDDDCCCCCCCCCCCC
BDDEEEEFG J JI GEDDDDCCCCCCCCCCCC
BDDDDDEEFJGGGHHI IFEDDDDCCCCCCCCCCCC
BCDDDDDDEEFFFFGG GFEDDDCCCCCCCCCCCCC
BBDDDDDDDDEEFFFGH IEDDDDCCCCCCCCCCCCB
BBCDDDDDDDDEEEEFG HIGEDDDCCCCCCCCCCCCCB
BBCCDDDDDDDDDEEEEGGHI HGFFEDDDCCCCCCCCCCCCCCB
BBBCDDDDDDDDDDEEEEFFH GFFEEDDDCCCCCCCCCCCCCBB
BBBCCDDDDDDDDDDEEEEFFH HFEEEDDDCCCCCCCCCCCCCCBB
</pre>
 
=={{header|J}}==
Line 8,082 ⟶ 8,226:
'''Normal Map Effect and Stripe Average Coloring'''
 
The Mandelbrot set is represented using Normal Maps and Stripe Average Coloring by Jussi Härkönen (cf. Arnaud Chéritat: [https://www.math.univ-toulouse.fr/~cheritat/wiki-draw/index.php/Mandelbrot_set#Normal_map_effect ''Normal map effect'']). See also the picture in section [https://www.math.univ-toulouse.fr/~cheritat/wiki-draw/index.php/Mandelbrot_set#Mixing_it_all ''Mixing it all''] and [https://www.shadertoy.com/view/wtscDX Julia Stripes] on Shadertoy. To get a stripe pattern similar to that of Arnaud Chéritat, one can increase the ''stripesdensity'' of the stripes, use ''cos'' instead of ''sin'', and set the colormap to ''binary''.
<syntaxhighlight lang="julia">using Plots
gr(aspect_ratio=:equal, axis=true, ticks=true, legend=false, dpi=200)
Line 8,089 ⟶ 8,233:
n, r = 200, 500 # number of iterations and escape radius (r > 2)
 
direction, height = 45.0, 1.5 # direction and height of the incoming light
stripesdensity, dampingintensity = 4.0, 2.0.5 # stripe density and dampingintensity parameterof the stripes
 
x = range(0, 2, length=d+1)
Line 8,103 ⟶ 8,247:
for k in 1:n
M = abs.(Z) .< r
S[M], T[M] = S[M] .+ sin.(stripesdensity .* angle.(Z[M])), T[M] .+ 1
Z[M], dZ[M], ddZ[M] = Z[M] .^ 2 .+ C[M], 2 .* Z[M] .* dZ[M] .+ 1, 2 .* (dZ[M] .^ 2 .+ Z[M] .* ddZ[M])
end
 
N = abs.(Z) .>= r # basic normal map effect and stripe average coloring (potential function)
P, Q = S[N] ./ T[N], (S[N] .+ sin.(stripesdensity .* angle.(Z[N]))) ./ (T[N] .+ 1)
U, V = Z[N] ./ dZ[N], 1 .+ (log2.(log.(abs.(Z[N])) ./ log(r)) .* (P .- Q) .+ Q) ./* dampingintensity
U, v = U ./ abs.(U), exp(direction / 180 * pi * im) # unit normal vectors and light vector
D[N] = max.((real.(U) .* real(v) .+ imag.(U) .* imag(v) .+ V .* height) ./ (1 + height), 0)
 
Line 8,118 ⟶ 8,262:
N = abs.(Z) .> 2 # advanced normal map effect using higher derivatives (distance estimation)
U = Z[N] .* dZ[N] .* ((1 .+ log.(abs.(Z[N]))) .* conj.(dZ[N] .^ 2) .- log.(abs.(Z[N])) .* conj.(Z[N] .* ddZ[N]))
U, v = U ./ abs.(U), exp(direction / 180 * pi * im) # unit normal vectors and light vector
D[N] = max.((real.(U) .* real(v) .+ imag.(U) .* imag(v) .+ height) ./ (1 + height), 0)
 
Line 9,273 ⟶ 9,417:
Sample usage:
<syntaxhighlight lang="matlab">mandelbrotSet(-2.05-1.2i,0.004+0.0004i,0.45+1.2i,500);</syntaxhighlight>
 
=={{header|Maxima}}==
Using autoloded package plotdf
<syntaxhighlight lang="maxima">
mandelbrot ([iterations, 30], [x, -2.4, 0.75], [y, -1.2, 1.2],
[grid,320,320])$
</syntaxhighlight>
[[File:MandelbrotMaxima.png|thumb|center]]
 
=={{header|Metapost}}==
Line 11,055 ⟶ 11,207:
'''Normal Map Effect and Stripe Average Coloring'''
 
The Mandelbrot set is represented using Normal Maps and Stripe Average Coloring by Jussi Härkönen (cf. Arnaud Chéritat: [https://www.math.univ-toulouse.fr/~cheritat/wiki-draw/index.php/Mandelbrot_set#Normal_map_effect ''Normal map effect'']). Note that the second derivative (ddZ) grows very fast, so the second method can only be used for small iteration numbers (n <= 400). See also the picture in section [https://www.math.univ-toulouse.fr/~cheritat/wiki-draw/index.php/Mandelbrot_set#Mixing_it_all ''Mixing it all''] and [https://www.shadertoy.com/view/wtscDX Julia Stripes] on Shadertoy. To get a stripe pattern similar to that of Arnaud Chéritat, one can increase the ''stripesdensity'' of the stripes, use ''cos'' instead of ''sin'', and set the colormap to ''binary''.
<syntaxhighlight lang="python">import numpy as np
import matplotlib.pyplot as plt
Line 11,062 ⟶ 11,214:
n, r = 200, 500 # number of iterations and escape radius (r > 2)
 
direction, height = 45.0, 1.5 # direction and height of the incoming light
stripesdensity, dampingintensity = 4.0, 2.0.5 # stripe density and dampingintensity parameterof the stripes
 
x = np.linspace(0, 2, num=d+1)
Line 11,076 ⟶ 11,228:
for k in range(n):
M = abs(Z) < r
S[M], T[M] = S[M] + np.sin(stripesdensity * np.angle(Z[M])), T[M] + 1
Z[M], dZ[M], ddZ[M] = Z[M] ** 2 + C[M], 2 * Z[M] * dZ[M] + 1, 2 * (dZ[M] ** 2 + Z[M] * ddZ[M])
 
N = abs(Z) >= r # basic normal map effect and stripe average coloring (potential function)
P, Q = S[N] / T[N], (S[N] + np.sin(stripesdensity * np.angle(Z[N]))) / (T[N] + 1)
U, V = Z[N] / dZ[N], 1 + (np.log2(np.log(np.abs(Z[N])) / np.log(r)) * (P - Q) + Q) /* dampingintensity
U, v = U / abs(U), np.exp(direction / 180 * np.pi * 1j) # unit normal vectors and light vector
D[N] = np.maximum((U.real * v.real + U.imag * v.imag + V * height) / (1 + height), 0)
 
Line 11,090 ⟶ 11,242:
N = abs(Z) > 2 # advanced normal map effect using higher derivatives (distance estimation)
U = Z[N] * dZ[N] * ((1 + np.log(abs(Z[N]))) * np.conj(dZ[N] ** 2) - np.log(abs(Z[N])) * np.conj(Z[N] * ddZ[N]))
U, v = U / abs(U), np.exp(direction / 180 * np.pi * 1j) # unit normal vectors and light vector
D[N] = np.maximum((U.real * v.real + U.imag * v.imag + height) / (1 + height), 0)
 
Line 11,163 ⟶ 11,315:
break
 
x = np.linspace(0, 2, num=d+1, dtype=np.float64)
y = np.linspace(0, 2 * h / d, num=h+1, dtype=np.float64)
 
A, B = np.meshgrid(x * np.pi, y * np.pi)
Line 11,363 ⟶ 11,515:
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|20162023.08-053-01g2f8234c22}}
[[File:Mandel-perl6.png|thumb]]
Variant of a Mandelbrot script from the [http://modules.raku.org/ Raku ecosystem]. Produces a [[Write ppm file|Portable Pixel Map]] to STDOUT.
Redirect into a file to save it.
Converted to a .png file for display here.
 
Using the [https://docs.raku.org/language/statement-prefixes#hyper,_race hyper statement prefix] for concurrency, the code below produces a [[Write ppm file|graymap]] to standard output.
<syntaxhighlight lang="raku" line>constant @color_map = map ~*.comb(/../).map({:16($_)}), <
 
000000 0000fc 4000fc 7c00fc bc00fc fc00fc fc00bc fc007c fc0040 fc0000 fc4000
[[File:mandelbrot-raku.jpg|300px|thumb|right]]
fc7c00 fcbc00 fcfc00 bcfc00 7cfc00 40fc00 00fc00 00fc40 00fc7c 00fcbc 00fcfc
<syntaxhighlight lang=raku>constant MAX-ITERATIONS = 64;
00bcfc 007cfc 0040fc 7c7cfc 9c7cfc bc7cfc dc7cfc fc7cfc fc7cdc fc7cbc fc7c9c
my $width = +(@*ARGS[0] // 800);
fc7c7c fc9c7c fcbc7c fcdc7c fcfc7c dcfc7c bcfc7c 9cfc7c 7cfc7c 7cfc9c 7cfcbc
my $height = $width + $width %% 2;
7cfcdc 7cfcfc 7cdcfc 7cbcfc 7c9cfc b4b4fc c4b4fc d8b4fc e8b4fc fcb4fc fcb4e8
say "P2";
fcb4d8 fcb4c4 fcb4b4 fcc4b4 fcd8b4 fce8b4 fcfcb4 e8fcb4 d8fcb4 c4fcb4 b4fcb4
say "$width $height";
b4fcc4 b4fcd8 b4fce8 b4fcfc b4e8fc b4d8fc b4c4fc 000070 1c0070 380070 540070
say MAX-ITERATIONS;
700070 700054 700038 70001c 700000 701c00 703800 705400 707000 547000 387000
 
1c7000 007000 00701c 007038 007054 007070 005470 003870 001c70 383870 443870
sub cut(Range $r, UInt $n where $n > 1 --> Seq) {
543870 603870 703870 703860 703854 703844 703838 704438 705438 706038 707038
607038 547038 447038 387038 387044 387054 387060 387070 386070 385470 384470
505070 585070 605070 685070 705070 705068 705060 705058 705050 705850 706050
706850 707050 687050 607050 587050 507050 507058 507060 507068 507070 506870
506070 505870 000040 100040 200040 300040 400040 400030 400020 400010 400000
401000 402000 403000 404000 304000 204000 104000 004000 004010 004020 004030
004040 003040 002040 001040 202040 282040 302040 382040 402040 402038 402030
402028 402020 402820 403020 403820 404020 384020 304020 284020 204020 204028
204030 204038 204040 203840 203040 202840 2c2c40 302c40 342c40 3c2c40 402c40
402c3c 402c34 402c30 402c2c 40302c 40342c 403c2c 40402c 3c402c 34402c 30402c
2c402c 2c4030 2c4034 2c403c 2c4040 2c3c40 2c3440 2c3040
>;
constant MAX_ITERATIONS = 50;
my $width = my $height = +(@*ARGS[0] // 31);
sub cut(Range $r, UInt $n where $n > 1) {
$r.min, * + ($r.max - $r.min) / ($n - 1) ... $r.max
}
 
my @re = cut(-2 .. 1/2, $width);
my @im = cut( 0 .. 5/4, 1 + ($height div 2)) X* 1i;
sub mandelbrot(Complex $z is copy, Complex $c --> Int) {
my @re = cut(-2 .. 1/2, $height);
for 1 .. MAX-ITERATIONS {
my @im = cut( 0 .. 5/4, $width div 2 + 1) X* 1i;
$z = $z*$z + $c;
return $_ if $z.abs > 2;
sub mandelbrot(Complex $z is copy, Complex $c) {
for 1 .. MAX_ITERATIONS {
$z = $z*$z + $c;
return $_ if $z.abs > 2;
}
return 0;
}
my @lines = hyper for @im X+ @re {
say "P3";
mandelbrot(0i, $_);
say "$width $height";
}.rotor($width);
say "255";
 
.put for @lines[1..*].reverse;
for @re -> $re {
.put for @lines;</syntaxhighlight>
put @color_map[|.reverse, |.[1..*]][^$width] given
 
my @ = map &mandelbrot.assuming(0i, *), $re «+« @im;
<!-- # Not sure this version is that much modern or faster now.
}</syntaxhighlight>
 
Alternately, a more modern, faster version.
[[File:Mandelbrot-set-perl6.png|300px|thumb|right]]
<syntaxhighlight lang="rakuperl6" line>use Image::PNG::Portable;
 
my ($w, $h) = 800, 800;
Line 11,469 ⟶ 11,601:
}
}</syntaxhighlight>
 
-->
 
=={{header|REXX}}==
Line 12,320 ⟶ 12,454:
# for which the sequence z[n+1] := z[n] ** 2 + z[0] (n >= 0) is bounded.
# Since this program is computing intensive it should be compiled with
# hi comps7c -O2 mandelbr
 
const integer: pix is 200;
Line 12,352 ⟶ 12,486:
z0 := center + complex(flt(x) * zoom, flt(y) * zoom);
point(x + pix, y + pix, colorTable[iterate(z0)]);
end for;
end for;
end func;
Line 12,370 ⟶ 12,504:
end for;
displayMandelbrotSet(complex(-0.75, 0.0), 1.3 / flt(pix));
DRAW_FLUSHflushGraphic;
readln(KEYBOARD);
end func;
Line 13,433 ⟶ 13,567:
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell|4}}
<syntaxhighlight lang="bash">function mandelbrot((xmin=-8601)) # int(-2.1*4096){
local -ir maxiter=100
((xmax=2867)) # int( 0.7*4096)
local -i i j {x,y}m{in,ax} d{x,y}
local -ra C=( {0..9} )
((ymin=-4915)) # int(-1.2*4096)
local -i lC=${#C[*]}
((ymax=4915)) # int( 1.2*4096)
local -i columns=${COLUMNS:-72} lines=${LINES:-24}
 
((
((maxiter=30))
xmin=-21*4096/10,
xmax= 7*4096/10,
ymin=-12*4096/10,
ymax= 12*4096/10,
 
(( dx=(xmax-xmin)/72))columns,
(( dy=(ymax-ymin)/24))lines
))
 
for ((cy=ymax, i=0; i<lines; cy-=dy, i++))
C='0123456789'
do for ((cx=xmin, j=0; j<columns; cx+=dx, j++))
((lC=${#C}))
do (( x=0, y=0, x2=0, y2=0 ))
 
for (( iter=0; iter<maxiter && x2+y2<=16384; iter++ ))
for((cy=ymax;cy>=ymin;cy-=dy)) ; do
do
for((cx=xmin;cx<=xmax;cx+=dx)) ; do
((
((x=0,y=0,x2=0,y2=0))
y=((x*y)>>11)+cy,
for((iter=0;iter<maxiter && x2+y2<=16384;iter++)) ; do
x=x2-y2+cx,
((y=((x*y)>>11)+cy,x=x2-y2+cx,x2=(x*x)>>12,y2=(y*y)>>12))
x2=(x*x)>>12,
done
y2=(y*y)>>12
((c=iter%lC))
))
echo -n ${C:$c:1}
done
((c=iter%lC))
echo
echo -n "${C[c]}"
done</syntaxhighlight>
done
echo
done
}</syntaxhighlight>
 
{{out}}
Line 14,142 ⟶ 14,287:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 14,187 ⟶ 14,332:
 
var Game = MandelbrotSet.new(800, 600)</syntaxhighlight>
 
{{out}}
[[File:Wren-Mandelbrot_set.png|400px]]
 
=={{header|XPL0}}==
29

edits