#!/usr/bin/env python # v1.1 29 December 2007 # Python program for making a julia set in a PPM file. # This version will be SLOW. It does not use numerical arrays. # Here is what the program does. For every (x,y) point within # a rectangular grid, the point is treated as complex number z. # The following very simple equation is interated for up to # itermax times: # z=z^2 +c # Depending on whether that iteration converges or diverges, # a color is assigned to the (x,y) point. By diverge ("breakout"), it is # meant that |z|^2 becomes greater than the number breakout # with itermax iterations. import sys,getopt from math import sqrt from os import system #import psyco #not useful, did not increase speed #psyco.full() ############### #default arguments: filename="frompython.ppm" h=400 w=600 magnify=10 itermax=100 breakout=64. cr=-.7492 ci=.1 x0=.09950 y0=-.00062 displayppm=False #change default from commandline (options,arguments)=getopt.getopt(sys.argv[1:],'h:w:b:m:i:d',["cr=","ci=","x0","y0="]) for op,ar in options: if op=="--cr": cr=float(ar) if op=="--ci": ci=float(ar) if op=="--x0": ci=float(ar) if op=="--y0": ci=float(ar) if op=="-m": magnify=float(ar) if op=="-i": itermax=int(ar) if op=="-h": h=int(ar) if op=="-w": w=int(ar) if op=="-b": breakout=float(ar) if op=="-d": displayppm=True rb=sqrt(breakout) # header for ppm file pfile=open(filename,'wb') pfile.write("P6\n") pfile.write("#magnify=%f itermax=%d\n" % (magnify, itermax)) pfile.write("%d %d\n" % (w, h)) pfile.write("255\n") #loop over all pixels for j in range(1,h+1): for i in range(1,w+1): x=4*(1.*i/w - .5)/magnify+x0 y=(4.*h/w)*(1.*(h+1-j)/h - .5)/magnify+y0 zm=0 iter=0 while iter < itermax: xl=x yl=y xx=x*x - y*y + cr; y=2*x*y + ci x=xx zsq=x*x+y*y if zsq>zm: zm=zsq if zsq>breakout: break iter=iter+1 if iter>=itermax : red=0 green=int(255.*zm/breakout) blue=int(255.*zm/breakout) else: red=int(255.*(rb+xl)/(2*rb)) green=0 blue=int(.5*255.*(rb+yl)/(2*rb)) pfile.write("%c%c%c" % (red,green,blue)) print "processed row",j,"of",h pfile.close() print "wrote ",w,"by",h, "ppm file: ",filename if displayppm: system("display "+filename)