forked from doge1338/fractal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (141 loc) · 3.23 KB
/
main.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
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"log"
"os"
"runtime"
"runtime/pprof"
"time"
)
// Configuration
const (
// Position and height
px = -0.5557506
py = -0.55560
ph = 0.000000001
//px = -2
//py = -1.2
//ph = 2.5
// Quality
imgWidth = 1024
imgHeight = 1024
maxIter = 1500
samples = 50
linearMixing = true
showProgress = true
profileCpu = false
)
const (
ratio = float64(imgWidth) / float64(imgHeight)
)
func main() {
log.Println("Allocating image...")
img := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight))
log.Println("Rendering...")
start := time.Now()
render(img)
end := time.Now()
log.Println("Done rendering in", end.Sub(start))
log.Println("Encoding image...")
f, err := os.Create("result.png")
if err != nil {
panic(err)
}
err = png.Encode(f, img)
if err != nil {
panic(err)
}
log.Println("Done!")
}
func render(img *image.RGBA) {
if profileCpu {
f, err := os.Create("profile.prof")
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
jobs := make(chan int)
for i := 0; i < runtime.NumCPU(); i++ {
go func () {
for y := range jobs {
for x := 0; x < imgWidth; x++ {
var r, g, b int
for i := 0; i < samples; i++ {
nx := ph * ratio * ((float64(x) + RandFloat64()) / float64(imgWidth)) + px
ny := ph * ((float64(y) + RandFloat64()) / float64(imgHeight)) + py
c := paint(mandelbrotIter(nx, ny, maxIter))
if linearMixing {
r += int(RGBToLinear(c.R))
g += int(RGBToLinear(c.G))
b += int(RGBToLinear(c.B))
} else {
r += int(c.R)
g += int(c.G)
b += int(c.B)
}
}
var cr, cg, cb uint8
if linearMixing {
cr = LinearToRGB(uint16(float64(r) / float64(samples)))
cg = LinearToRGB(uint16(float64(g) / float64(samples)))
cb = LinearToRGB(uint16(float64(b) / float64(samples)))
} else {
cr = uint8(float64(r) / float64(samples))
cg = uint8(float64(g) / float64(samples))
cb = uint8(float64(b) / float64(samples))
}
img.SetRGBA(x, y, color.RGBA{ R: cr, G: cg, B: cb, A: 255 })
}
}
}()
}
for y := 0; y < imgHeight; y++ {
jobs <- y
if showProgress {
fmt.Printf("\r%d/%d (%d%%)", y, imgHeight, int(100*(float64(y) / float64(imgHeight))))
}
}
if showProgress {
fmt.Printf("\r%d/%[1]d (100%%)\n", imgHeight)
}
}
func paint(r float64, n int) color.RGBA {
insideSet := color.RGBA{ R: 255, G: 255, B: 255, A: 255 }
if r > 4 {
return hslToRGB(float64(n) / 800 * r, 1, 0.5)
}
return insideSet
}
func mandelbrotIter(px, py float64, maxIter int) (float64, int) {
var x, y, xx, yy, xy float64
for i := 0; i < maxIter; i++ {
xx, yy, xy = x * x, y * y, x * y
if xx + yy > 4 {
return xx + yy, i
}
x = xx - yy + px
y = 2 * xy + py
}
return xx + yy, maxIter
}
// by u/Boraini
//func mandelbrotIterComplex(px, py float64, maxIter int) (float64, int) {
// var current complex128
// pxpy := complex(px, py)
//
// for i := 0; i < maxIter; i++ {
// magnitude := cmplx.Abs(current)
// if magnitude > 2 {
// return magnitude * magnitude, i
// }
// current = current * current + pxpy
// }
//
// magnitude := cmplx.Abs(current)
// return magnitude * magnitude, maxIter
//}