Pythonã§ãã³ãã«ããéå(Mandelbrot Set)ãæãã¦ã¿ã。ãã 、æãã ãã§ã¯ã¤ã¾ããªãã®ã§、ã¯ã³ã©ã¤ãã¼(1è¡ããã°ã©ã )ã§æ¸ãã¦ã¿ã。Pythonã§ã¯ã³ã©ã¤ãã¼ã£ã¦ã®ã¯éªæªä»¥å¤ã®ããã¢ãã§ããªãã¨æãã®ã§、ã¾ã¨ããªæ¹ã¯ã¾ãããªãã»ãããããã¨。
ã¯ã³ã©ã¤ãã¼ã«ããã¨äºã£ã¦ãã¹ã¯ã©ããããã§ã¯ãªã、Python FAQã§æåãªã¯ã³ã©ã¤ãã¼ã®ãã³ãã«ããéåãæµç¨ããã¦ããã£ã。ãã 、ãªãªã¸ãã«ã®ãã®ã¯ã¢ã¹ãã¼æåã§è¡¨ç¾ãã¦ããã®ã§Python Imaging Library (PIL)ãå©ç¨ãã¦ã°ã©ãã£ã«ã«ãªãã®ã«ä¿®æ£ãã。ãã、PILãå ¥ã£ã¦ããªããããªã、ããã¯Pythonistaã®åæãã ã¨æã。ã³ãã³ãã©ã¤ã³ã§ä»¥ä¸ã®éãã«å®è¡ããã°OK。
python -c "import Image,math;im=Image.new('L',(480,480));im.fromstring((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y>=4.0)or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(256-int(math.sqrt(F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i))*256/math.sqrt(i))),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy))))(0.383,0.384,0.2337,0.2347,800,480,480));im.save('m.gif');im.show()"
ããã§、æåã«ç¤ºããç»åãç»é¢ã«åºåã、m.gifã¨ããç»åãã¡ã¤ã«ãåæã«ä½æãã。å¾ãã®æ¹ã®å¼æ°ã(-2.1,0.7,-1.4,1.4,256,480,480)ã«å¤æ´ããã°、å·¦ã«ç¤ºãå ¨ä½åãåºåãã。
åä¾ã®ãã、ã³ã³ãã¥ã¼ã¿ã®è½åããã«ã«ä½¿ã£ã¦æ°å¦ã®æ·±é ãªççãæãã¦ããããã«è¦ãããã³ãã«ããéåã¯æ§ãã ã£ããã ããªã。ãã£ã¨ãã®ãããé£ãããã¨ããã¦ãããã ã¨æ³åãããã®ã ãã©、å®éã«è¨ç®ãããã®ã¯ç°¡åãªãã ãã。
追è¨: ä¸è¨ã®ã³ã¼ãã ãã ã¨ã¡ãã£ã¨ä¸è¦ªåã ã¨æã£ãã®ã§、ããå°ããããããã(ã¯ã³ã©ã¤ãã¼ã§ã¯ãªã)ã³ã¼ããæ¸ãã¦ãã。åºåçµæã¯åã。
import Image, math Ru, Ro, Iu, Io, IM, Sx, Sy = 0.383, 0.384, 0.2337, 0.2347, 800, 480, 480 #Ru, Ro, Iu, Io, IM, Sx, Sy = -2.1, 0.7, -1.4, 1.4, 256, 480, 480 def mandelbrot(x, y, IM): c, z, i = complex(x, y), complex(0, 0), 0 while abs(z**2) < 4.0 and i < IM: z = z**2 + c i += 1 return 256 - int(math.sqrt(i) * 256 / math.sqrt(IM)) im = Image.new("L", (Sx, Sy)) im.putdata([mandelbrot(Ru + (x / float(Sx)) * (Ro - Ru), Iu + (y / float(Sy)) * (Io - Iu), IM) for y in range(Sy) for x in range(Sx)]) im.save("m.gif") im.show()
ã¯ã³ã©ã¤ãã¼ã«ããã¨äºã£ã¦ãã¹ã¯ã©ããããã§ã¯ãªã、Python FAQã§æåãªã¯ã³ã©ã¤ãã¼ã®ãã³ãã«ããéåãæµç¨ããã¦ããã£ã。ãã 、ãªãªã¸ãã«ã®ãã®ã¯ã¢ã¹ãã¼æåã§è¡¨ç¾ãã¦ããã®ã§Python Imaging Library (PIL)ãå©ç¨ãã¦ã°ã©ãã£ã«ã«ãªãã®ã«ä¿®æ£ãã。ãã、PILãå ¥ã£ã¦ããªããããªã、ããã¯Pythonistaã®åæãã ã¨æã。ã³ãã³ãã©ã¤ã³ã§ä»¥ä¸ã®éãã«å®è¡ããã°OK。
python -c "import Image,math;im=Image.new('L',(480,480));im.fromstring((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y>=4.0)or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(256-int(math.sqrt(F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i))*256/math.sqrt(i))),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy))))(0.383,0.384,0.2337,0.2347,800,480,480));im.save('m.gif');im.show()"
ããã§、æåã«ç¤ºããç»åãç»é¢ã«åºåã、m.gifã¨ããç»åãã¡ã¤ã«ãåæã«ä½æãã。å¾ãã®æ¹ã®å¼æ°ã(-2.1,0.7,-1.4,1.4,256,480,480)ã«å¤æ´ããã°、å·¦ã«ç¤ºãå ¨ä½åãåºåãã。
åä¾ã®ãã、ã³ã³ãã¥ã¼ã¿ã®è½åããã«ã«ä½¿ã£ã¦æ°å¦ã®æ·±é ãªççãæãã¦ããããã«è¦ãããã³ãã«ããéåã¯æ§ãã ã£ããã ããªã。ãã£ã¨ãã®ãããé£ãããã¨ããã¦ãããã ã¨æ³åãããã®ã ãã©、å®éã«è¨ç®ãããã®ã¯ç°¡åãªãã ãã。
追è¨: ä¸è¨ã®ã³ã¼ãã ãã ã¨ã¡ãã£ã¨ä¸è¦ªåã ã¨æã£ãã®ã§、ããå°ããããããã(ã¯ã³ã©ã¤ãã¼ã§ã¯ãªã)ã³ã¼ããæ¸ãã¦ãã。åºåçµæã¯åã。
import Image, math Ru, Ro, Iu, Io, IM, Sx, Sy = 0.383, 0.384, 0.2337, 0.2347, 800, 480, 480 #Ru, Ro, Iu, Io, IM, Sx, Sy = -2.1, 0.7, -1.4, 1.4, 256, 480, 480 def mandelbrot(x, y, IM): c, z, i = complex(x, y), complex(0, 0), 0 while abs(z**2) < 4.0 and i < IM: z = z**2 + c i += 1 return 256 - int(math.sqrt(i) * 256 / math.sqrt(IM)) im = Image.new("L", (Sx, Sy)) im.putdata([mandelbrot(Ru + (x / float(Sx)) * (Ro - Ru), Iu + (y / float(Sy)) * (Io - Iu), IM) for y in range(Sy) for x in range(Sx)]) im.save("m.gif") im.show()
ã³ã¡ã³ã