mandelbrot.html


001| '''Mandelbrot! '''
002| ####################################################################################################
003| from sys import stdout
004| from os import path
005| from time import time
006| from PIL import Image, ImageDraw
007| from colors import ColorBlack, ColorWhite
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\app'
010| ImgsPath = path.join(BasePath, 'img')
011| 
012| url0 = 'http://guciek.github.io/web_mandelbrot.html#{x};{y};{mag};{n}'
013| url1 = 'http://guciek.github.io/web_mandelbrot.html#-0.5;0;2;10000'
014| url  = 'http://guciek.github.io/web_mandelbrot.html#%s;%s;%s;%s'
015| 
016| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
017| class Mandlebrot (object):
018| 
019|     def __init__ (self, dims=(500,500), view=((-2.5,+0.5),(-1.5,+1.5)), cmax=10000):
020|         self.dims = dims
021|         self.view = view
022|         self.cutoff = cmax
023|         self.window = (view[0][1]-view[0][0], view[1][1]-view[1][0])
024|         self.scale = (float(dims[0])/float(self.window[0]), float(dims[1])/float(self.window[1]))
025|         self.ulc = (5,5)
026|         self.lrc = (dims[0]-5, dims[1]-5)
027|         self.im = Image.new('RGB', dims, (0xff,0xff,0xff))
028|         self.draw = ImageDraw.Draw(self.im)
029|         # Create canvas...
030|         self.t0 = time()
031|         self._generate()
032|         self.t1 = time()
033|         self._grid()
034|         self._axis()
035|         self._border()
036| 
037|     def __str__ (self):
038|         '''String version: format, size, mode.'''
039|         s = '[%.2f,%.2f] (%d,%d) %s'
040|         t = (self.window[0],self.window[1], self.im.size[0],self.im.size[1], self.im.mode)
041|         return s % t
042| 
043|     def __repr__ (self):
044|         '''Canonical representation (JSON-like string).'''
045|         s = '{Mandlebrot:{size:%s, window:%s, cutoff:%s, mode:"%s", addr:%08x}}'
046|         t = (self.dims, self.window, self.cutoff, self.im.mode, id(self))
047|         return s % t
048| 
049|     def px (self, x): return int(self.scale[0] * (float(x)-self.view[0][0]))
050|     def py (self, y): return self.dims[1] - int(self.scale[1] * (float(y)-self.view[1][0]))
051| 
052|     def _fx (self, px): return self.view[0][0] + (float(px) / self.scale[0])
053|     def _fy (self, py): return self.view[1][0] + (float(self.dims[1]-py) / self.scale[1])
054| 
055|     def _generate (self):
056|         n_xpts = abs(self.ulc[0]-self.lrc[0])
057|         n_ypts = abs(self.ulc[1]-self.lrc[1])
058|         xs = [self.ulc[0]+n for n in range(n_xpts)]
059|         ys = [self.ulc[1]+n for n in range(n_ypts)]
060|         #
061|         print('Mandelbrot Image', file=stdout)
062|         print(str(self), file=stdout)
063|         print(repr(self), file=stdout)
064|         print('dims=%s' % str(self.dims), file=stdout)
065|         print('view=%s' % str(self.view), file=stdout)
066|         print('window=%s' % str(self.window), file=stdout)
067|         print('scale=%s' % str(self.scale), file=stdout)
068|         print('cutoff=%s' % str(self.cutoff), file=stdout)
069|         print(file=stdout)
070|         stdout.flush()
071|         for px in xs:
072|             for py in ys:
073|                 c = complex(self._fx(px), self._fy(py))
074|                 n = self._test_point(c)
075|                 if n < 0:
076|                     self.draw.point((px,py), fill=ColorBlack)
077|                 else:
078|                     clr = 255 - ((n*16) % 256)
079|                     self.draw.point((px,py), fill=(clr,clr,clr))
080|             stdout.write('.')
081|             stdout.flush()
082|         stdout.write('\n')
083|         stdout.flush()
084| 
085|     def _test_point (self, c):
086|         counter = 0
087|         z = complex(0) + c
088|         while counter < self.cutoff:
089|             if 2.0 < abs(z):
090|                 return counter
091|             z = (z*z) + c
092|             counter += 1
093|         return -1
094| 
095|     def _border (self):
096|         # Border...
097|         self.draw.rectangle((0,0)+(self.dims[0]-1,self.dims[1]-1), outline=(0,0,0))
098|         self.draw.rectangle((1,1)+(self.dims[0]-2,self.dims[1]-2), outline=(31,31,31))
099|         self.draw.rectangle((2,2)+(self.dims[0]-3,self.dims[1]-3), outline=(63,63,63))
100| 
101|     def _grid (self):
102|         # X grid lines (0.1 spacing)...
103|         y = int(self.view[1][0]*10)/10.0
104|         while y < self.view[1][1]:
105|             px0 = (self.px(self.view[0][0]), self.py(y))
106|             px1 = (self.px(self.view[0][1]), self.py(y))
107|             self.draw.line([px0,px1], fill=(0xbf,0xbf,0xff), width=1)
108|             y += 0.1
109|         # Y grid lines (0.1 spacing)...
110|         x = int(self.view[0][0]*10)/10.0
111|         while x < self.view[0][1]:
112|             px0 = (self.px(x), self.py(self.view[1][0]))
113|             px1 = (self.px(x), self.py(self.view[1][1]))
114|             self.draw.line([px0,px1], fill=(0xbf,0xbf,0xff), width=1)
115|             x += 0.1
116|         # X grid lines (1.0 spacing)...
117|         for  y in range(int(self.view[1][0]), 1+int(self.view[1][1])):
118|             px0 = (self.px(self.view[0][0]), self.py(y))
119|             px1 = (self.px(self.view[0][1]), self.py(y))
120|             self.draw.line([px0,px1], fill=(0x7f,0x7f,0xbf), width=2)
121|         # Y grid lines (1.0 spacing)...
122|         for x in range(int(self.view[0][0]), 1+int(self.view[0][1])):
123|             py0 = (self.px(x), self.py(self.view[1][0]))
124|             py1 = (self.px(x), self.py(self.view[1][1]))
125|             self.draw.line([py0,py1], fill=(0x7f,0x7f,0xbf), width=2)
126| 
127| 
128|     def _axis (self):
129|         # X axis...
130|         px0 = (self.px(self.view[0][0]), self.py(+0.0))
131|         px1 = (self.px(self.view[0][1]), self.py(+0.0))
132|         self.draw.line([px0,px1], fill=(0x7f,0x7f,0x7f), width=2)
133|         # Y axis
134|         py0 = (self.px(+0.0), self.py(self.view[1][0]))
135|         py1 = (self.px(+0.0), self.py(self.view[1][1]))
136|         self.draw.line([py0,py1], fill=(0x7f,0x7f,0x7f), width=2)
137| 
138|     def show (self):
139|         '''Display image in default viewer.'''
140|         self.im.show()
141| 
142|     def save (self, fname='mandlebrot.png', pname=ImgsPath, mode='PNG'):
143|         '''Save image as a file.'''
144|         fn = path.join(pname, fname)
145|         self.im.save(fn, mode)
146| 
147| 
148| ####################################################################################################
149| '''
149| LS = 299,792,458 m
149| LY = 9.45425 x 10^15 m
149| PL = 1.616229 x 10^-35 m  (1/PL = 6.1872 x 10^34 PL/m)
149| 
149| > 86,400 s/dy
149| * 365 dy/yr
149| = 31,536,000 s/yr
149| [3.1536 x 10^7]
149| 
149| > 299,792,458 m/s (LS)
149| * 31,536,000 s/yr
149| = 9,454,254,955,488,000 m/yr (LY)
149| [9.4542 x 10^15]
149| 
149| > 6.1872 x 10^34 PL/m
149| * 9.4542 x 10^15 m/LY
149| = 5.84958 x 10^50 PL/LY
149| 
149| > 5.84958 x 10^50 PL/LY
149| * 30 x 10^9 LY/VU  (Visible Universe)
149| = 1.75487 x 10^61 PL/VU
149| 
149| --
149| > 186,282 mi/s (LS)
149| * 31,536,000 s/yr
149| = 5,874,589,152,000 mi/yr (LY)
149| [5.874589 x 10^12]
149| 
149| > 5280 ft/mi
149| * 5,874,589,152,000 mi/LY
149| = 31,017,830,722,560,000 ft/LY
149| [3.101783 x 10^16]
149| 
149| > 12 in/ft
149| * 31,017,830,722,560,000 ft/LY
149| = 372,213,968,670,720,000 in/LY
149| [3.722139 x 10^17]
149| 
149| > 372,213,968,670,720,000 in/LY
149| * 30,000,000,000 LY/VU [3.0 x 10^10; Visible Universe]
149| = 11,166,419,060,121,600,000,000,000,000 in/VU
149| [1.1166419 x 10^28]
149| 
149| Compare that to a Mandelbrot zoom of 10^173.
149| 
149| '''

150| ####################################################################################################
151| if __name__ == '__main__':
152|     dims = (1000, 1000)
153|     view = ((-2.0, +0.5), (-1.25, +1.25))
154|     cmax = 9000
155|     #
156|     m = Mandlebrot(dims=dims, view=view, cmax=cmax)
157|     m.save()
158|     print(m)
159| ####################################################################################################
160| '''eof'''
161|