Python中的控件形状重叠';乌龟是什么?
我正在写一个程序,它需要用户输入(1)要绘制的形状的边数,(2)要绘制多大,(3)要绘制多少,以及(4)要使用多少颜色。然后,海龟将这些形状的许多部分围成一个圆圈,所有这些形状在圆圈的中心共享一个顶点 让我烦恼的是,如果形状在最末端重叠,最后的形状将位于所有形状之上,而我希望它们像其他形状一样被塞进前面形状的后面 我已经设法确定错误重叠的形状的数量取决于形状的边数-例如,对于三角形,1-6个形状没有重叠。对于7-12,一个形状重叠不正确。对于13-18,两个形状的重叠不正确。等等Python中的控件形状重叠';乌龟是什么?,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我正在写一个程序,它需要用户输入(1)要绘制的形状的边数,(2)要绘制多大,(3)要绘制多少,以及(4)要使用多少颜色。然后,海龟将这些形状的许多部分围成一个圆圈,所有这些形状在圆圈的中心共享一个顶点 让我烦恼的是,如果形状在最末端重叠,最后的形状将位于所有形状之上,而我希望它们像其他形状一样被塞进前面形状的后面 我已经设法确定错误重叠的形状的数量取决于形状的边数-例如,对于三角形,1-6个形状没有重叠。对于7-12,一个形状重叠不正确。对于13-18,两个形状的重叠不正确。等等 到目前为止,我
到目前为止,我已经为它考虑了第一组和最后一组形状作为他们自己的事物,Pull 1和Pule2,并且开始时,我至少希望能够告诉它在Pull后面画Pule2。 主要问题:这对海龟来说是可能的吗?那么,如果是这样,我该怎么做呢?(使用3.5)
编辑:我认为这可能是不可能的。据我所知,海龟只能在现有形状的基础上画画。但也有人建议我加入一个截图,以防增加清晰度;这是海龟画的图片(当被告知画9个3种不同颜色的三角形时) 我的目标是让这个完整的三角形在12点时藏在这个三角形下面,但仍然在它之前的那个三角形上面,就像最初画的一样 我想这可能是不可能的 你低估了海龟的勇气。下面是我的初始示例,展示了您希望解决的不对称问题: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函数通过Sutherland 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()
输出
这是因为
海龟
将每个形状置于最后一个形状之上。您可能需要重新绘制第一个形状的一部分,以便与最后一个形状重叠。(另外:你的帖子可能不是对每个人都完全清楚,如果你发布一些截图可能会有所帮助)