Python Turtle:使用circle()方法绘制同心圆

问题描述 投票:0回答:5

我正在展示用 Python 的 Turtle 模块绘制的孙子图案, 他要求看同心圆。 我觉得用乌龟的

circle()
来画会更快 而不是编写自己的代码来生成圆。哈!我被困住了。 我看到所产生的圆从乌龟的圆周开始 当前位置及其绘制方向取决于海龟的当前位置 运动方向,但我不知道我需要做什么才能得到 同心圆。 我目前对有效的生产方式不感兴趣 同心圆:我想看看我必须做什么才能得到 这个工作方式:

def turtle_pos(art,posxy,lift):
    if lift:
        art.penup()
        art.setposition(posxy)
        art.pendown()

def drawit(tshape,tcolor,pen_color,pen_thick,scolor,radius,mv):
    window=turtle.Screen() #Request a screen
    window.bgcolor(scolor) #Set its color

    #...code that defines the turtle trl

    for j in range(1,11):
        turtle_pos(trl,[trl.xcor()+mv,trl.ycor()-mv],1)
        trl.circle(j*radius)

drawit("turtle","purple","green",4,"black",20,30)
python turtle-graphics
5个回答
5
投票

你可以这样做:

import turtle

turtle.penup()
for i in range(1, 500, 50):
    turtle.right(90)    # Face South
    turtle.forward(i)   # Move one radius
    turtle.right(270)   # Back to start heading
    turtle.pendown()    # Put the pen back down
    turtle.circle(i)    # Draw a circle
    turtle.penup()      # Pen up while we go home
    turtle.home()       # Head back to the start pos

这将创建下图:

enter image description here

基本上,它将海龟向下移动一个半径长度,以将所有圆圈的中心点保持在同一位置。


2
投票

来自文档:

中心是海龟左侧的半径单位。

所以当你开始画一个圆时,无论乌龟在哪里,圆的中心都会向右有一段距离。在每个圆圈之后,只需向左或向右移动一定数量的像素,然后绘制另一个圆圈,其半径根据海龟移动的距离进行调整。例如,如果您绘制一个半径为 50 像素的圆,然后向右移动 10 像素,您将绘制另一个半径为 40 的圆,并且这两个圆应该是同心的。


0
投票

所以现在我给你可以绘制同心圆的确切代码。

import turtle
t=turtle.Turtle()
for i in range(5):
  t.circle(i*10)
  t.penup()
  t.setposition(0,-(i*10))
  t.pendown()
turtle.done()

0
投票

目前我对有效的生产方式不感兴趣 同心圆:我想看看我必须做什么才能到达this 工作

为了解决OP的问题,对其原始代码进行更改以使其工作是微不足道的:

turtle_pos(trl, [trl.xcor() + mv, trl.ycor() - mv], 1)
trl.circle(j * radius)

变成:

turtle_pos(trl, [trl.xcor(), trl.ycor() - mv], 1)
trl.circle(j * mv + radius)

经过上述修复和一些样式更改的完整代码:

import turtle

def turtle_pos(art, posxy, lift):
    if lift:
        art.penup()
        art.setposition(posxy)
        art.pendown()

def drawit(tshape, tcolor, pen_color, pen_thick, scolor, radius, mv):
    window = turtle.Screen()  # Request a screen
    window.bgcolor(scolor)  # Set its color

    #...code that defines the turtle trl
    trl = turtle.Turtle(tshape)
    trl.pencolor(pen_color)
    trl.fillcolor(tcolor)  # not filling but makes body of turtle this color
    trl.width(pen_thick)

    for j in range(10):
        turtle_pos(trl, (trl.xcor(), trl.ycor() - mv), True)
        trl.circle(j * mv + radius)

    window.mainloop()

drawit("turtle", "purple", "green", 4, "black", 20, 30)


0
投票
from turtle import *
import turtle
for i in range(10,100,10):
    turtle.ht()
    turtle.pu()
    turtle.goto(0,-i)
    turtle.pd()
    turtle.circle(i)
© www.soinside.com 2019 - 2024. All rights reserved.