# Veusz script (version 0.7 or later)
# Jeremy Sanders (2005)

# computes the Mandelbrot set in real time

size = 300
maxiters = 20
image = zeros( (size, size) )

print("This takes some time, please wait")

minx = 100000
maxx = -100000
miny = 100000
maxy = -100000

for i in range(size):
    for j in range(int(size/2)):
        c1 = -2+4.*i/size
        c2 = 2-4.*j/size
        x = c1
        y = c2

        minx=min(x, minx)
        maxx=max(x, maxx)
        miny=min(y, miny)
        maxy=max(y, maxy)

        n = 0
        while n < maxiters and x**2+y**2 < 4.:
            x1 = x**2-y**2+c1
            y1 = 2*x*y+c2
            x = x1
            y = y1

            n += 1

        image[j, i] = n
        image[size-j-1, i] = n

# set output data into veusz
SetData2D('image', image, xrange=(minx, maxx),
          yrange=(miny, maxy))

# construct the graph
To(Add('page'))
To(Add('graph'))

# Add a label
Add('label', label='The Mandelbrot Set', yPos=0.95,
    alignHorz='centre', alignVert='top',
    Text__size='30pt')

# add colorbar in front of image
Add('colorbar', name='colorbar1', image='image1',
    direction='vertical', vertPosn='top')

# add image
Add('image', name='image1', data='image', min=1,
    colorScaling='log', colorMap='heat', colorInvert=True)

# adjust axes
Set('x/min', -2.2)
Set('x/max', 1.2)
Set('y/min', 0.3)
Set('y/max', 1.9)

To('/')
