File: tt.x1_mandelbrot

package info (click to toggle)
goawk 1.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 10,560 kB
  • sloc: awk: 3,060; yacc: 198; fortran: 189; python: 131; sh: 58; makefile: 12
file content (46 lines) | stat: -rw-r--r-- 1,315 bytes parent folder | download
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
41
42
43
44
45
46
# Generate and display the Mandelbrot set

BEGIN {
    # Constants to determine size and coordinates of grid
    if (!width) width = 600
    if (!height) height = 300
    min_x = -2.1; max_x = 0.6
    min_y = -1.2; max_y = 1.2
    iters = 32

    # "Colors", from '.' (diverges fastest) to '@' (diverges slowly),
    # and then ' ' for doesn't diverge within `iters` iterations.
    colors[0] = "."
    colors[1] = "-"
    colors[2] = "+"
    colors[3] = "*"
    colors[4] = "%%"
    colors[5] = "#"
    colors[6] = "$"
    colors[7] = "@"
    colors[8] = " "

    # Loop from top to bottom, and for each line left to right
    inc_y = (max_y-min_y) / height
    inc_x = (max_x-min_x) / width
    y = min_y
    for (row=0; row<height; row++) {
        x = min_x
        for (col=0; col<width; col++) {
            zr = zi = 0
            for (i=0; i<iters; i++) {
                # Main calculation: z = z^2 + c
                old_zr = zr
                zr = zr*zr - zi*zi + x
                zi = 2*old_zr*zi + y
                # Stop when magnitude is greater than 2
                if (zr*zr + zi*zi > 4) break
            }
            # Scale "color" according to how fast it diverged
            printf colors[int(i*8/iters)]
            x += inc_x
        }
        y += inc_y
        printf "\n"
    }
}