some more comments on changelists
[PyX/mjg.git] / examples / bitmap / julia.py
blobfadea17952dc3b3428b5f0b5bc179957e2f0b399
1 # contributed by Stefan Schenk
3 from cStringIO import StringIO
4 from math import sqrt
5 from pyx import *
7 xiterations = yiterations = 250
8 Min_Im = -1.5
9 Max_Im = 1.5
10 Min_Re = -1.5
11 Max_Re = 1.5
12 c = 0.41 + 0.3j
13 p = color.palette.RedBlue
15 def rgbcolortostring(c):
16 return "".join([chr(int(255*c.color[name])) for name in "rgb"])
18 data = StringIO()
20 # compute fractal
21 for y in range(yiterations):
22 for x in range(xiterations):
23 z = complex(1.0*(Max_Re-Min_Re)*x/xiterations + Min_Re,
24 1.0*(Max_Im-Min_Im)*y/yiterations + Min_Im)
26 for k in range(256):
27 z = z*z + c
28 if abs(z) > 2:
29 # append color(RGB) of the current pixel to the end of data
30 data.write(rgbcolortostring(p.getcolor(1.0/sqrt(k+1))))
31 break
32 else:
33 data.write("\0\0\0")
35 # generate image from data
36 julia = bitmap.image(xiterations, yiterations, "RGB", data.getvalue())
37 juliabitmap = bitmap.bitmap(0, 0, julia, height=10)
39 c = canvas.canvas()
40 c.insert(juliabitmap)
41 c.writeEPSfile("julia")
42 c.writePDFfile("julia")