-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (166 loc) · 4.94 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
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"image"
"image/gif"
"image/png"
"os"
"regexp"
"strconv"
"strings"
)
type Option struct {
val float64
re *regexp.Regexp
}
// Gets the options passed by user and parses them.
func parseOptions(options []string) (map[string]float64, bool, string) {
// Define patterns for different types of options
reFloat := regexp.MustCompile(`^-?[0-9]*(?:\.[0-9]+)?$`) // -inf < x < inf
reFloatP := regexp.MustCompile(`^[0-9]*(?:\.[0-9]*)?$`) // 0 < x
reIntP := regexp.MustCompile(`^[1-9]+[0-9]*$`) // 1 <= x in Z
reOption := regexp.MustCompile(`^-([a-zA-Z]+)=([^ ]+)$`)
rePalette := regexp.MustCompile(`^[0-2]$`)
reFilename := regexp.MustCompile(`^[\w\.]+$`)
// Set default options
df := map[string]Option{
"radius": {val: 1, re: reFloatP},
"real": {val: 0, re: reFloat},
"imag": {val: 0, re: reFloat},
"creal": {val: 0, re: reFloat},
"cimag": {val: 0, re: reFloat},
"width": {val: 1920, re: reIntP},
"height": {val: 1080, re: reIntP},
"iters": {val: 200, re: reIntP},
"zoom": {val: 0, re: reFloatP},
"scale": {val: 0.5, re: reFloatP},
"palette": {val: 0, re: rePalette},
}
makeGif := false
filename := ""
parsedOptions := make(map[string]float64)
for name, opt := range df {
parsedOptions[name] = opt.val
}
// Parse all program arguments
for i, option := range options {
if option == "gif" {
makeGif = true
continue
}
// parse last argument as filename
if reFilename.Match([]byte(option)) && i == len(options)-1 {
filename = option
continue
}
matches := reOption.FindStringSubmatch(option)
if len(matches) != 3 {
fmt.Printf("Invalid option: %s\n", option)
continue
}
name := matches[1]
val := matches[2]
if _, ok := df[name]; !ok {
fmt.Printf("Invalid option name: %s\n", name)
continue
}
parsedValue := df[name].re.FindStringSubmatch(val)
if len(parsedValue) != 1 {
fmt.Printf("Could not parse number %s of option %s\n", val, name)
continue
}
fval, err := strconv.ParseFloat(parsedValue[0], 64)
if err != nil {
fmt.Printf("Program error, could not parse %s\n", parsedValue[0])
continue
}
parsedOptions[name] = fval
}
if filename == "" {
if makeGif {
filename = "fractal.gif"
} else {
filename = "fractal.png"
}
}
return parsedOptions, makeGif, filename
}
func main() {
args := os.Args[1:]
helpString := `usage: fractal type [options]
Generate images of mandelbrot and filled julia set :
Mandelbrot: z(n+1) = z(n)^2 + c, z(0)=0, iterate over c in C
Julia set : z(n+1) = z(n)^2 + c, c = constant, iterate over z(0) in C
type: m (mandelbrot) or j (julia set)
options:
-width=<width> set width of image to <width>, defult is 1920
-height=<height> set height of image to <height>, default is 1080
-palette=<pal> set the color palette. <pal> is an int in [0,2]
-real=<real> set real part of center to <real>
-imag=<imag> set imaginary part of center to <imag>
-radius=<radius> set radius to include in image to <radius>
-creal=<creal> set real part of c in julia set to <creal>
-cimag=<cimag> set imaginary part of c in julia set to <cimag>
-iters=<iters> set the max ammonut of iterations per pixel to <iters>
-zoom=<exp> set the max zoom of a gif to 10^<exp>
-scale=<scale> set the factor for which each image in the gif is
scaled to exp(-<scale>). Default is 1/2 -> 1.65 scaling`
// parse command line arguments
if len(args) == 0 {
fmt.Println(helpString)
os.Exit(1)
}
fractalType := strings.ToLower(args[0])
if fractalType != "m" && fractalType != "j" {
fmt.Println(helpString)
os.Exit(1)
}
options, makeGif, filename := parseOptions(args[1:])
width := int(options["width"])
height := int(options["height"])
center := complex(options["real"], options["imag"])
c := complex(options["creal"], options["cimag"])
radius := options["radius"]
maxIters := uint(options["iters"])
zoom := options["zoom"]
scale := options["scale"]
paletteNum := int(options["palette"])
// Define image traits
var img *image.RGBA
br, tl := rectWithCircleInscribed(width, height, center, radius)
if makeGif {
var images []*image.Paletted
var delays []int
if fractalType == "m" {
images = mandelbrotGIF(width, height, tl, br, maxIters, img, zoom, scale, paletteNum)
} else {
images = juliaGIF(width, height, tl, br, maxIters, c, img, zoom, scale, paletteNum)
}
for i := 0; i < len(images); i++ {
delays = append(delays, 50)
}
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
err := gif.EncodeAll(f, &gif.GIF{
Image: images,
Delay: delays,
})
if err != nil {
fmt.Println(err)
}
} else {
img := image.NewRGBA(image.Rect(0, 0, width, height))
if fractalType == "m" {
mandelbrotImage(width, height, tl, br, maxIters, img, paletteNum)
} else {
juliaImage(width, height, tl, br, maxIters, c, img, paletteNum)
}
f, err := os.Create(filename)
if err == nil {
png.Encode(f, img)
os.Exit(0)
}
fmt.Println(err)
os.Exit(1)
}
}