Я сделал некоторый код и хотел экспортировать холст как изображение, однако каждый раз, когда я запускаю код, черепаха скрывается, фигура рисуется, затем черепаха по какой-то причине появляется снова, и изображение сохраняется. Я пытался добавить t.ht()
к каждой строке в разделе сохранения изображений, но безрезультатно. Весь код размещен ниже
import turtle
from itertools import combinations
from math import pi, cos, sin
def points(n, r):
"""Generate a list of points making the vertices of a regular n-gon centred at the origin"""
return [(r * cos(2 * pi * i / n), r * sin(2 * pi * i / n)) for i in range(n)]
def rotate(points, a):
"""Rotate a given list of points by the angle a (in radians) about the origin"""
return [(x*cos(a) - y*sin(a), y*cos(a) + x*sin(a)) for x, y in points]
def scale(points, s):
"""Scale a given list of points by the scale factor s about the origin"""
return [(x*s, y*s) for x, y in points]
def complete_poly(points, n):
"""Draw a connected graph using the points specified"""
t.pu()
t.goto(points[-1])
t.pd()
for ix, iy in combinations(range(n), 2):
t.goto(points[ix])
t.goto(points[iy])
def incomplete_poly(points, n):
"""Draw a graph connecting only nodes seperating by a single node or none"""
t.pu()
t.goto(points[-1])
t.pd()
for inner in range(n):
t.pu()
t.goto(points[inner])
t.pd()
t.goto(points[(inner+2)%n])
def outer_poly(points, n):
"""Draw only the edges of an n-gon defined by points"""
t.pu()
t.goto(points[-1])
t.pd()
for side in range(n):
t.goto(points[side])
t = turtle.Turtle()
t.lt(90)
t.speed(0)
t.ht()
# n is the number of sides of the polygon it draws
# i is the number of iterations
# r is the starting radius of the polygon
n = 7
i = 10
r = 200
s = 2*cos(pi/n) - 1/cos(pi/n)
p = points(n, r)
outer_poly(p, n)
for _ in range(i):
incomplete_poly(p, n)
p = scale(rotate(p, pi/n), s)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="7-intergon.eps")
turtle.done()