跳到正文

个人记录

Python画椭圆

zhangshuhang2011约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年2月8日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 1 分钟

版本

  1. v1当前

a 和 b 表示两个半轴长,t 表示绘制用的海龟(t_ellipse就是用默认海龟),fill 表示是否填充。

Python
import turtle
def ellipse(t,a,b,fill=False):
    trac = t.getscreen().tracer()
    t.getscreen().tracer(False)
    x,y = t.pos()
    d = t.isdown()
    t.pu()
    t.goto(-a+x,y)
    t.pd()
    if(fill): t.begin_fill()
    for i in range(-a,a+1):
        t.goto(i+x,(1-(i/a)**2)**0.5*b+y)
    for i in range(a,-a-1,-1):
        t.goto(i+x,-(1-(i/a)**2)**0.5*b+y)
    if(fill): t.end_fill()
    t.pu()
    t.goto(x,y)
    if(d): t.pd()
    t.getscreen().tracer(trac)
def t_ellipse(a,b,fill=False):
    ellipse(turtle.getturtle(),a,b,fill)
turtle.speed(0)
turtle.ht()
t_ellipse(200,100,True)
t_ellipse(100,200)
turtle.done()
原文