最初に描かれた三角形の中点を見つけるにはどうすればよいですか?三角形の中に複数の三角形があるシェルピンスキーの三角形を作成する必要があります。これまでのところ、以下に示す最初の三角形のコードがあります。
import pygame
pygame.init()
colors = [pygame.Color(0, 0, 0, 255), # Black
pygame.Color(255, 0, 0, 255), # Red
pygame.Color(0, 255, 0, 255), # Green
pygame.Color(0, 0, 255, 255), # Blue
pygame.Color(255, 255, 255, 255)] # White
# Each of these constants is the index to the corresponding pygame Color object
# in the list, colors, defined above.
BLACK = 0
RED = 1
GREEN = 2
BLUE = 3
WHITE = -1
height = 640
width = 640
size = [width, height]
screen = pygame.display.set_mode(size)
screen.fill(WHITE)
def draw_triangle(p1, p2, p3, color, line_width, screen):
p1 = [5, height - 5]
p2 = [(width - 10) / 2, 5]
p3 = [width - 5, height - 5]
pygame.draw.polygon(screen, 0, [p1, p2, p3], 2)
pygame.display.flip()
def find_midpoint(p1, p2):
def sierpinski(degree, p1, p2, p3, color, line_width, screen):
残りの両方の関数は、シェルピンスキーの三角形を完成させるために必要なものです。まず、中点を見つける関数を作成し、次にそれらの三角形内にシェルピンスキーの三角形と呼ばれる複数の三角形を作成する関数を作成します。
degree
議論の目的が何であるかわかりません、おそらくそれは再帰的な深さを制限することについてですか?
再帰的なシェルピンスキー関数を使用した質問に基づく例を次に示します。
import pygame
def draw_triangle(p1, p2, p3, color, line_width, screen):
pygame.draw.polygon(screen, color, [p1, p2, p3], line_width)
def midpoint(p1, p2):
""" Return the mid-point on the line p1 to p2 """
x1, y1 = p1
x2, y2 = p2
x_mid = (x1 + x2) // 2
y_mid = (y1 + y2) // 2
return (x_mid, y_mid)
def sierpinski(degree, p1, p2, p3, color, line_width, screen):
# p1 → bottom left, p2 → bottom right, p3 → top
# recursive function so check for exit condition first
if abs(p1[0] - p2[0]) <= 2 and abs(p2[0] - p3[0]) <= 2 and abs(p1[0] - p3[0]) <= 2:
return
draw_triangle(p1, p2, p3, color, line_width, screen)
a = midpoint(p1, p2)
b = midpoint(p1, p3)
c = midpoint(p2, p3)
# skip the centre triangle
sierpinski(degree, p1, a, b, color, line_width, screen)
sierpinski(degree, p2, a, c, color, line_width, screen)
sierpinski(degree, p3, b, c, color, line_width, screen)
height = 640
width = 640
pygame.init()
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.display.set_caption("Sierpiński")
clock = pygame.time.Clock()
update_screen = True
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.VIDEORESIZE:
width, height = event.dict["size"]
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
update_screen = True
if update_screen:
# only draw the screen when required
screen.fill(pygame.color.Color("white"))
# determine initial points based on window size
p1 = [5, height - 5]
p2 = [(width - 10) // 2, 5]
p3 = [width - 5, height - 5]
sierpinski(None, p1, p2, p3, pygame.color.Color("black"), 1, screen)
pygame.display.update()
update_screen = False
# limit framerate
clock.tick(30)
pygame.quit()
コンストラクターへの文字列引数を受け入れるpygame.color.Colorを使用する代わりに、簡潔にするために色の処理を削除しました。また//
、の代わりに整数除算を使用しましたround(…)
。
再帰関数の深さや複雑さに応じて、すべてのフレームを再描画できますが、関数の複雑さが増した場合の制限の例を示したいと思いました。最後に、私は最近画面のサイズ変更で遊んでいますが、それは1回の描画と結びついているように見えたので、それも含めました。
編集:再帰depを指定sierpinski
するdegree
引数をサポートするように関数を変更しました
def sierpinski(degree, p1, p2, p3, color, line_width, screen):
# p1 → bottom left, p2 → bottom right, p3 → top
# recursive function so check for exit condition first
if degree is None:
if abs(p1[0] - p2[0]) <= 2 and abs(p2[0] - p3[0]) <= 2 and abs(p1[0] - p3[0]) <= 2:
return
else:
if degree == 0:
return
else:
degree -= 1
…
次に、イベント処理を追加して、マウスホイールを使用して次数をインクリメントおよびデクリメントできるようにしました。これは、タイトルバーに表示されます。
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 4: # wheel up
if degree is None:
degree = 8
else:
degree += 1
if degree > maximum_degree:
degree = maximum_degree
update_screen = True
elif event.button == 5: # wheel down
if degree is None:
degree = 3
else:
degree -= 1
if degree < minimum_degree:
degree = minimum_degree
update_screen = True
…
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加