import numpy as np
import matplotlib.pyplot as plt
def barnsley_fern(n):
x=[0]
y=[0]
for _ in range(n):
r=np.random.random()
if r<0.01:
x.append(0)
y.append(0.16*y[-1])
elif r<0.86:
x.append(0.85*x[-1]+0.04*y[-1])
y.append(-0.04*x[-2]+0.85*y[-1]+1.6)
elif r<0.93:
x.append(0.2*x[-1]-0.26*y[-1])
y.append(0.23*x[-2]+0.22*y[-1]+1.6)
else:
x.append(-0.15*x[-1]+0.28*y[-1])
y.append(0.26*x[-2]+0.24*y[-1]+0.44)
return x,y
n=100000
x,y=barnsley_fern(n)
plt.figure(figsize=(6,10))
plt.scatter(x,y,s=0.2,color='green')
plt.axis('off')
plt.title('Fern leaf pattern')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Necessary Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy is imported as np to handle numerical operations, such as generating random numbers.
matplotlib.pyplot is imported as plt to plot the fern.
2. Defining the Function barnsley_fern(n)
def barnsley_fern(n):
This function generates n points using Iterated Function Systems (IFS) to form the Barnsley Fern.
3. Initializing Lists to Store Coordinates
x = [0]
y = [0]
x and y are initialized as lists with the first point at (0,0).
These lists will store the x and y coordinates of the fern as points are generated.
4. Iterating n Times to Generate the Fern
for _ in range(n):
The loop runs n times, generating n points.
5. Generating a Random Number for Transformation Selection
r = np.random.random()
A random number r between 0 and 1 is generated.
This number determines which transformation (out of 4) will be applied to the current point.
6. Applying One of the Four Transformations
if r < 0.01:
x.append(0)
y.append(0.16 * y[-1])
Transformation 1 (Stem) – Probability 1%
Maps all points to the stem of the fern.
Keeps x = 0, and y is scaled down (0.16*y[-1]).
elif r < 0.86:
x.append(0.85 * x[-1] + 0.04 * y[-1])
y.append(-0.04 * x[-2] + 0.85 * y[-1] + 1.6)
Transformation 2 (Main Leaf) – Probability 85%
Forms the largest part of the fern.
The new (x, y) point is calculated using a linear transformation.
elif r < 0.93:
x.append(0.2 * x[-1] - 0.26 * y[-1])
y.append(0.23 * x[-2] + 0.22 * y[-1] + 1.6)
Transformation 3 (Left Leaflets) – Probability 7%
Creates the left leaflets of the fern.
Uses different coefficients in the affine transformation.
else:
x.append(-0.15 * x[-1] + 0.28 * y[-1])
y.append(0.26 * x[-2] + 0.24 * y[-1] + 0.44)
Transformation 4 (Right Leaflets) – Probability 7%
Forms the right leaflets of the fern.
7. Returning the Generated Points
return x, y
The function returns the lists x and y, which contain all generated points.
8. Calling the Function to Generate 100,000 Points
n = 100000
x, y = barnsley_fern(n)
Calls barnsley_fern(n) with n = 100000 to generate 100,000 points.
The returned values x and y contain the coordinates of the fern.
9. Plotting the Fern
plt.figure(figsize=(6, 10))
Creates a figure with a 6x10 inches size.
plt.scatter(x, y, s=0.2, color='green')
scatter() plots the points from x and y.
s=0.2 makes the points very small for a finer look.
color='green' colors the fern green.
10. Removing Axes and Displaying the Plot
plt.axis('off')
Hides the axes for a cleaner look.
plt.title("Fern Leaf Pattern")
Adds a title to the plot.
plt.show()
Displays the Barnsley Fern.
0 Comments:
Post a Comment