-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractals2.py
More file actions
95 lines (77 loc) · 2.75 KB
/
Copy pathfractals2.py
File metadata and controls
95 lines (77 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# A simple fractals program by Kirby Urner
# Latest version: Sept 25, 2004 -- added image palette for color
from __future__ import division # needed pre 2.4 (?)
import Image # PIL installed under site-packages
import ImagePalette
# put your preferred path for saving files here:
mypath = 'd:/python23/Lib/site-packages/mathstuff2/'
def mkpalette():
global palette
palette = [0,0,0]
for i in range(0,255):
palette.extend((i*5%200 + 55, i*7%200 + 55, i*11%200 + 55))
return palette
class Julia:
def __init__(self, filename, size, n=64, box=((-2,1.25),(0.5,-1.25)) ):
self.size = size
self.filename = filename
self.n = n
self.uleft = box[0]
self.lright = box[1]
self.xwidth = self.lright[0] - self.uleft[0]
self.ywidth = self.uleft[1] - self.lright[1]
def __call__(self,z):
self.z = z
self.compute()
def newimage(self):
self.im = Image.new('P',self.size)
self.im.putpalette(mkpalette())
def compute(self):
print "Computing %s..." % self.__class__.__name__
self.newimage()
for x in range(self.size[0]):
for y in range(self.size[1]):
xp,yp = self.getcoords(x,y)
dotcolor = self.fractal(xp,yp)
self.im.putpixel((x,y),dotcolor)
self.saveimage()
def fractal(self,x,y):
n = self.n
z = self.z
o = complex(x,y)
dotcolor = 0 # default, convergent
for trials in range(n):
if abs(o) <= 2.0:
o = o**2 + z
else:
dotcolor = trials
break # diverged
return dotcolor
def getcoords(self,x,y):
percentx = x/self.size[0]
percenty = y/self.size[1]
xp = self.uleft[0] + percentx * (self.xwidth)
yp = self.uleft[1] - percenty * (self.ywidth)
return (xp,yp)
def saveimage(self):
self.im.save(self.filename)
class Mandelbrot(Julia):
def fractal(self,x,y):
n = self.n
z = complex(x,y)
o = complex(0,0)
dotcolor = 0 # default, convergent
for trials in range(n):
if abs(o) <= 2.0:
o = o**2 + z
else:
dotcolor = trials
break # diverged
return dotcolor
def test():
f = Julia(mypath + 'julia.png',(500,500), n=128, box=((-1.2,1.2),(1.2,-1.2)) )
f(complex(-0.74543,0.11301))
f = Mandelbrot(mypath + 'mandelbrot.png',(500,500))
f.compute()
if __name__ == '__main__':
test()