Skip to main content
 首页 » 编程设计

python之控制 Python turtle 中的形状重叠

2025年04月02日16zhujiabin

我正在编写一个程序,接受用户输入(1)要绘制的形状的边数,(2)要绘制多大,(3)要绘制多少,以及(4)有多少颜色使用。然后, turtle 将许多该形状围成一个圆圈,并在圆心共享一个顶点。

令我困扰的是,如果形状在最后重叠,最后的形状将位于所有形状之上,而我希望它们像其他形状一样隐藏在它们前面的形状后面。

我设法确定错误重叠的形状数量取决于形状的边数 - 例如,对于三角形,1-6 个形状没有重叠。对于 7-12,一个形状重叠不正确。对于 13-18,两个形状重叠不正确。等等。

到目前为止,我已经为它编写了将第一组和最后一组形状视为它们自己的东西,poly1 和 poly2,并且开始时我至少希望能够告诉它在 poly1 后面绘制 poly2 .

主要问题:这对 turtle 来说甚至可能吗?然后,如果是这样,我该怎么做? (使用 3.5)

编辑:我认为这可能是不可能的。我听到的一切都是 turtle 只能在现有形状之上绘制。但也有人建议我包括一个屏幕截图,以防它增加清晰度;这是 turtle 画的图像(当被告知要用 3 种不同颜色画 9 个三角形时)。

我的目标是让完整的三角形隐藏在 12 点钟位置的那个下方,但仍然在它之前的那个上方,就像最初绘制的一样。

请您参考如下方法:

I'm thinking this is probably impossible.

你低估了 turtle 的进取心。这是我的初始示例,展示了您希望修复的不对称问题:

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() 

输出