from PIL import Image, ImageDraw
import math

SCALE = 4
SIZE  = 180 * SCALE   # 720px high-res canvas
PAD   = 10            # padding so strokes don't clip
LW    = SCALE * 2     # 8px at 720 → 2px after downscale

# Equilateral triangle, pointing up, centered in canvas
side  = SIZE - 2 * PAD
tri_h = side * math.sqrt(3) / 2
cx    = SIZE / 2
cy    = SIZE / 2

top_y    = cy - tri_h / 2
bottom_y = cy + tri_h / 2

apex = (cx, top_y)
bl   = (PAD, bottom_y)
br   = (SIZE - PAD, bottom_y)

# Inscribed circle: inradius = tri_h / 3, tangent to all three sides
inradius  = tri_h / 3
ic_center = (cx, bottom_y - inradius)

def new_img():
    img = Image.new('RGB', (SIZE, SIZE), 'white')
    return img, ImageDraw.Draw(img)

def save(img, name):
    img.save(name)
    img.resize((180, 180), Image.LANCZOS).save('thumb_' + name)

def draw_triangle(draw):
    draw.line([apex, bl, br, apex], fill='black', width=LW)

def draw_circle(draw):
    x0 = ic_center[0] - inradius
    y0 = ic_center[1] - inradius
    x1 = ic_center[0] + inradius
    y1 = ic_center[1] + inradius
    draw.ellipse([x0, y0, x1, y1], outline='black', width=LW)

def draw_vline(draw):
    draw.line([apex, (cx, bottom_y)], fill='black', width=LW)

# Triforce midpoints
mid_left  = ((cx + PAD) / 2,          (top_y + bottom_y) / 2)
mid_right = ((cx + SIZE - PAD) / 2,   (top_y + bottom_y) / 2)
mid_base  = (cx,                        bottom_y)

def draw_triforce(draw):
    draw.line([mid_left,  mid_right], fill='black', width=LW)
    draw.line([mid_left,  mid_base],  fill='black', width=LW)
    draw.line([mid_right, mid_base],  fill='black', width=LW)

# part1: triangle + inscribed circle + vertical line (Deathly Hallows)
img, draw = new_img()
draw_triangle(draw)
draw_circle(draw)
draw_vline(draw)
save(img, 'part1.png')

# part2: add Triforce subdivision
draw_triforce(draw)
save(img, 'part2.png')

# part3: add Poké Ball center dot
def draw_pokeball(draw):
    r = SCALE * 7
    x0 = ic_center[0] - r
    y0 = ic_center[1] - r
    x1 = ic_center[0] + r
    y1 = ic_center[1] + r
    draw.ellipse([x0, y0, x1, y1], fill='black')

draw_pokeball(draw)
save(img, 'part3.png')

# part4: altitudes from bottom-left and bottom-right vertices
def draw_altitudes(draw):
    draw.line([bl, mid_right], fill='black', width=LW)
    draw.line([br, mid_left],  fill='black', width=LW)

draw_altitudes(draw)
save(img, 'final.png')
img.resize((180, 180), Image.LANCZOS).save('thumb.png')

print("Done")
