pygame写分形几何
曼德博集合(Mandelbrot set,或译为曼德布洛特复数集合)是一种在复平面上组成分形的点的集合,以数学家本华·曼德博的名字命名。曼德博集合与朱利亚集合有些相似的地方,例如使用相同的复二次多项式来进行迭代。 ---维基百科
import pygame,sys,time from pygame.locals import * FPS = 30 WINDOWWIDTH = 640 WINDOWHEIGHT = 480 MAX_WIDTH = 1.0 MIN_WIDTH = -2.0 MAX_HEIGHT = 1.0 MIN_HEIGHT = -1.0 BLUE = (0,152,0) BLACK = (10,2,2) def generateMandelbrot(): mandel = pygame.Surface((WINDOWWIDTH,WINDOWHEIGHT)) mandel.fill(BLACK) pixObj = pygame.PixelArray(mandel) for h in range(0,WINDOWHEIGHT): for w in range(0,WINDOWWIDTH): x = MIN_WIDTH + (MAX_WIDTH - MIN_WIDTH)*w / WINDOWWIDTH y = MIN_HEIGHT+ (MAX_HEIGHT - MIN_HEIGHT)*h / WINDOWHEIGHT c = x + y*1j z = 0 for i in range(1,100): z = z*z + c if abs(z) > 10.0 : break if i >= 99: pixObj[w][h] = BLUE del pixObj return mandel def main(): global DISPLAYSURF pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT)) mandel = generateMandelbrot() DISPLAYSURF.blit(mandel,(0,0)) while True: for event in pygame.event.get(QUIT): pygame.quit() sys.exit() pygame.display.update() FPSCLOCK.tick(FPS) main()
结果如下