Python用海龟图形绘制n角星

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

我的教授要求我们班编写一个 Python 函数,其功能如下:

在名为 star(turtle, n, d) 的函数中绘制一个边为 d 的规则 n 角星

这是我到目前为止的代码:

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 都不起作用。

感谢您的帮助!

python turtle-graphics
4个回答
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,'red',10 的输出示例


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)
(偶数)如下所示:


0
投票

我喜欢英国广播公司

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)

-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)`
© www.soinside.com 2019 - 2024. All rights reserved.