Zufallsfraktale
Initialisierungen:
> restart;
> with(plots): #pointplot
Warning, the name changecoords has been redefined
m - Anzahl der Iterationen
> anz:=5000;
A - Liste mit Eckpunkten ( hier: Dreieck)
> A:=[[0, 0], [0.5, 1], [1, 0]];
n - Anzahl der Eckpunkte
> n := nops(A);
zufall - gibt eine Zahl zwischen 1 und n zurück, wählt also zufällig eine Ecke aus.
> zufall:=rand(1..n);
>
p - Verhältniss von neuem zu altem Punkt :
> p := 1/2;
P - Startpunkt
> P:=[0, 0];
Punkte-Array festlegen (wg. Geschwindigkeit) :
> Punkte := array(1..anz, 1..2);
Iterationen:
>
for i from 1 to anz do
k:= zufall(); # zufällig eine ecke auswählen
P := [(P[1]+A[k, 1])*p,(P[2]+A[k, 2])*p]:
Punkte[i, 1] := P[1];
Punkte[i, 2] := P[2];
od:
'Punkte' in eine Liste von Punkten umwandeln (wg. pointplot) :
> Punkte:=convert(Punkte, listlist):
Punkte Darstellen :
> pointplot(Punkte, symbol=point, axes=boxed,scaling = constrained);
>
Zufallsfraktalerzeugung als Funktion
Initialisierungen:
>
restart;
with(plots): #pointplot
Warning, the name changecoords has been redefined
fractal
- Prozedur zum Zeichnen von Zufallsfraktalen
Parameter:
1. A - Eine Liste von Punkten (z.B. [[0, 0], [1, 0], [0, 1]])
2. Anzahl von Iterationen
3. Verhältniss von neuem zu altem Punkt
Rückgabe: Plot vom Fraktal
>
fractal := proc(A::list, anz::posint, p)
local i, k, n, zufall, P, Punkte:
n:=nops(A);
zufall := rand(1..n):
P:=[0, 0]:
Punkte := array(1..anz, 1..2):
for i from 1 to anz do
k := zufall():
P := [ (P[1]+A[k, 1])*p, (P[2]+A[k, 2])*p ]:
Punkte[i, 1] := P[1]:
Punkte[i, 2] := P[2]:
od:
Punkte:=convert(Punkte, listlist):
pointplot(Punkte, symbol=point, axes=boxed, scaling = constrained):
end:
>
n_eck
- Prozedur zum erstellen von eckpunkten Regelmäßiger n-Ecke.
Parameter: 1. n - Anzahl der Ecken.
Rückgabe: Liste von Punkten
>
n_eck := proc(n::posint)
local P, i:
P:=[]:
for i from 1 to n do
P:=[op(P), [2*sin(2*Pi*i/n), 2*cos(2*Pi*i/n)]]:
od:
return P;
end:
>
Beispiele:
> fractal(n_eck(6), 10000, 0.3);
> fractal(n_eck(5), 10000, 0.4);
> fractal([[0, 0], [0.5, 0.4], [0.5, 1], [1, 0]], 10000, 0.4);
> fractal(n_eck(3), 10000, -0.5);
>
>