如何使用 Python 乌龟画一朵花
How to draw a flower using Python turtle
我正在练习 Python 技能。我很好奇如何在不使用 turtle.circle(radius)
.
的情况下使用海龟和函数绘制这朵花
https://i.stack.imgur.com/ZYUsz.png
此外,我做了一个像这样的多边形螺旋:
import turtle
import math
def draw_polygons():
""""Make t draws a polygon of side sd and length l"""
sd = 20
area = 50000
while sd >= 3:
side_length = math.sqrt(area / sd * 4 * math.atan(math.pi / sd))
for i in range(sd):
for a_color in ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]:
rest.fillcolor(a_color)
rest.begin_fill()
rest.forward(side_length)
rest.left(360/sd)
print("side length =", side_length)
rest.penup()
rest.forward(side_length / 2)
rest.pendown()
rest.right(30)
sd -= 1
rest = turtle.Turtle()
wn = turtle.Screen()
draw_polygons()
wn.exitonclick()
我想用不同的颜色填充每个多边形,我哪里做错了?或者下一步我应该做什么?
示例多边形螺旋形如下所示:
https://i.stack.imgur.com/4U9uD.png
乍一看我会说你的缩进是错误的。
for a_color in ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]:
rest.fillcolor(a_color)
rest.begin_fill()
在这里循环遍历所有颜色,然后填充形状。
颜色将始终为紫色,因为这是您在 rest.fillcolor
中设置的最后一种颜色
作为解决问题的一种方法,您可以创建颜色列表并通过调用 rest.fillcolor(colors[(i % len(colors)))
.
获取所需的颜色
你问的是两个不同的程序,我们先说第二个。除了你的颜色问题,你的循环逻辑在 begin_fill()
内部而不是内部循环外部是不正确的。那么让我们重新编写该程序:
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()
对于return您原来的问题,一种方法是在处理矩形的下一种颜色之前先放下矩形的每种颜色。这是对您的代码的修改,仅绘制图像的花瓣部分,因为我认为这就是您要问的问题。我只是目测了下面的一些距离和角度,您需要进行数学运算才能使它们正确:
from turtle import Turtle, Screen
import math
COLORS = ["green", "orange", "red", "yellow"]
def draw_polygons(area):
""" Draws a circle using rectangles with area 'area' """
side_length = math.sqrt(area * math.atan(math.pi / 4))
for a_color in COLORS:
for _ in range(12):
rest.fillcolor(a_color)
rest.forward(3 * side_length / 10)
rest.right(15)
rest.left(112) # I eyeballed this angle, need to do math
rest.pendown()
rest.begin_fill()
for _ in range(2):
rest.forward(side_length * 2)
rest.left(90)
rest.forward(side_length / 2)
rest.left(90)
rest.end_fill()
rest.penup()
rest.right(112) # undo rotation
rest.forward(3 * side_length / 10)
rest.right(15)
# I eyeballed these opposing parameters -- need to do the math
rest.forward(2 * side_length / 13)
rest.right(7.5)
wn = Screen()
rest = Turtle()
rest.speed('fastest')
rest.penup()
rest.sety(120)
draw_polygons(20_000)
rest.hideturtle()
wn.exitonclick()
我正在练习 Python 技能。我很好奇如何在不使用 turtle.circle(radius)
.
https://i.stack.imgur.com/ZYUsz.png
此外,我做了一个像这样的多边形螺旋:
import turtle
import math
def draw_polygons():
""""Make t draws a polygon of side sd and length l"""
sd = 20
area = 50000
while sd >= 3:
side_length = math.sqrt(area / sd * 4 * math.atan(math.pi / sd))
for i in range(sd):
for a_color in ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]:
rest.fillcolor(a_color)
rest.begin_fill()
rest.forward(side_length)
rest.left(360/sd)
print("side length =", side_length)
rest.penup()
rest.forward(side_length / 2)
rest.pendown()
rest.right(30)
sd -= 1
rest = turtle.Turtle()
wn = turtle.Screen()
draw_polygons()
wn.exitonclick()
我想用不同的颜色填充每个多边形,我哪里做错了?或者下一步我应该做什么?
示例多边形螺旋形如下所示:
https://i.stack.imgur.com/4U9uD.png
乍一看我会说你的缩进是错误的。
for a_color in ["red", "yellow", "blue", "brown", "pink", "green", "black", "orange", "purple"]:
rest.fillcolor(a_color)
rest.begin_fill()
在这里循环遍历所有颜色,然后填充形状。 颜色将始终为紫色,因为这是您在 rest.fillcolor
中设置的最后一种颜色作为解决问题的一种方法,您可以创建颜色列表并通过调用 rest.fillcolor(colors[(i % len(colors)))
.
你问的是两个不同的程序,我们先说第二个。除了你的颜色问题,你的循环逻辑在 begin_fill()
内部而不是内部循环外部是不正确的。那么让我们重新编写该程序:
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()
对于return您原来的问题,一种方法是在处理矩形的下一种颜色之前先放下矩形的每种颜色。这是对您的代码的修改,仅绘制图像的花瓣部分,因为我认为这就是您要问的问题。我只是目测了下面的一些距离和角度,您需要进行数学运算才能使它们正确:
from turtle import Turtle, Screen
import math
COLORS = ["green", "orange", "red", "yellow"]
def draw_polygons(area):
""" Draws a circle using rectangles with area 'area' """
side_length = math.sqrt(area * math.atan(math.pi / 4))
for a_color in COLORS:
for _ in range(12):
rest.fillcolor(a_color)
rest.forward(3 * side_length / 10)
rest.right(15)
rest.left(112) # I eyeballed this angle, need to do math
rest.pendown()
rest.begin_fill()
for _ in range(2):
rest.forward(side_length * 2)
rest.left(90)
rest.forward(side_length / 2)
rest.left(90)
rest.end_fill()
rest.penup()
rest.right(112) # undo rotation
rest.forward(3 * side_length / 10)
rest.right(15)
# I eyeballed these opposing parameters -- need to do the math
rest.forward(2 * side_length / 13)
rest.right(7.5)
wn = Screen()
rest = Turtle()
rest.speed('fastest')
rest.penup()
rest.sety(120)
draw_polygons(20_000)
rest.hideturtle()
wn.exitonclick()