Everyone must have thought about drawing an ellipse with turtle in Python. After thinking about it for a long time, I finally figured out the ellipse, but I don’t know if this method is the simplest or correct. Please correct me if there is any error. , If you find it easy to use, just use it, hahaha The
code is as follows:
import turtle
def half_a(x):
a = x
b = 90
while True:
turtle.circle(a, 1)
a = a - x / 100
b = b - 1
if b == 0:
break
def half_b(x):
a = x * 0.1
b = 90
while True:
turtle.circle(a, 1)
a = a + x / 100
b = b - 1
if b == 0:
break
def ellipse(x):
turtle.speed(0) #设置画笔速度
turtle.color("red") #设置画笔颜色
turtle.pensize(1) #设置画笔粗细
half_a(x)
half_b(x)
half_a(x)
half_b(x)
turtle.hideturtle() # 隐藏箭头显示
ellipse(100) #可根据自己的需要改变这个值
turtle.mainloop()
Operating effect:
If you want to draw an ellipse in another direction, you only need to exchange a piece of code. The
code is as follows:
import turtle
def half_a(x):
a = x
b = 90
while True:
turtle.circle(a, 1)
a = a - x / 100
b = b - 1
if b == 0:
break
def half_b(x):
a = x * 0.1
b = 90
while True:
turtle.circle(a, 1)
a = a + x / 100
b = b - 1
if b == 0:
break
def ellipse(x):
turtle.speed(0) #设置画笔速度
turtle.color("red") #设置画笔颜色
turtle.pensize(1) #设置画笔粗细
half_b(x)
half_a(x)
half_b(x)
half_a(x)
turtle.hideturtle() # 隐藏箭头显示
ellipse(100) #可根据自己的需要改变这个值
turtle.mainloop()
running result: