📜  在Python中使用 Turtle 绘制椭圆

📅  最后修改于: 2022-05-13 01:55:39.029000             🧑  作者: Mango

在Python中使用 Turtle 绘制椭圆

先决条件:海龟编程基础

Turtle是Python中的一个内置模块。它使用屏幕(纸板)和海龟(笔)提供绘图。要在屏幕上绘制一些东西,我们需要移动海龟(笔)。要移动海龟,有一些函数,即 forward()、backward() 等。

方法:

使用以下步骤:

  • 进口龟
  • 将椭圆分成四个圆弧
  • 定义一种方法来成对形成这些弧
  • 调用函数。

下面是实现:

Python3
# import package
import turtle
 
# method to draw ellipse
def draw(rad):
     
  # rad --> radius of arc
  for i in range(2):
     
    # two arcs
    turtle.circle(rad,90)
    turtle.circle(rad//2,90)
 
# Main section
# tilt the shape to negative 45
turtle.seth(-45)
 
# calling draw method
draw(100)


Python3
# import package and making object
import turtle
screen = turtle.Screen()
 
# method to draw ellipse
def draw(rad):
     
  # rad --> radius for arc
    for i in range(2):
        turtle.circle(rad,90)
        turtle.circle(rad//2,90)
 
# Main Section
# Set screen size
screen.setup(500,500)
 
# Set screen color
screen.bgcolor('black')
 
# Colors
col=['violet','blue','green','yellow',
     'orange','red']
 
# some integers
val=10
ind=0
 
# turtle speed
turtle.speed(100)
 
# loop for multiple ellipse
for i in range(36):
     
    # oriented the ellipse at angle = -val
    turtle.seth(-val)
     
    # color of ellipse
    turtle.color(col[ind])
     
    # to access different color
    if ind==5:
        ind=0
    else:
        ind+=1
     
    # calling method
    draw(80)
     
    # orientation change
    val+=10
 
# for hiding the turtle
turtle.hideturtle()


输出 :

使用椭圆形绘制设计

使用以下步骤:

  • 进口龟
  • 设置画面
  • 将椭圆分成四个圆弧
  • 定义一种方法来成对形成这些弧
  • 多次调用该函数以获得不同的颜色。

下面是实现:

Python3

# import package and making object
import turtle
screen = turtle.Screen()
 
# method to draw ellipse
def draw(rad):
     
  # rad --> radius for arc
    for i in range(2):
        turtle.circle(rad,90)
        turtle.circle(rad//2,90)
 
# Main Section
# Set screen size
screen.setup(500,500)
 
# Set screen color
screen.bgcolor('black')
 
# Colors
col=['violet','blue','green','yellow',
     'orange','red']
 
# some integers
val=10
ind=0
 
# turtle speed
turtle.speed(100)
 
# loop for multiple ellipse
for i in range(36):
     
    # oriented the ellipse at angle = -val
    turtle.seth(-val)
     
    # color of ellipse
    turtle.color(col[ind])
     
    # to access different color
    if ind==5:
        ind=0
    else:
        ind+=1
     
    # calling method
    draw(80)
     
    # orientation change
    val+=10
 
# for hiding the turtle
turtle.hideturtle()

输出 :