Wednesday, 16 April 2025

Apollonian Gasket Pattern using Python

 


import matplotlib.pyplot as plt

import numpy as np

def draw_circle(ax, x, y, r):

    circle = plt.Circle((x, y), r, edgecolor='black', facecolor='none')

    ax.add_patch(circle)

def apollonian(ax, x, y, r, depth):

    if depth == 0:

        return

    draw_circle(ax, x, y, r)

    new_radius = r / 2

    apollonian(ax, x - r, y, new_radius, depth-1)

    apollonian(ax, x + r, y, new_radius, depth-1)

    apollonian(ax, x, y + r, new_radius, depth-1)

fig, ax = plt.subplots(figsize=(6, 6))

ax.set_aspect('equal', 'box')

ax.set_xlim(-1.5, 1.5)

ax.set_ylim(-1.5, 1.5)

initial_radius = 1

apollonian(ax, 0, 0, initial_radius, 5) 

ax.set_title('Apollonian Gasket')

plt.show()

#source code --> clcoding.com

Code Explanation:

1.Importing Necessary Libraries

import matplotlib.pyplot as plt

import numpy as np

matplotlib.pyplot: This is the main library for plotting 2D and 3D figures in Python. We're using it to create and display the plot.

numpy: This is a powerful numerical library in Python used for handling arrays and performing mathematical operations. It is useful for generating coordinate data.

 2. Drawing a Single Circle

def draw_circle(ax, x, y, r):

    circle = plt.Circle((x, y), r, edgecolor='black', facecolor='none')

    ax.add_patch(circle)

Purpose: This function draws a single circle on the plot.

x, y: Coordinates of the circle's center.

r: Radius of the circle.

plt.Circle(): This creates a circle object at the given position with the specified radius. The edgecolor='black' ensures that the circle's edge is outlined in black, and facecolor='none' ensures that the circle has no fill color (transparent).

 3. Recursive Function to Create the Apollonian Gasket

def apollonian(ax, x, y, r, depth):

    if depth == 0:

        return

    # Draw the current circle

    draw_circle(ax, x, y, r)

   

    # Calculate the radius of the three new tangent circles

    new_radius = r / 2

   

    # Recursively draw the three smaller tangent circles

    apollonian(ax, x - r, y, new_radius, depth-1)

    apollonian(ax, x + r, y, new_radius, depth-1)

    apollonian(ax, x, y + r, new_radius, depth-1)

Purpose: This function is the core of generating the Apollonian Gasket. It starts by drawing a circle and then recursively creates three smaller tangent circles inside the original circle.

Key Steps:

Base case (Stopping condition): If the depth reaches 0, the recursion stops. This prevents the function from going into infinite recursion.

Drawing the Circle: The current circle is drawn using draw_circle().

Calculating New Radius: Each recursive step reduces the radius of the new circles to half of the current radius (new_radius = r / 2). This makes the fractal progressively smaller.

Recursive Calls: The function then calls itself three times:

x - r, y: Creates a circle to the left of the current circle.

x + r, y: Creates a circle to the right of the current circle.

x, y + r: Creates a circle above the current circle.

 4. Setting Up the Plot

fig, ax = plt.subplots(figsize=(8, 8))

ax.set_aspect('equal', 'box')

ax.set_xlim(-1.5, 1.5)

ax.set_ylim(-1.5, 1.5)

Purpose: Sets up the plotting area.

plt.subplots(figsize=(8, 8)): Creates a new plot with a figure size of 8x8 inches.

ax.set_aspect('equal', 'box'): Ensures the aspect ratio of the plot is equal, so circles are not distorted. It makes sure the X and Y axes have the same scale.

ax.set_xlim(-1.5, 1.5) and ax.set_ylim(-1.5, 1.5): These functions define the range of the X and Y axes. It ensures the circles are centered and visible within the plot.

 5. Calling the Recursive Function

initial_radius = 1

apollonian(ax, 0, 0, initial_radius, 5)  # Depth=5 for deeper recursion

Purpose: This line starts the recursion.

The initial_radius = 1 defines the radius of the first circle (the largest one).

apollonian(ax, 0, 0, initial_radius, 5): This call starts the fractal drawing from the center of the plot (0, 0) with an initial radius of 1, and a recursion depth of 5. The depth determines how many iterations of circles will be drawn. Higher depth values make the fractal more intricate.

 6. Displaying the Plot

ax.set_title('Apollonian Gasket')

plt.show()

ax.set_title('Apollonian Gasket'): Adds a title to the plot.

plt.show(): This line displays the plot on the screen.

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)