forked from ray-g/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.go
153 lines (137 loc) · 3.56 KB
/
mandelbrot.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Mandelbrot emits a PNG image of the Mandelbrot fractal.
package main
import (
"image"
"image/color"
"image/png"
"io"
"log"
"math/big"
"math/cmplx"
"net/http"
"os"
"strconv"
)
var stdout io.Writer = os.Stdout
const (
cmpl64 = iota
cmpl128
bigFloat
//bigRat // remove due to extremely slow rendering speed...
typeSize
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "web" {
//!+http
handler := func(w http.ResponseWriter, r *http.Request) {
typ, err := strconv.ParseInt(r.URL.Query().Get("type"), 10, 32)
if err != nil {
typ = 0
}
draw(w, int(typ)%typeSize)
}
http.HandleFunc("/", handler)
//!-http
log.Fatal(http.ListenAndServe("localhost:8000", nil))
return
}
for i := 0; i < typeSize; i++ {
draw(stdout, i)
}
}
func draw(out io.Writer, typ int) {
const (
xmin, ymin, xmax, ymax = -2, -2, +2, +2
width, height = 1024, 1024
)
img := image.NewRGBA(image.Rect(0, 0, width, height))
for py := 0; py < height; py++ {
y := float64(py)/height*(ymax-ymin) + ymin
for px := 0; px < width; px++ {
x := float64(px)/width*(xmax-xmin) + xmin
z := complex(x, y)
// Image point (px, py) represents complex value z.
switch typ {
case cmpl64:
img.Set(px, py, mandelbrot64(z))
case cmpl128:
img.Set(px, py, mandelbrot128(z))
case bigFloat:
img.Set(px, py, mandelbrotFloat(z))
//case bigRat:
// img.Set(px, py, mandelbrotRat(z))
}
}
}
png.Encode(out, img) // NOTE: ignoring errors
}
func mandelbrot128(z complex128) color.Color {
const iterations = 200
const contrast = 15
var v complex128
for n := uint8(0); n < iterations; n++ {
v = v*v + z
if cmplx.Abs(v) > 2 {
return color.Gray{255 - contrast*n}
}
}
return color.Black
}
func mandelbrot64(z complex128) color.Color {
const iterations = 200
const contrast = 15
var v complex64
for n := uint8(0); n < iterations; n++ {
v = v*v + complex64(z)
if cmplx.Abs(complex128(v)) > 2 {
return color.Gray{255 - contrast*n}
}
}
return color.Black
}
func mandelbrotFloat(z complex128) color.Color {
const iterations = 200
const contrast = 15
zR := (&big.Float{}).SetFloat64(real(z))
zI := (&big.Float{}).SetFloat64(imag(z))
vR := &big.Float{}
vI := &big.Float{}
for n := uint8(0); n < iterations; n++ {
// (r+i)^2=r^2 + 2ri + i^2
vR2, vI2 := &big.Float{}, &big.Float{}
vR2.Mul(vR, vR).Sub(vR2, (&big.Float{}).Mul(vI, vI)).Add(vR2, zR)
vI2.Mul(vR, vI).Mul(vI2, big.NewFloat(2)).Add(vI2, zI)
vR, vI = vR2, vI2
squareSum := &big.Float{}
squareSum.Mul(vR, vR).Add(squareSum, (&big.Float{}).Mul(vI, vI))
if squareSum.Cmp(big.NewFloat(4)) > 0 {
return color.Gray{255 - contrast*n}
}
}
return color.Black
}
func mandelbrotRat(z complex128) color.Color {
// Problems with high resolution images and slow 'escape' speed.
// Because multiplication of arbitrary-precision arithmetic
// achieve O(N log(N) log(log(N))) complexity
// https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Implementation_issues
const iterations = 200
const contrast = 15
zR := (&big.Rat{}).SetFloat64(real(z))
zI := (&big.Rat{}).SetFloat64(imag(z))
vR := &big.Rat{}
vI := &big.Rat{}
for n := uint8(0); n < iterations; n++ {
// (r+i)^2=r^2 + 2ri + i^2
vR2, vI2 := &big.Rat{}, &big.Rat{}
vR2.Mul(vR, vR).Sub(vR2, (&big.Rat{}).Mul(vI, vI)).Add(vR2, zR)
vI2.Mul(vR, vI).Mul(vI2, big.NewRat(2, 1)).Add(vI2, zI)
vR, vI = vR2, vI2
squareSum := &big.Rat{}
squareSum.Mul(vR, vR).Add(squareSum, (&big.Rat{}).Mul(vI, vI))
if squareSum.Cmp(big.NewRat(4, 1)) > 0 {
return color.Gray{255 - contrast*n}
}
}
return color.Black
}