Kronecker product based fractals: Difference between revisions
Content added Content deleted
(Added FreeBASIC) |
|||
Line 1,431: | Line 1,431: | ||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="vbnet">Type Matrix |
|||
As Integer x |
|||
As Integer y |
|||
As Integer Ptr Dato |
|||
End Type |
|||
Function kroneckerProduct(a As Matrix, b As Matrix) As Matrix |
|||
Dim As Integer m = a.x, n = a.y |
|||
Dim As Integer p = b.x, q = b.y |
|||
Dim As Matrix r |
|||
r.x = m * p |
|||
r.y = n * q |
|||
r.dato = Callocate(r.x * r.y, Sizeof(Integer)) |
|||
Dim As Integer i, j, k, l |
|||
For i = 0 To m - 1 |
|||
For j = 0 To n - 1 |
|||
For k = 0 To p - 1 |
|||
For l = 0 To q - 1 |
|||
r.dato[(p * i + k) * r.y + (q * j + l)] = a.dato[i * a.y + j] * b.dato[k * b.y + l] |
|||
Next |
|||
Next |
|||
Next |
|||
Next |
|||
Return r |
|||
End Function |
|||
Function kroneckerPower(a As Matrix, n As Integer) As Matrix |
|||
Dim As Matrix pow = a |
|||
For i As Integer = 1 To n - 1 |
|||
pow = kroneckerProduct(pow, a) |
|||
Next |
|||
Return pow |
|||
End Function |
|||
Sub printMatrix(text As String, m As Matrix) |
|||
Dim As Integer i, j |
|||
Print text & " fractal:" |
|||
For i = 0 To m.x - 1 |
|||
For j = 0 To m.y - 1 |
|||
Print Iif(m.dato[i * m.y + j] = 1, "*", " "); |
|||
Next |
|||
Print |
|||
Next |
|||
Print |
|||
End Sub |
|||
Dim As Matrix a = Type(3, 3, Callocate(9, Sizeof(Integer))) |
|||
a.dato[0] = 0: a.dato[1] = 1: a.dato[2] = 0 |
|||
a.dato[3] = 1: a.dato[4] = 1: a.dato[5] = 1 |
|||
a.dato[6] = 0: a.dato[7] = 1: a.dato[8] = 0 |
|||
printMatrix("Vicsek", kroneckerPower(a, 4)) |
|||
a.dato[0] = 1: a.dato[1] = 1: a.dato[2] = 1 |
|||
a.dato[3] = 1: a.dato[4] = 0: a.dato[5] = 1 |
|||
a.dato[6] = 1: a.dato[7] = 1: a.dato[8] = 1 |
|||
printMatrix("Sierpinski carpet", kroneckerPower(a, 4)) |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Same as Kotlin entry.</pre> |
|||
=={{header|gnuplot}}== |
=={{header|gnuplot}}== |