我想这大概是不可能的。
你低估龟的进取心。这里是我表现出你希望解决不对称问题最初的例子:
import math
from itertools import cycle
from turtle import Turtle, Screen
COLORS = cycle(['red', 'green', 'blue', 'yellow'])
def rotate_polygon(polygon, angle):
theta = math.radians(angle)
sin, cos = math.sin(theta), math.cos(theta)
return [(x * cos - y * sin, x * sin + y * cos) for x, y in polygon]
def fill_polygon(turtle, polygon, color):
turtle.color(color)
for vertex in polygon:
turtle.goto(vertex)
if not turtle.filling():
turtle.begin_fill()
turtle.end_fill()
# triangle cursor 5x in size and X translated 50 pixels
polygon = ((100, -28.85), (50, 57.75), (0, -28.85))
screen = Screen()
yertle = Turtle(visible=False)
yertle.penup()
for angle in range(0, 360, 30):
rotated_polygon = rotate_polygon(polygon, angle)
color = next(COLORS)
fill_polygon(yertle, rotated_polygon, color)
screen.exitonclick()
输出
我们真的希望最终的黄色三角形整齐下初始红色贴身携带,像一个不断上升的埃舍尔楼梯。我选择了这个插图,因为它有多个重叠,最后的黄色三角不仅在红色下面,而且在红色后面是绿色和蓝色。同样,最后的黄色之前的蓝色和绿色应低于红色。等
上面我的代码要复杂得多,需要吸取这个特殊的示意图,但需要额外的结构支持以下改进:
一种办法是制定出路口,而不是画出最新三角形的那部分。另一种方法是绘制新的三角形,但在应该被遮盖的三角形交叉点处重新着色。这后一种方法是我实现下,使用现有的Python函数来获取通过萨瑟兰-Hodgman多边形裁剪算法的交集:
import math
from itertools import cycle
from turtle import Turtle, Screen
COLORS = cycle(['red', 'green', 'blue', 'yellow'])
def clip(subjectPolygon, clipPolygon):
# obtain this code from:
# https://rosettacode.org/wiki/Sutherland-Hodgman_polygon_clipping#Python
return outputList
def rotate_polygon(polygon, angle):
theta = math.radians(angle)
sin, cos = math.sin(theta), math.cos(theta)
return [(x * cos - y * sin, x * sin + y * cos) for x, y in polygon]
def fill_polygon(turtle, polygon, color):
turtle.color(color)
for vertex in polygon:
turtle.goto(vertex)
if not turtle.filling():
turtle.begin_fill()
turtle.end_fill()
# triangle cursor 5x in size and X translated 50 pixels
polygon = ((100, -28.85), (50, 57.75), (0, -28.85))
screen = Screen()
yertle = Turtle(visible=False)
yertle.speed('slowest') # slowly so we can see redrawing
yertle.penup()
polygons = []
POLYGON, COLOR = 0, 1
for angle in range(0, 360, 30):
rotated_polygon = rotate_polygon(polygon, angle)
color = next(COLORS)
fill_polygon(yertle, rotated_polygon, color)
polygons.append((rotated_polygon, color))
# The -3 here is empirical and really should be calculated, an exercise for the reader
for forward, backward in enumerate(range(-3, 1 - len(polygons), -1)):
if polygons[forward] != polygons[backward]:
try:
intersection_polygon = clip(rotated_polygon, polygons[forward][POLYGON])
except (IndexError, ZeroDivisionError):
break # because clip() can throw an error when no intersection
if intersection_polygon:
fill_polygon(yertle, intersection_polygon, polygons[forward][COLOR])
else:
break # if no intersection, don't look any further
else:
break # avoid testing against polygons clockwise from this one (needs work)
screen.exitonclick()
输出
这是因为'turtle'把每个形状在最后。你将不得不重绘第一个形状的一部分来重叠最后一个形状。 (另外:你的帖子可能并不完全清楚,如果你发布了一些截图,这可能会有所帮助) –