Python画椭圆

· · 个人记录

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

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()