【问题标题】:How to draw a dotted circle using python Turtle package如何使用 python Turtle 包绘制虚线圆
【发布时间】:2021-11-11 15:53:48
【问题描述】:

类似于所附图片的东西。我尝试修改我上网的代码。但我的代码进入无限循环 [1]:https://i.stack.imgur.com/HfKog.png

def draw_circle(radius):
    turtle.up()
    turtle.goto(0, radius)  # go to (0, radius)
    turtle.pendown()  # pen down
    times_y_crossed = 0
    x_sign = 1.0
    while times_y_crossed <= 1:
        turtle.dot(5, "Red")
        turtle.forward(5)  # move by 1/360
        turtle.right(1.0)
        turtle.penup()
        turtle.forward(5)  # move by 1/360
        turtle.right(1.0)
        x_sign_new = math.copysign(1, turtle.xcor())
        if x_sign_new != x_sign:
            times_y_crossed += 10
        x_sign = x_sign_new
    turtle.up()  # pen up
    return

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    您的代码存在一些问题,例如它不计入半径并且未设置笔的颜色,并且我检查过它为我做了半圈。 我向您展示了一个简单的工作示例,首先是一个虚线版本,因为您的原始代码看起来像您想要一个虚线圆圈

    import turtle
    import math
    def draw_circle_dashed(radius):
        turtle.up()
        turtle.goto(0, radius)  # go to (0, radius)
        times_y_crossed = 0
        dist=2*math.pi*radius/360
        turtle.pencolor("red")
        for _ in range(180):
            turtle.pendown()
            turtle.forward(dist)  # move by 1/360
            turtle.right(1.0)
            turtle.penup()
            turtle.forward(dist)  # move by 1/360
            turtle.right(1.0)
        turtle.up()  # pen up
        return
    draw_circle_dashed(200)
    

    还有一个虚线变体,因为问题标题

    import turtle
    import math
    def draw_circle_dotted(radius):
        turtle.up()
        turtle.goto(0, radius)  # go to (0, radius)
        dist=2*math.pi*radius/360
        for _ in range(360):
            turtle.dot(2,"red")
            turtle.forward(dist)  # move by 1/360
            turtle.right(1.0)
        turtle.up()  # pen up
        return
    draw_circle_dotted(300)
    

    【讨论】:

    • 为了清楚起见,这将绘制一个 虚线 圆圈,而不是 OP 希望和说明的 虚线 圆圈。虚线圆圈增加了复杂性。
    • 其实不然,虚线变体也很简单。
    • 代码似乎没有针对点直径进行调整,因此 OP 的点直径为 5 而不是 2 看起来不正确。这就是额外的复杂性所在。
    【解决方案2】:

    我们可以使用turtle自己的circle()方法来做这个,因为我们可以任意启动和停止它,像我们一样留下点:

    from turtle import Screen, Turtle
    from math import pi
    
    DOT_DIAMETER = 5
    
    def draw_circle(radius):
        turtle.penup()
    
        circumference = 2 * pi * radius
    
        dot_extent = 360 * DOT_DIAMETER*2 / circumference  # diameter to angle
    
        extent = 0
        while extent < 360:
            turtle.dot(DOT_DIAMETER)
            turtle.circle(radius, extent=dot_extent)
    
            extent += dot_extent
    
    screen = Screen()
    
    turtle = Turtle()
    turtle.color('red')
    
    draw_circle(100)
    
    turtle.hideturtle()
    screen.exitonclick()
    

    利用我们从该练习中学到的知识,现在让我们修复您的代码。我看到的问题是这样的:

    turtle.right(1.0)
    

    转向的角度取决于点的直径,但我们实际上必须计算它:

    from turtle import Screen, Turtle
    from math import pi, copysign
    
    DOT_DIAMETER = 5
    
    def draw_circle(radius):
        turtle.penup()
        turtle.sety(radius)
    
        diameter = 2 * radius
        circumference = pi * diameter
        dot_extent = 360 * DOT_DIAMETER / circumference  # diameter to angle
    
        times_y_crossed = 0
        x_sign = 1
    
        while times_y_crossed < 2:
            turtle.dot(DOT_DIAMETER, 'red')  # draw the dot
            turtle.right(dot_extent)
            turtle.forward(DOT_DIAMETER)
    
            turtle.right(dot_extent)  # draw the gap
            turtle.forward(DOT_DIAMETER)
    
            x_sign_new = copysign(1, turtle.xcor())
    
            if x_sign_new != x_sign:
                times_y_crossed += 1
                x_sign = x_sign_new
    
        turtle.pendown()
    
    screen = Screen()
    
    turtle = Turtle()
    turtle.color('red')
    
    draw_circle(100)
    
    turtle.hideturtle()
    screen.exitonclick()
    

    通过不计算角度,并使用 1.0 的固定角度,您偏离了两倍。

    【讨论】:

    • 非常感谢。
    猜你喜欢
    • 2018-03-23
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多