# Mandala mit vielen gefüllten Flächen
# - Keine Animation: tracer(0, 0) + update() einmalig
# - Schichten: konzentrische Ringe, gefüllte Blätter, Dreiecke
# - Übersichtlich und kommentiert

import turtle as t
import math

# ---------------------------
# Grundeinstellung
# ---------------------------
#t.setup(900, 900)
t.bgcolor("#0e0f13")
t.colormode(255)
t.tracer(0, 0)                # Animation aus
pen = t.Turtle(visible=False)
pen.speed(0)
pen.pensize(2)

# Farbpalette für Flächen
PALETTE = [
    "#ff6b6b", "#feca57", "#48dbfb", "#1dd1a1",
    "#5f27cd", "#ff9ff3", "#54a0ff", "#10ac84",
    "#c8d6e5", "#ee5253", "#341f97", "#5fbdff"
]

# ---------------------------
# Hilfsfunktionen
# ---------------------------
def color_i(i):                      # Farbe zyklisch wählen
    return PALETTE[i % len(PALETTE)]

def polar_xy(r, angle_deg):          # Polarkoordinaten -> (x, y)
    a = math.radians(angle_deg)
    return r * math.cos(a), r * math.sin(a)

def goto_polar(r, angle_deg):
    x, y = polar_xy(r, angle_deg)
    pen.penup(); pen.goto(x, y); pen.pendown()

def filled_circle(r, fill, outline=None):
    """Gefüllter Kreis mit Radius r um (0,0)."""
    if outline is None:
        outline = fill
    pen.color(outline, fill)
    pen.penup(); pen.goto(0, -r); pen.setheading(0)
    pen.pendown()
    pen.begin_fill(); pen.circle(r); pen.end_fill()

def ring_stack(radii, colors):
    """Konzentrierte Farbringe. Von groß nach klein zeichnen."""
    for i, r in enumerate(radii):
        filled_circle(r, colors[i % len(colors)])

def petal(angle, offset, petal_radius, extent, fill):
    """Gefülltes Blatt als Linsenform aus zwei Kreisbögen."""
    pen.color(fill, fill)
    pen.penup()
    goto_polar(offset, angle)
    pen.setheading(angle + 90)  # Tangente
    pen.pendown()
    pen.begin_fill()
    pen.circle(petal_radius, extent)     # Bogen 1
    pen.left(180 - extent)
    pen.circle(petal_radius, extent)     # Bogen 2 zurück
    pen.end_fill()

def petal_ring(count, offset, petal_radius, extent, palette_shift=0):
    """Ring aus gefüllten Blättern."""
    for i in range(count):
        angle = i * 360 / count
        petal(angle, offset, petal_radius, extent, color_i(i + palette_shift))

def triangle_ring_filled(r, count, base, height, palette_shift=0):
    """Ring aus gefüllten gleichschenkligen Dreiecken."""
    for i in range(count):
        angle = i * 360 / count
        # Basiszentrum B
        Bx, By = polar_xy(r, angle)
        # Tangential- und Normalvektoren
        ta = math.radians(angle + 90)
        na = math.radians(angle)
        tx, ty = math.cos(ta), math.sin(ta)
        nx, ny = math.cos(na), math.sin(na)
        # Eckpunkte
        Lx, Ly = Bx - (base/2)*tx, By - (base/2)*ty
        Rx, Ry = Bx + (base/2)*tx, By + (base/2)*ty
        Ax, Ay = Bx + height*nx,  By + height*ny
        pen.color(color_i(i + palette_shift), color_i(i + palette_shift))
        pen.penup(); pen.goto(Lx, Ly); pen.pendown()
        pen.begin_fill()
        pen.goto(Rx, Ry)
        pen.goto(Ax, Ay)
        pen.goto(Lx, Ly)
        pen.end_fill()

def dotted_ring(r, count, size, palette_shift=0):
    """Akzentpunkte als kleine gefüllte Kreise."""
    for i in range(count):
        angle = i * 360 / count
        pen.color(color_i(i + palette_shift))
        goto_polar(r, angle)
        pen.begin_fill(); pen.circle(size); pen.end_fill()

# ---------------------------
# Mandala-Aufbau
# ---------------------------

# 1) Große Farbringe als Basis
ring_stack(
    radii=[380, 340, 300, 260, 220, 180, 140, 100, 70, 45, 25],
    colors=["#18202a", "#20354b", "#274d6e", "#2f6a93", "#3891c1",
            "#6fd2ff", "#ffe082", "#ffa94d", "#ff6b6b", "#b197fc", "#ffffff"]
)

# 2) Außenkranz aus großen Blättern
petal_ring(count=48, offset=320, petal_radius=90, extent=120, palette_shift=0)

# 3) Dreiecks-Krone darüber
triangle_ring_filled(r=300, count=36, base=36, height=72, palette_shift=6)

# 4) Zweiter Blätterring versetzt
petal_ring(count=32, offset=230, petal_radius=70, extent=140, palette_shift=3)

# 5) Innere Dreiecke
triangle_ring_filled(r=180, count=24, base=30, height=55, palette_shift=9)

# 6) Rosettenblätter nahe Zentrum
petal_ring(count=16, offset=115, petal_radius=50, extent=150, palette_shift=12)

# 7) Akzentpunkte
dotted_ring(r=350, count=72, size=3, palette_shift=0)
dotted_ring(r=260, count=48, size=2, palette_shift=4)
dotted_ring(r=150, count=32, size=2, palette_shift=8)

# 8) Zentrum gefüllt
filled_circle(32, "#ffffff")
filled_circle(24, "#5f27cd")
filled_circle(12, "#ff9ff3")

# ---------------------------
# Ausgabe finalisieren
# ---------------------------
t.update()   # Einmaliges Rendern
t.done()
