编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
print("Hello world!   -  python.jsrun.net .")import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
import numpy as np
import io
import base64

# 配置Trinket的显示设置
plt.switch_backend('agg')  # 避免在Trinket中卡顿
plt.ioff()  # 关闭交互模式

# 创建图形
fig, ax = plt.subplots(figsize=(8, 10))
ax.set_facecolor('white')
ax.set_aspect('equal')
ax.axis('off')

# 蛋糕层参数
layers = [
    {'y': 0.2, 'width': 0.8, 'height': 0.25},   # 底层
    {'y': 0.5, 'width': 0.65, 'height': 0.2},   # 中层
    {'y': 0.75, 'width': 0.5, 'height': 0.18}    # 顶层
]

# 绘制蛋糕层
for layer in layers:
    # 蛋糕主体
    rect = plt.Rectangle((0.5 - layer['width']/2, layer['y']), 
                         layer['width'], layer['height'],
                         facecolor='none', edgecolor='black', linewidth=2)
    ax.add_patch(rect)
    
    # 顶部装饰圆球
    for i in range(8):
        x_pos = 0.5 - layer['width']/2 + layer['width']/7 * (i + 0.5)
        circle = plt.Circle((x_pos, layer['y'] + layer['height']), 
                            0.015, facecolor='none', edgecolor='black', linewidth=1.5)
        ax.add_patch(circle)
    
    # 底部糖霜滴落效果
    verts = []
    codes = []
    segment_width = layer['width'] / 8
    
    for i in range(7):
        start_x = 0.5 - layer['width']/2 + segment_width * (i + 1)
        verts.extend([
            [start_x, layer['y']],  # 起点
            [start_x - segment_width/3, layer['y'] - 0.03],  # 滴落底部
            [start_x + segment_width/3, layer['y'] - 0.03],
            [start_x, layer['y']]  # 返回起点
        ])
        codes.extend([
            Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4
        ])
    
    path = Path(verts, codes)
    patch = patches.PathPatch(path, facecolor='none', edgecolor='black', linewidth=1.2)
    ax.add_patch(patch)

# 绘制蜡烛
candle_width = 0.02
candle_height = 0.15
for i in range(3):
    cx = 0.4 + i * 0.1
    cy = layers[2]['y'] + layers[2]['height']
    
    # 蜡烛主体
    candle = plt.Rectangle((cx - candle_width/2, cy), 
                          candle_width, candle_height,
                          facecolor='none', edgecolor='black', linewidth=1.5)
    ax.add_patch(candle)
    
    # 火焰
    flame = plt.Circle((cx, cy + candle_height + 0.02), 
                       0.025, facecolor='none', edgecolor='black', linewidth=1.2)
    ax.add_patch(flame)
    
    # 火焰内波浪线
    theta = np.linspace(0, 2*np.pi, 20)
    r = 0.015 + 0.005 * np.sin(5*theta)
    x = cx + r * np.cos(theta)
    y = cy + candle_height + 0.02 + r * np.sin(theta)
    ax.plot(x, y, 'k-', linewidth=0.8)

# 添加装饰圆点
for layer in layers:
    for i in range(5):
        for j in range(3):
            dot_x = 0.5 - layer['width']/4 + layer['width']/4 * i
            dot_y = layer['y'] + layer['height']/4 * j
            dot = plt.Circle((dot_x, dot_y), 0.01, 
                            facecolor='none', edgecolor='black', linewidth=0.8)
            ax.add_patch(dot)

# 添加"th"文字
plt.text(0.5, layers[1]['y'] + layers[1]['height']/2, 
         "th", fontsize=40, 
         fontfamily='serif', fontweight='bold',
         ha='center', va='center')

# 适配Trinket显示和下载
buf = io.BytesIO()
plt.savefig(buf, format='svg', bbox_inches='tight', pad_inches=0.1)
plt.close(fig)