我正在练习我的Python技能。 我很好奇如何在不使用 turtle.circle(radius)的情况下使用turtles和函数绘制此花。
代码如下:
from turtle import Turtle, Screen
import math
COLORS = ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]
def draw_polygons(sides, area):
""" Draws a polygon with 'sides' sides and area 'area' """
for i, sd in enumerate(range(sides, 2, -1)):
side_length = math.sqrt(area / sd * 4 * math.atan(math.pi / sd))
# print("side length =", side_length)
a_color = COLORS[i % len(COLORS)]
rest.fillcolor(a_color)
rest.pendown()
rest.begin_fill()
for _ in range(sd):
rest.forward(side_length)
rest.left(360 / sd)
rest.end_fill()
rest.penup()
rest.forward(side_length / 2)
rest.right(30)
wn = Screen()
rest = Turtle()
rest.speed('fastest')
draw_polygons(20, 40_000)
rest.hideturtle()
wn.exitonclick()
更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址:https://www.linuxidc.com/Linux/2020-01/162131.htm