pygameを使ってKochKochスノーフレークを実装したいと思います。
私はhttp://en.wikipedia.org/wiki/File:KochFlake.svgからの次の一連の画像を使用しています
私のアルゴリズムはこれです
- 三角形を描く
- サイズの3分の1の三角形の点を計算し、中心線を削除します
- 外側のポイントを見つけます(上の画像の2番目の図に示されているように)
- すべてのエンドポイントのリストを作成します
- ポリゴンを使用してすべてのポイントを結合
私は2番目のステップまで完了しました。しかし、私は3番目のステップで苦労しています-外側のポイントを見つける方法がわからないので-ヒントはありますか?
これが2番目のステップまでの私のコードです
import pygame
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption('Koch snowflake')
white = (255, 255, 255)
black = (0, 0 ,0)
def midpoints(pt1 , pt2):
(x1, y1) = pt1
(x2, y2) = pt2
return ((x1+x2)/2, (y1 + y2)/2)
def midline(pt1, pt2):
(x1, y1) = pt1
(x2, y2) = pt2
return [(x1 + float(x2-x1)/3.0,y1 + float(y2-y1)/3.0), (x1 + float(x2-x1)*2.0/3,y1+ float(y2-y1)*2.0/3)]
def drawline(pt1, pt2):
pygame.draw.line(screen, white, pt1, pt2)
def clearline(pt1,pt2):
pygame.draw.line(screen, black, pt1, pt2, 4)
a = [(150,150), (450,150), (300,410), (150,150)]
pygame.draw.polygon(screen, white ,(a[0], a[1], a[2]), 1)
i = 0
order = 0
length = len(a)
while order < length - 1:
pts = midline(a[i], a[i+1])
clearline(pts[0], pts[1])
a = a[:i+1] + pts + a[i+1:]
print a
if order < 3:
i = i+3
order = order + 1
#pygame.draw.polygon(screen, white ,Tup, 1)
pygame.display.update()