import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Function to compute the main bulb for k-periodic points
def main_bulb(k, num_points=1000):
    theta = np.linspace(0, 2 * np.pi, num_points)
    if k == 2:  # Main cardioid
        x = 0.25 * (1 - np.cos(theta))
        y = 0.25 * np.sin(theta)
    else:  # Surrounding bulbs for higher k
        p = 1 - 1 / k
        x = p * np.cos(theta)
        y = p * np.sin(theta)
    return x, y

# Setup the figure and axis
fig, ax = plt.subplots()
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal')
ax.set_title("Main Bulb of Mandelbrot Set")

line, = ax.plot([], [], 'b-', lw=2)

# Initialization function
def init():
    line.set_data([], [])
    return line,

# Animation function
def animate(k):
    x, y = main_bulb(k)
    line.set_data(x, y)
    ax.set_title(f"Main Bulb of Mandelbrot Set (k={k})")
    return line,

# Create the animation
ani = FuncAnimation(fig, animate, init_func=init, frames=range(2, 10), interval=1000, blit=True)

plt.show()
