-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (123 loc) · 3.29 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
package main
import (
"flag"
"github.com/lucasb-eyer/go-colorful"
"github.com/pkg/profile"
"image"
"image/color"
"image/png"
"os"
"sync"
)
type Config struct {
MaxIterations, Width, Height int
XMin, XMax, YMin, YMax float64
Out, Mode string
}
var config Config
func main() {
defer profile.Start(profile.TraceProfile, profile.ProfilePath(".")).Stop()
var (
maxIterations = flag.Int("maxIterations", 100, "Maximum number of number of Mandelbrot iterations")
width = flag.Int("width", 1000, "Width of the output image")
height = flag.Int("height", 1000, "Height of the output image")
xMin = flag.Float64("xMin", -2, "Minimum value of X painted on image")
xMax = flag.Float64("xMax", 1, "Maximum value of X painted on image")
yMin = flag.Float64("yMin", -2, "Minimum value of Y painted on image")
yMax = flag.Float64("yMax", 2, "Maximum value of Y painted on image")
out = flag.String("out", "output.png", "Filename of the output image")
mode = flag.String("mode", "seq", "Image generation method: `seq|px|row`")
)
flag.Parse()
config = Config{
MaxIterations: *maxIterations,
Width: *width,
Height: *height,
XMin: *xMin,
XMax: *xMax,
YMin: *yMin,
YMax: *yMax,
Out: *out,
Mode: *mode,
}
img := image.NewRGBA(
image.Rect(0, 0, config.Width, config.Height),
)
switch *mode {
case "seq":
drawSequentially(img)
case "px":
drawPixelByPixel(img)
case "row":
drawRowByRow(img)
default:
panic("Unknown mode!")
}
err := saveImage(img, *out)
if err != nil {
panic(err)
}
}
func drawSequentially(img *image.RGBA) {
for row := 0; row < config.Width; row++ {
for col := 0; col < config.Height; col++ {
paintPixel(row, col, img)
}
}
}
func drawPixelByPixel(img *image.RGBA) {
var wg sync.WaitGroup
wg.Add(config.Width * config.Height)
for row := 0; row < config.Width; row++ {
for col := 0; col < config.Height; col++ {
go func(row, col int) {
paintPixel(row, col, img)
wg.Done()
}(row, col)
}
}
wg.Wait()
}
func drawRowByRow(img *image.RGBA) {
var wg sync.WaitGroup
wg.Add(config.Width)
for row := 0; row < config.Width; row++ {
go func(row int) {
for col := 0; col < config.Height; col++ {
paintPixel(row, col, img)
}
wg.Done()
}(row)
}
wg.Wait()
}
func paintPixel(row int, col int, img *image.RGBA) {
c := getComplexNumberFromCoOrdinates(row, col)
iterations := mandelBrot(c, config.MaxIterations)
img.Set(row, col, computeColor(iterations, config.MaxIterations))
}
func getComplexNumberFromCoOrdinates(row, col int) complex128 {
return complex(
config.XMin+(float64(row)*(config.XMax-config.XMin))/float64(config.Width),
config.YMin+(float64(col)*(config.YMax-config.YMin))/float64(config.Height),
)
}
func computeColor(iterations int, maxIterations int) (c color.Color) {
hue := (float64(iterations) / float64(maxIterations)) * 360
saturation := 1.0
value := 0.5
if iterations == maxIterations {
value = 0
}
c = colorful.Hsl(hue, saturation, value)
return
}
func saveImage(img *image.RGBA, outputPath string) (err error) {
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
err = png.Encode(file, img)
return
}