python - 如何在 Python 中将 20 只海龟放在一个圆圈上?
问题描述
我必须把 20 只海龟放在一个有 20 条边的多边形的顶点上,所以它们会在一个有规则间距的圆上。
我有 Turtle 类,我想把这 20 只乌龟放在哪里。我知道如何将更多海龟放在一个文件中,但我怎样才能将它们放在一个圆圈中?
# Already working
class Turtle:
def __init__(self, x, y):
self.x = x
self.y = y
self.heading = 0
self.lines = []
def left(self, angle):
self.heading -= angle
def right(self, angle):
self.heading += angle
def forward(self, d):
nx = self.x + d * math.cos(self.heading * math.pi / 180)
ny = self.y + d * math.sin(self.heading * math.pi / 180)
self.lines.append((self.x, self.y, nx, ny))
self.x, self.y = nx, ny
def save(filename, lines):
f = open(filename, "w")
f.write('<svg viewBox="-500 -500 1000 1000">')
s = '<line x1="{}" y1="{}" x2="{}" y2="{}" style="{}" />'
for i in lines:
for x1, y1, x2, y2 in i:
f.write(s.format(x1, y1, x2, y2, "stroke:black;stroke-width:1"))
f.write("</svg>")
f.close()
# Here is just a try to put more turtles with more lines in one file
# But I can't do this with 20 turtles...
def set_turtles():
global all_lines
turtle_names = []
t_red = Turtle(-100, 0)
turtle_names.append(t_red)
t1 = Turtle(0, 100)
turtle_names.append(t1)
t2 = Turtle(0, -100)
turtle_names.append(t2)
for turtle in turtle_names:
for i in range(4):
turtle.forward(10)
turtle.left(90)
all_lines.append(turtle.lines)
save("drawing_one.html", all_lines)
f.close()
解决方案
与其使用自定义的海龟类来做任何事情,不如使用 Python 的海龟类来帮助解决问题。我们仍然需要我们自己的自定义海龟类(未调用Turtle
),它可以记录它在foward()
调用时生成的行。另外实现一个静态方法将所有自定义海龟绘制的所有线条转储到 SVG 文件。但是我们可以简单地继承所有其他方法!
使用这种方法,要记录线条,我们不需要三角函数,而是在调用之前super().forward(...)
记录乌龟的位置,然后再记录乌龟的位置。
最后,我们不需要跟踪我们的自定义海龟,而是使用Screen().turtles()
并过滤掉那些不是我们自定义类的实例的海龟。
from turtle import Screen, Turtle
RADIUS = 100
class SVG_Turtle(Turtle):
TEMPLATE = '<line x1="{}" y1="{}" x2="{}" y2="{}" style="{}" />'
STYLE = 'stroke:black;stroke-width:1'
HEADER = '<svg viewBox="-500 -500 1000 1000">'
FOOTER = '</svg>'
def __init__(self, position):
super().__init__(visible=False)
self.lines = []
self.penup()
self.goto(position)
def forward(self, distance):
position = self.position()
super().forward(distance)
self.lines.append((position, self.position()))
@staticmethod
def save(filename):
with open(filename, 'w') as file:
file.write(SVG_Turtle.HEADER)
for turtle in screen.turtles():
if not isinstance(turtle, SVG_Turtle):
continue
for ((x1, y1), (x2, y2)) in turtle.lines:
file.write(SVG_Turtle.TEMPLATE.format(x1, y1, x2, y2, SVG_Turtle.STYLE))
file.write(SVG_Turtle.FOOTER)
screen = Screen()
yertle = Turtle(visible=False) # standard Python turtle to lay down our custom ones
yertle.penup()
yertle.sety(-RADIUS)
screen.tracer(False)
for _ in range(20):
turtle = SVG_Turtle(yertle.position())
turtle.setheading(turtle.towards(0, 0)) # Optional visual detail I added
yertle.circle(RADIUS, 360 / 20, 20)
for turtle in screen.turtles():
if isinstance(turtle, SVG_Turtle):
for _ in range(4):
turtle.forward(10)
turtle.left(90)
screen.tracer(True)
SVG_Turtle.save("drawing.html")
即使不允许你使用 Python 的海龟来帮助解决你自己的海龟问题,这也应该能给你一些关于如何继续以及如何组织代码的想法。
推荐阅读
- javascript - IE 11 中的 AddEventListener 函数变量范围问题
- sql-server - 如何有效地更改 SQL 中的 In 子句
- python - Tkinter Treeview 操作单元格中的值
- java - 任何用于 Soap Api 自动化测试的开源工具?
- scala - 更改scala spark中的日期格式后列的值更改
- sql - 在 JOIN 子句中使用 WHERE 时也显示空值
- python - Python:使用新键从字典输入中返回字典列表
- algorithm - 如何证明T(n)=T(n-1)+log n =Θ(nlog n)的复杂度
- javascript - 映射函数不将数据返回到渲染
- regex - 拆分字符串并在数组中获取分隔符