File: mandel.py

package info (click to toggle)
python-bitarray 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,288 kB
  • sloc: python: 11,456; ansic: 7,657; makefile: 73; sh: 6
file content (40 lines) | stat: -rw-r--r-- 844 bytes parent folder | download | duplicates (3)
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
import sys
from bitarray import bitarray
from numba import jit  # type: ignore

width, height = 4000, 3000
maxdepth = 500


@jit(nopython=True)
def mandel(c):
    d = 0
    z = c
    while abs(z) < 4.0 and d <= maxdepth:
        d += 1
        z = z * z + c
    return d


def main():
    data = bitarray(endian='big')

    for j in range(height):
        sys.stdout.write('.')
        sys.stdout.flush()
        y = +1.5 - 3.0 * j / height
        for i in range(width):
            x = -2.75 + 4.0 * i / width
            c = mandel(complex(x, y)) % 2
            data.append(c)
    print("done")

    with open('out.ppm', 'wb') as fo:
        fo.write(b'P4\n')
        fo.write(b'# partable bitmap image of the Mandelbrot set\n')
        fo.write(b'%i %i\n' % (width, height))
        data.tofile(fo)


if __name__ == '__main__':
    main()