Even more Mandalas, high school math and Python

python
mathematics
Published

December 29, 2024

central flower

We start by creating a central flower. first the central flower made up of 4 sided figures. The relative size of the flower in the total radius of the mandala, and the number of petals can be controlled.The figure below is made out of 12 kites with a shared vertex at the center of the “circle”.

Code
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import random
import math

def random_hex_color():
    return "#{:06x}".format(random.randint(0, 0xFFFFFF))
figure, axes = plt.subplots()

r = 10 #radius of outer circle

def central_flower(size,petals): 
    #size is the size of the flower as a portion of the total picture
    # petals is number of petals                           
    r_flower = r*size
    theta = np.linspace(0,2*np.pi,petals,endpoint=False)
    for t in theta:
        x1 = r_flower*np.cos(t)
        y1 = r_flower*np.sin(t)
        r_petal = r*size*0.3
        x2 = r_petal*np.cos(t+2*np.pi/petals)
        y2 = r_petal*np.sin(t+2*np.pi/petals)
        x3 = r_petal*np.cos(t-2*np.pi/petals)
        y3 = r_petal*np.sin(t-2*np.pi/petals)
        vertices = [(x1,y1),(x2,y2),(0,0),(x3,y3)]
        polygon = Polygon (vertices,facecolor=random_hex_color(),edgecolor='black', alpha=0.2)
        axes.add_artist(polygon)

central_flower(0.3,12)
        
#set the figure area square
axes.set_aspect(1)

#set axes limits 
axes.set_xlim(-r, r)
axes.set_ylim(-r, r)

# Hide axes labels
axes.set_xticks([])
axes.set_yticks([])

# Remove the border
for spine in axes.spines.values():
    spine.set_visible(False)

plt.show()

Rest follows!