forked from M-Labs/artiq
1
0
Fork 0

examples: add Mandelbrot demo

This commit is contained in:
Sebastien Bourdeauducq 2014-09-16 23:12:03 +08:00
parent dc38356ad9
commit 5dbc1aa5a3
1 changed files with 42 additions and 0 deletions

42
examples/mandelbrot.py Normal file
View File

@ -0,0 +1,42 @@
import sys
from artiq.language.core import *
from artiq.devices import corecom_serial, core
class Mandelbrot(AutoContext):
def col(self, i):
sys.stdout.write(" .,-:;i+hHM$*#@ "[i])
def row(self):
print("")
# based on: http://warp.povusers.org/MandScripts/python.html
@kernel
def run(self):
minX = -2.0
maxX = 1.0
width = 78
height = 36
aspectRatio = 2
yScale = (maxX-minX)*(height/width)*aspectRatio
for y in range(height):
for x in range(width):
c_r = minX+x*(maxX-minX)/width
c_i = y*yScale/height-yScale/2
z_r = c_r
z_i = c_i
for i in range(16):
if z_r*z_r + z_i*z_i > 4:
break
new_z_r = (z_r*z_r)-(z_i*z_i) + c_r
z_i = 2*z_r*z_i + c_i
z_r = new_z_r
self.col(i)
self.row()
if __name__ == "__main__":
with corecom_serial.CoreCom() as com:
exp = Mandelbrot(core=core.Core(com))
exp.run()