これはおそらく不可能だと思っています。
あなたは亀の草刈りを過小評価します。ここでは、修正したい非対称の問題を呈する私の最初の例だ:
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()
OUTPUT
私たちは本当に初期の赤1の下できれいに隠れてする最後の黄色の三角形をしたいです、エッシャーの階段のように。私はそれが複数の重なりを持つので、このイラストを選んだ、最終的な黄色の三角形は、理論的には、赤を下回るだけでなく、赤に続く緑と青の両方を覆うべきである。同様に、最終的な黄色に先行する青色と緑色は、赤色を覆います。上記など
私のコードは、この特定のイラストを描くのに必要とされるよりも、より複雑であるが、以下の機能拡張をサポートするために追加の構造を必要とします:
一つのアプローチは、交差点をうまくすることはないだろう最新の三角形のその部分を描画します。もう1つの方法は、新しい三角形を描画することですが、アンダーラップする三角形の交点を再描画します。この後者のアプローチサザーランド-Hodgmanポリゴンクリッピングアルゴリズムを経由して交差点を取得するために、既存のPythonの関数を使用して、私は以下の実装です:turtle`はそれぞれを置く `ためだ
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()
OUTPUT
最後の一番上の形。どうにかして、最後の形状に重なるように最初の形状の一部を再描画する必要があります。 (また、あなたの投稿が誰にでも完全にはっきりしないかもしれない、いくつかのスクリーンショットを投稿した場合に役立つかもしれない) –