"""
Mandelbrot Set Ver 1.7
OST/Py4 (ostcanvas.py)
http://en.wikipedia.org/wiki/Mandelbrot_set
http://www.brainsturbator.com/articles/crash_course_on_crop_circles_the_sequel/
(gpl) OST, 2011
"""
from ost.farmworld import Farm, Tractor, wordplow
def mandelbrot(c):
"""
generates successive members in a feedback loop
modified by c, a position on the complex plane,
in this module implemented as a 2d array or
list of lists containing ascii characters
"""
z = 0
while True:
z = z*z + c
yield z
class CropCircle (Tractor):
"""
In various fields, these Mandelbrot Sets would
mysteriously appear, as if ploughed by an
Alien Tractor, or perhaps many...
"""
def plow(self, marker="*", depth=20):
"""
focus for Mandelbrot is
x from -2.0 to .5 (2.5 across)
y from 1.5 to -1.5 (3.0 down)
"""
if marker:
self.marker = marker
x = -2.0 + self.pos[1] * 2.5/self.thefarm.w
y = 1.5 - self.pos[0] * 3/self.thefarm.h
thegen = mandelbrot(complex(x,y))
self.marker = marker
for i in range(depth):
v = next(thegen)
# "color coding, worth fine tuning"
if abs( v ) <= 1:
self.marker = "#"
elif abs( v ) <= 10:
self.marker = "@"
elif abs( v ) <= 100000:
self.marker = "."
self.thefarm.field[self.pos[0]][self.pos[1]] = self.marker
def _test():
complexplane = Farm(48, 110)
# print(complexplane)
ccts = [CropCircle(complexplane, pos = [i, 0], facing="E", fuel=200)
for i in range(complexplane.h)]
for cct in ccts: cct.plow("X")
# print("Lined up for duty...")
# print(complexplane)
for j in range(complexplane.w): # plow the whole field
for cct in ccts:
cct.plow("*") # leave a trail
complexplane.ticktock() # advance all tractors
# print("The Mandelbrot Set begins to emerge...")
# print(complexplane)
wordplow(complexplane, [10,10], " PYTHON: JUST USE IT ")
wordplow(complexplane, [46,60], " O'Reilly School of Technology, (gpl) 2011 ")
code = ["","","","",""]
code[0] = " def mandelbrot(c): *"
code[1] = "**** z = 0 **********"
code[2] = "**** while True: ****"
code[3] = "******** z = z*z + c "
code[4] = "******** yield z ****"
for i in range(5):
wordplow(complexplane, [37+i,10], code[i])
print("The final canvas...")
print(complexplane)
if __name__ == "__main__":
_test()