Mandelbrot set: Difference between revisions
m
Replace deprecated functions
m (Added location analysis by Claude Heiland-Allen) |
m (Replace deprecated functions) |
||
(43 intermediate revisions by 9 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 2,488 ⟶ 2,542:
180 GOTO 180
</syntaxhighlight>
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{trans|Microsoft Super Extended Color BASIC}}
<syntaxhighlight lang="qbasic">100 SCREEN 2
110 CLS
120 x1 = 256 : y1 = 192
130 i1 = -1 : i2 = 1
140 r1 = -2 : r2 = 1
150 s1 = (r2-r1)/x1 : s2 = (i2-i1)/y1
160 FOR y = 0 TO y1
170 i3 = i1+s2*y
180 FOR x = 0 TO x1
190 r3 = r1+s1*x
200 z1 = r3 : z2 = i3
210 FOR n = 0 TO 30
220 a = z1*z1 : b = z2*z2
230 IF a+b > 4 GOTO 270
240 z2 = 2*z1*z2+i3
250 z1 = a-b+r3
260 NEXT n
270 PSET (x,y),n-16*INT(n/16)
280 NEXT x
290 NEXT y
300 GOTO 300</syntaxhighlight>
{{out}}
[[File:Mandelbrot-MS-BASIC.png]]
==={{header|Nascom BASIC}}===
Line 2,890 ⟶ 2,971:
Pt-On(real(C),imag(C),N
End
End</syntaxhighlight>
==={{header|True BASIC}}===
{{trans|Microsoft Super Extended Color BASIC}}
<syntaxhighlight lang="qbasic">SET WINDOW 0, 256, 0, 192
LET x1 = 256/2
LET y1 = 192/2
LET i1 = -1
LET i2 = 1
LET r1 = -2
LET r2 = 1
LET s1 = (r2-r1) / x1
LET s2 = (i2-i1) / y1
FOR y = 0 TO y1 STEP .05
LET i3 = i1 + s2 * y
FOR x = 0 TO x1 STEP .05
LET r3 = r1 + s1 * x
LET z1 = r3
LET z2 = i3
FOR n = 0 TO 30
LET a = z1 * z1
LET b = z2 * z2
IF a+b > 4 THEN EXIT FOR
LET z2 = 2 * z1 * z2 + i3
LET z1 = a - b + r3
NEXT n
SET COLOR n - 16*INT(n/16)
PLOT POINTS: x,y
NEXT x
NEXT y
END</syntaxhighlight>
==={{header|Visual BASIC for Applications on Excel}}===
{{works with|Excel 2013}}
Based on the BBC BASIC version. Create a spreadsheet with -2 to 2 in row 1 and -2 to 2 in the A column (in steps of your choosing). In the cell B2, call the function with =mandel(B$1,$A2) and copy the cell to all others in the range. Conditionally format the cells to make the colours pleasing (eg based on values, 3-color scale, min value 2 [colour red], midpoint number 10 [green] and highest value black. Then format the cells with the custom type "";"";"" to remove the numbers.
<syntaxhighlight lang="vba">Function mandel(xi As Double, yi As Double)
maxiter = 256
Line 2,911 ⟶ 3,022:
mandel = i
End Function</syntaxhighlight>
[[File:vbamandel.png]]
Edit: I don't seem to be able to upload the screenshot, so I've shared it here: https://goo.gl/photos/LkezpuQziJPAtdnd9
Line 4,947 ⟶ 5,057:
=={{header|Dc}}==
===ASCII output===
{{works with|GNU
{{works with|OpenBSD
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,115 ⟶ 5,225:
=={{header|EasyLang}}==
[https://easylang.
<syntaxhighlight lang="easylang">
# Mandelbrot
#
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
cy = (scr_y - center_y) / scale
for scr_x = 0 to
cx = (scr_x - center_x) / scale
.
.
Line 5,155 ⟶ 5,278:
.
on mouse_up
center_x +=
center_y +=
if systime - time0 < 0.3
center_x -=
center_y -=
scale *= 2
else
center_x += (
center_y += (
scale /=
.
.
</syntaxhighlight>
Line 6,370 ⟶ 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,350 ⟶ 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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HFEEEDDDCCCCCCCCCCCCCCBB
</pre>
=={{header|J}}==
Line 7,979 ⟶ 8,179:
===Normal Map Effect, Mercator Projection and Perturbation Theory===
'''
This is a translation of the corresponding Python section: see there for more explanations. The ''e^(-|z|)-smoothing'', ''normalized iteration count'' and ''exterior distance estimation'' algorithms are used. Partial antialiasing is used for boundary detection.
<syntaxhighlight lang="julia">using Plots
gr(aspect_ratio=:equal, axis=true, ticks=true, legend=false, dpi=200)
Line 8,013 ⟶ 8,213:
N = abs.(Z) .> 2 # exterior distance estimation
D[N] =
heatmap(D .^ 0.1, c=:balance)
savefig("Mandelbrot_set_3.png")
N, thickness = D .> 0, 0.01 # boundary detection
D[N] = max.(1 .- D[N] ./ thickness, 0)
heatmap(D .^ 2.0, c=:binary)
savefig("Mandelbrot_set_4.png")</syntaxhighlight>
'''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 ''density'' 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,027 ⟶ 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
x = range(0, 2, length=d+1)
Line 8,041 ⟶ 8,247:
for k in 1:n
M = abs.(Z) .< r
S[M], T[M] = S[M] .+
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
P, Q = S[N] ./ T[N], (S[N] .+
U,
D[N] = max.((real.(U) .* real(v) .+ imag.(U) .* imag(v) .+ V .* height) ./ (1 + height), 0)
Line 8,055 ⟶ 8,260:
savefig("Mandelbrot_normal_map_1.png")
N = abs.(Z) .> 2 # advanced normal map effect
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 8,072 ⟶ 8,277:
n, r = 8000, 10000 # number of iterations and escape radius (r > 2)
a = -.743643887037158704752191506114774 #
b = 0.131825904205311970493132056385139 #
x = range(0, 2, length=d+1)
Line 9,212 ⟶ 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 10,948 ⟶ 11,161:
===Normal Map Effect, Mercator Projection and Deep Zoom Images===
'''
The Mandelbrot set is printed with smooth colors. The ''e^(-|z|)-smoothing'', ''normalized iteration count'' and ''exterior distance estimation'' algorithms are used with NumPy and complex matrices (see Javier Barrallo & Damien M. Jones: [http://www.mi.sanu.ac.rs/vismath/javier/index.html ''Coloring Algorithms for Dynamical Systems in the Complex Plane''] and
<syntaxhighlight lang="python">import numpy as np
import matplotlib.pyplot as plt
Line 10,984 ⟶ 11,197:
plt.imshow(D ** 0.1, cmap=plt.cm.twilight_shifted, origin="lower")
plt.savefig("Mandelbrot_set_3.png", dpi=200)
N, thickness = D > 0, 0.01 # boundary detection
D[N] = np.maximum(1 - D[N] / thickness, 0)
plt.imshow(D ** 2.0, cmap=plt.cm.binary, origin="lower")
plt.savefig("Mandelbrot_set_4.png", dpi=200)</syntaxhighlight>
'''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 ''density'' 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 10,995 ⟶ 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
x = np.linspace(0, 2, num=d+1)
Line 11,009 ⟶ 11,228:
for k in range(n):
M = abs(Z) < r
S[M], T[M] = S[M] + np.
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
P, Q = S[N] / T[N], (S[N] + np.
U,
D[N] = np.maximum((U.real * v.real + U.imag * v.imag + V * height) / (1 + height), 0)
Line 11,022 ⟶ 11,240:
plt.savefig("Mandelbrot_normal_map_1.png", dpi=200)
N = abs(Z) > 2 # advanced normal map effect
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,039 ⟶ 11,257:
n, r = 8000, 10000 # number of iterations and escape radius (r > 2)
a = -.743643887037158704752191506114774 #
b = 0.131825904205311970493132056385139 #
x = np.linspace(0, 2, num=d+1)
Line 11,097 ⟶ 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,297 ⟶ 11,515:
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|
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.
[[File:mandelbrot-raku.jpg|300px|thumb|right]]
<syntaxhighlight lang=raku>constant MAX-ITERATIONS = 64;
my $width = +(@*ARGS[0] // 800);
my $height = $width + $width %% 2;
say "P2";
say "$width $height";
say MAX-ITERATIONS;
sub cut(Range $r, UInt $n where $n > 1 --> Seq) {
$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) {
for 1 .. MAX-ITERATIONS {
$z = $z*$z + $c;
return $_ if $z.abs > 2;
}
return 0;
}
my @lines = hyper for @im X+ @re {
mandelbrot(0i, $_);
}.rotor($width);
.put for @lines[1..*].reverse;
.put for @lines;</syntaxhighlight>
<!-- # Not sure this version is that much modern or faster now.
Alternately, a more modern, faster version.
[[File:Mandelbrot-set-perl6.png|300px|thumb|right]]
<syntaxhighlight lang="
my ($w, $h) = 800, 800;
Line 11,403 ⟶ 11,601:
}
}</syntaxhighlight>
-->
=={{header|REXX}}==
Line 12,254 ⟶ 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
#
const integer: pix is 200;
Line 12,286 ⟶ 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,304 ⟶ 12,504:
end for;
displayMandelbrotSet(complex(-0.75, 0.0), 1.3 / flt(pix));
readln(KEYBOARD);
end func;
Line 13,367 ⟶ 13,567:
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell|4}}
<syntaxhighlight lang="bash">function mandelbrot(
local -ir maxiter=100
local -i i j {x,y}m{in,ax} d{x,y}
local -ra C=( {0..9} )
local -i lC=${#C[*]}
local -i columns=${COLUMNS:-72} lines=${LINES:-24}
((
xmin=-21*4096/10,
xmax= 7*4096/10,
ymin=-12*4096/10,
ymax= 12*4096/10,
))
for ((cy=ymax, i=0; i<lines; cy-=dy, i++))
do for ((cx=xmin, j=0; j<columns; cx+=dx, j++))
do (( x=0, y=0, x2=0, y2=0 ))
for (( iter=0; iter<maxiter && x2+y2<=16384; iter++ ))
do
((
y=((x*y)>>11)+cy,
x=x2-y2+cx,
x2=(x*x)>>12,
y2=(y*y)>>12
))
((c=iter%lC))
echo -n "${C[c]}"
done
echo
done
}</syntaxhighlight>
{{out}}
Line 14,076 ⟶ 14,287:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="
import "dome" for Window
Line 14,121 ⟶ 14,332:
var Game = MandelbrotSet.new(800, 600)</syntaxhighlight>
{{out}}
[[File:Wren-Mandelbrot_set.png|400px]]
=={{header|XPL0}}==
|