-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.go
86 lines (77 loc) · 1.67 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
package main
import (
"fmt"
"math/cmplx"
"os"
)
func main() {
size := 1024
result := makeMandelbrotSet(
complex(0.50, 1.00),
complex(-1.50, -1.00),
size,
100,
500,
)
dirPath, err := os.Getwd()
if err != nil {
panic(err)
}
distDirPath := dirPath + "/go/dist"
if _, err := os.Stat(distDirPath); os.IsNotExist(err) {
os.Mkdir(distDirPath, 0777)
}
file, _ := os.Create(distDirPath + "/image.pgm")
defer file.Close()
file.Write(([]byte)("P2\n"))
file.Write(([]byte)(fmt.Sprint(size) + " " + fmt.Sprint(size) + "\n"))
file.Write(([]byte)("255\n"))
for row := 0; row < size; row++ {
for col := 0; col < size; col++ {
file.Write(([]byte)(fmt.Sprint(result[row][col]) + "\t"))
}
file.Write(([]byte)("\n"))
}
}
func makeMandelbrotSet(
max complex128,
min complex128,
size int,
limit float64,
maxCycle int,
) [][]int {
scale := complex(
(real(max)-real(min))/float64(size-1),
(imag(max)-imag(min))/float64(size-1),
)
table := make([]complex128, size)
table[0] = min
for index := 1; index < size; index++ {
table[index] = complex(
real(table[index-1])+real(scale),
imag(table[index-1])+imag(scale),
)
}
result := make([][]int, size)
for row := range result {
result[row] = make([]int, size)
}
for row := 0; row < size; row++ {
for col := 0; col < size; col++ {
history := make([]complex128, maxCycle)
history[0] = complex(0, 0)
for cycle := 1; cycle < maxCycle; cycle++ {
add := complex(
real(table[row]),
imag(table[col]),
)
history[cycle] = cmplx.Pow(history[cycle-1], 2) + add
if limit <= cmplx.Abs(history[cycle]) {
result[row][col] = cycle%255 + 1
break
}
}
}
}
return result
}