用Python的海龟图形绘制n角星

4

我的教授要求我们班级编写一个Python函数,实现以下功能:

使用边长为d的正n边形绘制一个n角星 - 函数名为star(turtle, n, d)

以下是我目前的代码:

def star(turtle, n, d):
    angle = (180-((180*(n-2))/n))*2
    for i in range(n):
        t.forward(d)
        t.left(angle)
    return angle

我遇到的问题是,我的函数只能绘制奇数角星形(5、7、9个角的星形)。当我要求它绘制一个偶数角星形时,它会输出一个具有n/2条边的多边形。因此,请求绘制8角星形会输出一个正方形,6角星形则为三角形,依此类推。
我尝试过多次修改角度公式,但它从未适用于任何给定的 n。
谢谢帮助!

我认为没有少于5个边的正星多边形 - martineau
我知道这个公式不能处理像6、8、10等数字。 - BryanLavinParmenter
1
如果有偶数条边,难道不需要两条不相连的线吗?(将六角星看作两个三角形) - mdurant
3个回答

3

通过使用GCD例程查找互质数并将失败视为异常,您可以使用相同的代码绘制大多数奇数和偶数尖星:

import sys
import turtle
from time import sleep

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

def normal_star(size, color, points):
    if points <= 4:
        raise ValueError('Not enough points')

    turtle.color(color)

    for coprime in range(points // 2, 1, -1):
        if gcd(points, coprime) == 1:

            print("({},{})".format(points, coprime), file=sys.stderr)

            start = turtle.position()

            for _ in range(points):
                turtle.forward(size)
                turtle.left(360.0 / points * coprime)

            turtle.setposition(start)

            return

    abnormal_star(size, color, points)

def abnormal_star(size, color, points):
    # deal with special cases here
    print("Exception:", points, file=sys.stderr)

for points in range(5, 20):
    turtle.reset()
    normal_star(200, 'red', points)
    sleep(5)

turtle.exitonclick()

对于5到20之间的点,这个算法只无法找到6的解决方案,您需要将其视为一个例外,即编写专门代码或告知用户这是一个无法处理的例外情况:

> python3 test.py
(5,2)
Exception: 6
(7,3)
(8,3)
(9,4)
(10,3)
(11,5)
(12,5)
(13,6)
(14,5)
(15,7)
(16,7)
(17,8)
(18,7)
(19,9)
(20,9)
>

针对参数200,'红色',10的输出示例如下:

enter image description here


1
这段代码将绘制一个具有大于5个顶点的星形。它需要两个参数:n 是顶点数,size 控制海龟步长的大小。
import turtle
turtle.showturtle()
turtle.shape("classic")

def turtle_star(n, size = 100):
    extent = 360 / n
    if n % 2 == 0:
        coords = []
        for a in range(0, n):
            turtle.penup()
            coords.append(turtle.pos())
            turtle.circle(size, extent)
        for b in range(0, len(coords)):
            if b % 2 == 0:
                turtle.pendown()
                turtle.goto(coords[b][0], coords[b][1])
            else:
                continue
        turtle.goto(coords[0][0], coords[0][1])
        turtle.penup()
        for c in range(0, (len(coords) + 1)):
            if c % 2 != 0:
                turtle.goto(coords[c][0], coords[c][1])
                turtle.pendown()
            else:
                continue
        turtle.goto(coords[1][0], coords[1][1])
    else:
        angle = 180 - (180 / n)
        for a in range(n):
            turtle.forward(size)
            turtle.right(angle)

turtle_star(11)(奇数)和 turtle(6)(偶数)如下所示:

enter image description here

enter image description here


1
“大于5的任意数量的点”似乎排除了奇数。对于7及以上的奇数,它只会画一条线。看起来在“else”子句中缺少一个循环。 - cdlane

-1

你的公式有点错误:

def star(turtle, n, d):
    for i in range(n):
        angle = 180.0 - 180.0 / n
        turtle.forward(d)
        turtle.right(angle)
        turtle.forward(d)`

这个答案未能解决原帖作者的主要问题,即“用偶数条边绘制一颗星”。 - cdlane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接