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