-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
116 lines (99 loc) · 2.71 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
package main
import (
"flag"
"fmt"
"image/png"
"os"
"runtime"
"strconv"
"time"
"github.com/theteacat/romanesgo/lib"
)
func main() {
fractalName := flag.String("ff", "none", "fractal")
var constants flagConstants
flag.Var(&constants, "c", "constants")
iterations := flag.Int("i", 128, "maximum iterations")
colorName := flag.String("cf", "default", "coloring function")
xCentre := flag.Float64("x", 0, "central x coord")
yCentre := flag.Float64("y", 0, "central y coord")
zoom := flag.Float64("z", 1, "zoom factor")
width := flag.Int("w", 1000, "image width")
height := flag.Int("h", 1000, "image height")
samples := flag.Int("ss", 1, "supersampling factor")
routines := flag.Int("r", runtime.NumCPU(), "goroutines used")
fn := flag.String("fn", "temp.png", "filename")
flag.Parse()
args := flag.Args()
if *fractalName == "none" || len(args) > 0 && args[0] == "help" {
handleHelp(args)
} else {
pointFunc, err := lib.GetPointFunc(*fractalName, *colorName, constants)
fatal(err)
fmt.Print("\n\tFractal (ff):\t\t", *fractalName,
"\n\tConstants (c):\t\t", constants.String(),
"\n\tMax Iterations (i):\t", *iterations,
"\n\tColoring function (cf):\t", *colorName,
"\n\tCentre x Coord (x):\t", *xCentre,
"\n\tCentre y Coord (y):\t", *yCentre,
"\n\tZoom factor (z):\t", *zoom,
"\n\tImage Width (w):\t", *width,
"\n\tImage Height (h):\t", *height,
"\n\tSupersampling (ss):\t", *samples,
"\n\tRoutines (r):\t\t", *routines,
"\n\tFilename (png) (fn):\t", *fn, "\n\n")
gen := lib.NewGenerator(*width, *height, *routines, *iterations, *samples, *xCentre, -*yCentre, *zoom, pointFunc)
newFile, err := os.Create(*fn)
fatal(err)
timeIt(func() {
gen.Generate()
err = png.Encode(newFile, gen.Img)
fatal(err)
})
}
}
func handleHelp(args []string) {
if len(args) < 2 {
fmt.Println(`Do "romanesgo help {Fractal Name}" for further info on a particular fractal function.`)
fmt.Println("\nFractals:")
for fname := range lib.Fractals {
fmt.Println("\t", fname)
}
fmt.Println("\nFlags:")
flag.PrintDefaults()
} else if len(args) == 2 {
frac, err := lib.GetFractal(args[1])
fatal(err)
fmt.Println(frac)
}
}
func fatal(err error) {
if err != nil {
fmt.Println("Error:", err)
os.Exit(0)
}
}
func timeIt(fn func()) {
start := time.Now()
fn()
fmt.Println("Done in", time.Since(start))
}
type flagConstants []float64
func (f *flagConstants) String() string {
str := ""
for key, val := range *f {
if key > 0 {
str = str + ", "
}
str = str + strconv.FormatFloat(val, 'f', -1, 64)
}
return str
}
func (f *flagConstants) Set(value string) error {
val, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
*f = append(*f, val)
return nil
}