[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]

Introduction

** Known as a typical fractal figure using Python **

** 1. Sierpinski triangle ** ** 2. Burnsley fern ** ** 3. Fractal tree **

Draw **. ** **

I will not mention the mathematics of fractal geometry here (because the author is shallow in the field). However, I hope you enjoy watching ** how fractal figures that can be seen naturally are generated from simple rules **.


Contents

(1) Sierpinski triangle

** It is a pattern found in the patterns of marine life such as shells. ** Draw a set of points made by the following simple rules.

1.Draw an equilateral triangle with the coordinates of the following three vertices.
Vertex 1: (x1,y1)
Peak 2: (x2,y2)
Top 3: (x3,y3)

2.Any point P inside the triangle(x_0,y_0)Is the first point.
3.The new points are defined as follows.
Draw a point PP at the midpoint between P and vertex 1(1/Probability of 3)
Draw a point PP at the midpoint between P and vertex 2(1/Probability of 3)
Draw a point PP at the midpoint between P and vertex 3(1/Probability of 3)
4.Repeat 3

(2) Burnsley fern

** Draw a pattern like a fern plant. ** It is odd that ferns with high symmetry can be created from random elements. In Rule 2 of making a Sierpinski triangle in (1), Change the rule to generate a new point $ (x_ {n + 1}, y_ {n + 1}) $ from $ (x_n, y_n) $ as follows.

Let $ r $ be a uniform random number of [0,1]

$(x_{n+1},y_{n+1}) = $ (0.5, 0.27 y_n) \ \ (r<0.02) (-0.139 x_n+0.263 y_n +0.57, 0.246 x_n + 0.224 y_n - 0.036) \ \ (0.02\leqq r<0.17) (0.17x_n-0.215 y_n+0.408, 0.222 x_n+0.176 y_n +0.0893) \ \ (0.17\leqq r<0.3) (0.781 x_n + 0.034 y_n +0.1075, -0.032 x_n + 0.739 y_n + 0.27 \ \ (0.03\leqq r\leqq1)

And.

(3) Fractal tree

** Generate a wooden pattern. ** **

In Rule 2 of 1) Making a Sierpinski triangle, Change the rule to generate a new point $ (x_ {n + 1}, y_ {n + 1}) $ from $ (x_n, y_n) $ as follows.

$(x_{n+1},y_{n+1}) = $ $ (0.05, 0.6 y_n) $ (Probability 10%) $ (0.05x_n, -0.5 y_n + 1.0 $ (probability 10%) $ (0.46x_n-0.15 y_n, 0.39 x_n + 0.38 y_n + 0.6 $ (probability 20%) $ (0.47x_n-0.15 y_n, 0.17 x_n + 0.42 y_n + 1.1 $ (probability 20%) $ (0.43x_n + 0.28y_n, -0.25x_n + 0.45y_n + 1.0 $ (probability 20%) $ (0.42x_n + 0.26y_n, -0.35x_n + 0.31y_n + 0.7 $ (probability 20%)


Code (1): Sierpinski triangle


"""
Sierpinski triangle

"""


import numpy as np
import matplotlib.pyplot as plt
from random import random, randrange
from math  import floor


fig=plt.figure()
anim=[]
#
a=np.zeros([3])
b=np.zeros([3])


X1,Y1=0,5
X2,Y2=5,0
X3, Y3=-5,0 

a[0], a[1], a[2] =X1, X2, X3
b[0],b[1], b[2]= Y1, Y2, Y3

plt.plot(a[0],b[0],'o',color='blue',markersize=1)
plt.plot(a[1],b[1],'o',color='blue',markersize=1)

plt.plot(a[2],b[2],'o',color='blue',markersize=1)

x=2.5
y=2.5
for i in range(15000):Main loop:Production rules(1)
    if i%1000 ==0:
        print(i)

    n=randrange(3)
    x=(x+a[n])/2
    y=(y+b[n])/2
    plt.plot(x,y,'o',color='blue',markersize=1)

plt.show()

Result (1): Sierpinski triangle

t.png

Every part is self-similar.


"""
Burnsley fern
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation #Import methods for creating animations
from random import random, randrange
from math  import floor


fig=plt.figure()
anim=[]
#

x=0.5
y=0.5
for i in range(15000):Main loop:Production rules(2)
    if i%1000==0:
        print(i)
    
    r=random()
    if r< 0.02:
        x,y = 0.5, 0.27*y
    
    elif 0.02 <= r <=0.17:
        x,y =  -0.139*x+0.263*y+0.57, 0.246*x+0.224*y-0.036
    
    elif 0.17 < r <=0.3:
        x,y = 0.17*x-0.215*y+0.408, 0.222*x+0.176*y+0.0893
    
    elif 0.3 < r <= 1:
        x,y = 0.781*x+0.034*y+0.1075, -0.032*x+0.739*y+0.27
    
    
    plt.plot(x,y,'o',color='blue',markersize=1)

plt.show()


Result (2): Burnsley fern

t.png

Enlarged leaves contain the same structure (self-similarity). However, as can be seen from the difference in leaves between branches, ferns as a whole do not have self-similarity.



"""
Fractal tree
13Aug. 2017
"""


import numpy as np
import matplotlib.pyplot as plt
from random import random, randrange
from math  import floor


fig=plt.figure()
anim=[]
#

x=0.5
y=0.5
for i in range(15000):Main loop:Production rules(3)
    if i%1000==0:
        print(i)
    
    r=random()
    if r<= 0.1:
        x,y = 0.05, 0.6*y
    
    elif 0.1 < r <=0.2:
        x,y =  -0.05*x, -0.5*y+1.0
    
    elif 0.2 < r <=0.4:
        x,y = 0.46*x-0.15*y, 0.39 *x+0.38*y+0.6
    
    elif 0.4 < r <= 0.6:
        x,y = 0.47*x-0.15*y, 0.17*x+0.42*y+1.1
        
    elif 0.6 < r <= 0.8:
        x,y = 0.43*x+0.28*y, -0.25*x+0.45*y+1.0
        
    elif 0.8 < r <= 1.0:
        x,y = 0.42*x+0.26*y, -0.35*x+0.31*y+0.7
        
    
    plt.plot(x,y,'o',color='blue',markersize=1)
   # anim.append(im)

plt.show()



Result (3): Fractal tree

tt.png


Addendum

● Fractal geometry has non-integer dimensions ([Fractal dimension](https://ja.wikipedia.org/wiki/%E3%83%95%E3%83%A9%E3%82%AF%E3%82%] BF% E3% 83% AB% E6% AC% A1% E5% 85% 83)). There seem to be various definitions of this dimension. I think the definition by Hausdorff Beskovich [1] is easy to understand. Let the straight line be one-dimensional, the triangle be two-dimensional, and the solid be three-dimensional, and the fractal dimension of the symmetrical figure is determined.

● Mathematically, the point set of both ferns and fractal trees in Bernsley is self-affine. Produced by a transformation with [2]. This seems to create self-similarity.


References

[1] Yoshihide Okumura (Department of Mathematics, Faculty of Science, Shizuoka University) pdf

[2] Commentary Page by Mitoku Hayakawa (Media Education Division, Education Information Infrastructure Center, Tohoku University) )

Recommended Posts

[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
Calculation of technical indicators by TA-Lib and pandas
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
[Scientific / technical calculation by Python] 3rd order spline interpolation, scipy
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
[Scientific / technical calculation by Python] Numerical integration, trapezoidal / Simpson law, numerical calculation, scipy
[Scientific / technical calculation by Python] Vector field visualization example, electrostatic field, matplotlib
[Scientific / technical calculation by Python] 1D-3D discrete fast Fourier transform, scipy
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[Scientific / technical calculation by Python] Numerical solution of one-dimensional unsteady heat conduction equation by Crank-Nicholson method (implicit method) and FTCS method (positive solution method), parabolic partial differential equation
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Control engineering] Calculation of transfer functions and state space models by Python
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
[Scientific / technical calculation by Python] Generation of non-uniform random numbers giving a given probability density function, Monte Carlo simulation