-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (65 loc) · 2.05 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
package main
import (
"flag"
"image"
"image/png"
"os"
"sync"
)
var (
width *uint64 = flag.Uint64("width", 1920, "Image width")
height *uint64 = flag.Uint64("height", 1080, "Image height")
xMin *float64 = flag.Float64("xMin", -2.5, "Minimum value of X painted on the image")
xMax *float64 = flag.Float64("xMax", 1, "Maximum value of X painted on the image")
yMin *float64 = flag.Float64("yMin", -1, "Minimum value of X painted on the image")
yMax *float64 = flag.Float64("yMax", 1, "Maximum value of X painted on the image")
maxIterations *uint64 = flag.Uint64("maxIterations", 100, "Maximum iteration count")
col *int = flag.Int("color", 1, "Color mode (use --help to see the available modes)")
out *string = flag.String("out", "output.png", "The output image, maut be a png file")
help *bool = flag.Bool("help", false, "Print a help message and exit")
)
func main() {
flag.Parse()
if *help {
printHelp()
return
}
img := mandelbrotSet(*xMin, *xMax, *yMin, *yMax, *width, *height, *maxIterations, *col)
file, err := os.Create(*out)
if err != nil {
panic(err)
}
defer file.Close()
png.Encode(file, img)
}
type img struct {
img *image.RGBA
sync.Mutex
}
func mandelbrotSet(xMin, xMax, yMin, yMax float64, width, height, maxIterations uint64, col int) image.Image {
result := img{
img: image.NewRGBA(
image.Rect(0, 0, int(width), int(height)),
),
}
var wg sync.WaitGroup
// For each pixel on the screen
var Px, Py uint64
for Px = 0; Px < width; Px++ {
for Py = 0; Py < height; Py++ {
wg.Add(1)
go func(xMin, xMax, yMin, yMax float64, width, height, maxIterations, x, y uint64, col int) {
p := newPixel(x, y)
p.computeComplexCoords(xMin, xMax, yMin, yMax, width, height)
p.computeIterationCount(maxIterations)
p.computeColor(maxIterations, col)
result.Lock()
result.img.Set(int(p.Px), int(p.Py), p.color)
result.Unlock()
wg.Done()
}(xMin, xMax, yMin, yMax, width, height, maxIterations, Px, Py, col)
}
}
wg.Wait()
return result.img
}