You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python Turtle库绘制圆形组成的字母T与S?

使用Python Turtle绘制圆形组合的字母T和S

核心思路是通过排列多个小圆形来构成字母轮廓,以下是可直接运行的代码实现,包含通用圆绘制函数、字母T和S的绘制逻辑:

代码实现

import turtle
import math

def draw_circle(t, x, y, radius, color):
    """绘制单个填充圆形的工具函数"""
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor(color)
    t.begin_fill()
    t.circle(radius)
    t.end_fill()

def draw_letter_t(t, radius, spacing, color):
    """绘制由圆形组合的字母T"""
    # 绘制顶部横杠:11个水平排列的圆
    start_x = -5 * (radius + spacing)
    start_y = 3 * (radius + spacing)
    for i in range(11):
        draw_circle(t, start_x + i*(2*radius + spacing), start_y, radius, color)
    
    # 绘制中间竖杠:7个垂直排列的圆
    center_x = start_x + 5*(2*radius + spacing)
    for i in range(7):
        draw_circle(t, center_x, start_y - i*(2*radius + spacing), radius, color)

def draw_letter_s(t, radius, spacing, color):
    """绘制由圆形组合的字母S"""
    # 上半部分圆弧:从左上到右下的曲线
    center_x1, center_y1 = 0, 2*(radius + spacing)
    for angle in range(180, 360, 15):
        rad = math.radians(angle)
        x = center_x1 + (3*(radius + spacing)) * math.cos(rad)
        y = center_y1 + (3*(radius + spacing)) * math.sin(rad)
        draw_circle(t, x, y, radius, color)
    
    # 下半部分圆弧:从右下到左上的曲线
    center_x2, center_y2 = 0, -2*(radius + spacing)
    for angle in range(0, 180, 15):
        rad = math.radians(angle)
        x = center_x2 + (3*(radius + spacing)) * math.cos(rad)
        y = center_y2 + (3*(radius + spacing)) * math.sin(rad)
        draw_circle(t, x, y, radius, color)

def main():
    screen = turtle.Screen()
    screen.bgcolor("white")
    t = turtle.Turtle()
    t.speed(0)  # 设置最快绘制速度
    
    # 绘制字母T(左侧位置)
    t.penup()
    t.goto(-150, 0)
    t.pendown()
    draw_letter_t(t, 10, 2, "blue")
    
    # 绘制字母S(右侧位置)
    t.penup()
    t.goto(150, 0)
    t.pendown()
    draw_letter_s(t, 10, 2, "red")
    
    t.hideturtle()
    turtle.done()

if __name__ == "__main__":
    main()

关键细节说明

  • draw_circle:封装重复绘制逻辑,减少冗余代码,参数控制圆的位置、大小和颜色。
  • 字母T:通过水平循环+垂直循环排列小圆,分别构成横杠和竖杠,调整range()的数值可改变横杠长度、竖杠高度。
  • 字母S:利用三角函数计算圆弧上的点,分两段圆弧拼接成S形,角度步长(15度)越小,曲线越平滑,同时圆的数量也会增加。
  • 可调参数:修改radius(小圆大小)、spacing(圆间距)、color(字母颜色)可快速调整字母外观。

内容的提问来源于stack exchange,提问作者Çölden Kaktüs Çalan Bedevi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 22:55:27