python簡單的分形圖片
阿新 • • 發佈:2019-01-08
康託集
程式碼:
- # 康託集
- import pygame
- pygame.init()
- screen = pygame.display.set_caption('康託集')
- screen = pygame.display.set_mode([1000, 250])
- screen.fill([255, 255, 255])
- pygame.display.flip()
- len0 = 1000 # 初始線條長度
- leni = len0 # 當前最小線條長度
- line = 0 # 當前行數
- while leni > 1:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- exit()
- n = 2**line # 集合元素份數
- tep = [0, ] # 元素第一個端點位置
- while len(tep) < n:
- nt = (tep[-1] + leni) * 2 # 接下來首個元素位置
- tepp = [] # 接下來元素位置
- for j in tep:
- tepp.append
(nt + j) - tep.extend(tepp)
- for k in tep:
- pygame.draw.line(screen, [0, 0, 0], [
- k, 30 * line + 5], [k + leni, 30 * line + 5], 10)
- pygame.display.flip()
- line += 1
- leni = leni / 3
- while 1:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- exit
()
輸出:
康託塵埃
程式碼:
- # 康託塵埃
- import pygame
- pygame.init()
- screen = pygame.display.set_caption('康託塵埃')
- screen = pygame.display.set_mode([487, 487])
- screen.fill([255, 255, 255])
- pygame.display.flip()
- cantor = [1, ] # 起點集,最小畫素為1
- while (cantor[-1] + 1) * 3 < 1000:
- st = (cantor[-1] + 1) * 2 # 下一迭代起點
- tep = []
- for i in cantor:
- tep.append(st + i) # 重複上一子集
- cantor.extend(tep)
- # print(cantor[-1]) # 輸出最大畫素起點
- for i in cantor:
- for j in cantor:
- screen.set_at([i, j], [0, 0, 0])
- pygame.display.flip()
- while 1:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- exit()
輸出:
方塊分形
程式碼:
- # 方塊分形
- import pygame
- maxlen = 500 # 邊界
- pygame.init()
- screen = pygame.display.set_caption('方塊分形')
- screen = pygame.display.set_mode([maxlen, maxlen])
- screen.fill([255, 255, 255])
- pygame.display.flip()
- def draw(st, leni):
- # st: 左上角點位置[left,top]
- # leni: 當前方塊邊長
- if leni > 3:
- leni /= 3
- draw(st, leni) # 左上
- draw([st[0] + leni * 2, st[1]], leni) # 右上
- draw([st[0] + leni, st[1] + leni], leni) # 中間
- draw([st[0], st[1] + leni * 2], leni) # 左下
- draw([st[0] + leni * 2, st[1] + leni * 2], leni) # 右下
- pygame.display.flip()
- else:
- pygame.draw.rect(screen, [0, 0, 0], [st[0], st[1], leni, leni])
- draw([0, 0], maxlen)
- while 1:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- exit()
輸出:
謝爾賓斯基三角墊
程式碼:
- # 謝爾賓斯基三角墊
- import pygame
- maxlen = 500 # 邊界
- pygame.init()
- screen = pygame.display.set_caption('謝爾賓斯基三角墊')
- screen = pygame.display.set_mode([maxlen, maxlen])
- screen.fill([255, 255, 255])
- pygame.display.flip()
- def mid(a, b):
- # 求出a, b點的中點座標
- return [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
- def draw(one, two, tri):
- # 引數代表三個頂點,上、左、右排序
- if one[0] - two[0] > 2: # 可分
- draw(one, mid(one, two), mid(one, tri)) # 畫上面的三角
- draw(mid(one, two), two, mid(two, tri)) # 畫左邊三角
- draw(mid(one, tri), mid(two, tri), tri) # 畫右邊的三角
- pygame.display.flip()
- else: # 達到最小結構
- pygame.draw.polygon(screen, [0, 0, 0], [one, two, tri])
- draw([maxlen / 2, 0], [0, maxlen], [maxlen, maxlen])
- while 1:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- exit()
輸出:
謝爾賓斯基方毯
程式碼:
- # 謝爾賓斯基方毯
- import pygame
- maxlen = 500 # 邊界
- pygame.init()
- screen = pygame.display.set_caption('謝爾賓斯基方毯')
- screen = pygame.display.set_mode([maxlen, maxlen])
- screen.fill([0, 0, 0])
- pygame.display.flip()
- def p2(p, r, d):
- # p: 參考左上頂點
- # r: 距離參考點向右偏移距離
- # d: 距離參考點向下偏離距離
- return [p[0] + r, p[1] + d]
- def points(p, leni):
- # 返回p,leni對應的四邊形四個頂點列表
- return [p, p2(p, leni, 0), p2(p, leni, leni), p2(p, 0, leni)]
- def draw(p, leni):
- # p:左上頂點
- # leni:邊長
- leni /= 3
- pygame.draw.polygon(screen, [255, 255, 255],
- points(p2(p, leni, leni), leni))
- if leni > 3:
- draw(p, leni)
- draw(p2(p, leni, 0), leni)
- draw(p2(p, 2 * leni, 0), leni)
- draw(p2(p, 0, leni), leni)
- draw(p2(p, 2 * leni, leni), leni)
- draw(p2(p, 0, 2 * leni), leni)
- draw(p2(p, leni, 2 * leni), leni)
- draw(p2(p, 2 * leni, 2 * leni), leni)
- pygame.display.flip()
- draw([0, 0], maxlen)
- while 1:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- exit()
輸出: