Repository navigation
Expand file tree
/
Copy pathmandelplot.go
More file actions
292 lines (229 loc) · 6.17 KB
/
Copy pathmandelplot.go
File metadata and controls
292 lines (229 loc) · 6.17 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"fmt"
"github.com/nfnt/resize"
"image"
. "image/color"
"image/png"
"math"
"math/cmplx"
"os"
"runtime"
"strconv"
"sync"
)
const SIZE int = 1600
type Range struct {
low, high float64
}
type Coordinate struct {
r float64
i float64
size float64
rangeR, rangeI Range
}
func NewCoordinate(r, i, size float64) *Coordinate {
coord := new(Coordinate)
coord.r = r
coord.i = i
coord.size = size
halfSize := size / 2
coord.rangeR = Range{r - halfSize, r + halfSize}
coord.rangeI = Range{i - halfSize, i + halfSize}
return coord
}
func convert(x, y int, location *Coordinate) (float64, float64) {
r := float64(x)*location.size/float64(SIZE) + location.rangeR.low
i := float64(y)*location.size/float64(SIZE) + location.rangeI.low
return r, i
}
func Hsv(H, S, V float64) Color {
Hp := H / 60.0
C := V * S
X := C * (1.0 - math.Abs(math.Mod(Hp, 2.0)-1.0))
m := V - C
r, g, b := 0.0, 0.0, 0.0
switch {
case 0.0 <= Hp && Hp < 1.0:
r = C
g = X
case 1.0 <= Hp && Hp < 2.0:
r = X
g = C
case 2.0 <= Hp && Hp < 3.0:
g = C
b = X
case 3.0 <= Hp && Hp < 4.0:
g = X
b = C
case 4.0 <= Hp && Hp < 5.0:
r = X
b = C
case 5.0 <= Hp && Hp < 6.0:
r = C
b = X
}
f := func(value float64) uint16 { return uint16(value * 65535.0) }
return RGBA64{f(m + r), f(m + g), f(m + b), 0xFFFF}
}
func draw(img *image.RGBA, x, y int, color Color) {
img.Set(x, SIZE-1-y, color)
}
func distanceEstimator(c complex128, maxIterations int, escapeRadius float64) (float64, float64, float64) {
iterations := 1
z := c
// Used for period detection
iterationsFast := 1
z_fast := z
dz := complex(1, 0)
// Distance Estimator Algorithm
for cmplx.Abs(z) <= escapeRadius && iterations < maxIterations {
z2 := z*z + c
dz = 2*z*dz + 1
z = z2
iterations = iterations + 1
// Period detection
z_fast = z_fast*z_fast + c
z_fast = z_fast*z_fast + c
iterationsFast = iterationsFast + 1
if z == z_fast {
//period := iterationsFast - iterations
iterations = maxIterations
}
}
magZ := cmplx.Abs(z)
distanceEstimate := magZ * math.Log(magZ) / cmplx.Abs(dz)
dwell := float64(iterations)
if iterations < maxIterations {
dwell += math.Log2(math.Log2(magZ)) - math.Log2(math.Log2(escapeRadius))
}
return distanceEstimate, dwell, cmplx.Phase(z)
}
func plot(img *image.RGBA, location *Coordinate, maxIterations, start, finish int, wg *sync.WaitGroup) {
for x := start; x < finish; x++ {
for y := 0; y < SIZE; y++ {
r, i := convert(x, y, location)
c := complex(r, i)
distanceEstimate, D, phase := distanceEstimator(c, maxIterations, 10.0)
dwell := math.Floor(D)
finalrad := D - dwell
// The Distance Estimate is scaled based on the size of the image and
// the magnification factor
dscale := math.Log2(distanceEstimate / location.size * (float64(SIZE) / 2))
if int(D) >= maxIterations {
draw(img, x, y, White)
} else {
// Coloring is achieved by combining DEM, Dwell and Binary Decomposition
//
// The following algorithm is an implementation of the pseudocode found
// on Robert Munafo's website: http://mrob.com/pub/muency/color.html
var value float64
// Convert the scaled distance estimate to a brightness (called
// "value" in the HSV color space) from 0.0 to 1.0 in 8 intervals.
if dscale > 0 {
value = 1.0
} else if dscale > -8 {
value = (8 + dscale) / 8
} else {
value = 0
}
/*The following re-maps the range 0.0...1.0 onto an "angle"
and "radius" on the color wheel. This color wheel has white in the
center, pastel hues close to the center, and vivid colors around the
edge of the wheel. In order to gain maximum use of all the available
colors, we compute an angle and radius in a way that places the
resulting points equally far apart from one another. The use
of square root means that the radius increases more slowly as we
move away from the center. Note also that the angle will be multiplied
by a constant below, so we'll end up going around the wheel multiple times*/
P := math.Log(D) / math.Log(100000)
var angle, radius float64
if P < 0.5 {
P = 1.0 - 1.5*P
angle = 1 - P
radius = math.Sqrt(P)
} else {
P = 1.5*P - 0.5
angle = P
radius = math.Sqrt(P)
}
// Brighness and saturation are lowered a bit
// in every other "stripe".
if int(dwell)%2 != 0 {
value = 0.85 * value
radius = 0.667 * radius
}
// This breaks the stripe into "squares" to make
// external angles evident.
if phase < 0 {
angle = angle + 0.02
}
angle = angle + 0.0001*finalrad
hue := angle * 5.0
hue = hue - math.Floor(hue)
// Hue is now in the range [0..1] and needs to be
// converted to [0..360[
hue *= 359
saturation := radius - math.Floor(radius)
draw(img, x, y, Hsv(hue, saturation, value))
}
}
}
wg.Done()
}
func main() {
min := image.Point{0, 0}
max := image.Point{SIZE, SIZE}
rect := image.Rectangle{min, max}
img := image.NewRGBA(rect)
usage := fmt.Sprintf("Usage: %v <real> <img> <size>", os.Args[0])
getFloat := func(arg string) float64 {
f, err := strconv.ParseFloat(arg, 64)
if err != nil {
fmt.Println(err)
fmt.Println(usage)
os.Exit(1)
}
return f
}
var r, i, size float64
if len(os.Args) < 4 {
// Discard arguments and plot the set centered and zoomed out
r = -0.75
i = 0
size = 3.5
} else {
r = getFloat(os.Args[1])
i = getFloat(os.Args[2])
size = getFloat(os.Args[3])
}
cpus := runtime.NumCPU()
chunkSize := SIZE / cpus
fmt.Printf("Spawning %v goroutine(s)\n", cpus)
var wg sync.WaitGroup
wg.Add(cpus)
for j := 0; j < cpus; j++ {
var start, stop int
start = j * chunkSize
if j < cpus-1 {
stop = (j + 1) * chunkSize
} else {
stop = SIZE
}
go plot(img, NewCoordinate(r, i, size), 10000, start, stop, &wg)
}
wg.Wait()
// The final image is half the size of the generated one.
// This way we get a crisp anti-aliased image
downscaled := resize.Resize(uint(SIZE/2), 0, img, resize.Lanczos3)
file, err := os.Create("out.png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = png.Encode(file, downscaled)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}