Sunday, 2 March 2025

Fern leaf pattern using python


 

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

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)