/
burningship.go
75 lines (58 loc) · 1.79 KB
/
burningship.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
package main
import (
"fmt"
"math"
"strconv"
)
// A BurningShip represents the strongly typed planar space for the Burning Ship fractal
type burningShipPlane struct {
Plane
}
func newBurningShip() burningShipPlane {
return burningShipPlane{Plane{-1.5, 2.0, -2.0, 1.5}}
}
func (m *burningShipPlane) process(c config) {
if c.midX == -99.0 {
c.midX = (m.rMax + m.rMin) / 2.0
}
if c.midY == -99.0 {
c.midY = (m.iMax + m.iMin) / 2.0
}
if c.mode == imageMode {
m.image(c)
} else if c.mode == coordinatesMode {
var r, i = m.calculateCoordinatesAtPoint(c)
fmt.Printf("%18.17e, %18.17e\n", r, i)
}
}
func (m *burningShipPlane) image(c config) {
initialiseGradient(c.gradient)
mbi := initialiseimage(c)
plottedChannel := make(chan PlottedPoint)
go func(points <-chan PlottedPoint) {
for p := range points {
if p.Escaped {
mbi.Set(p.X, p.Y, getPixelColour(p, c.maxIterations, c.colourMode))
}
}
}(plottedChannel)
var checkIfPointEscapes escapeCalculator = func(real float64, imag float64, config config) (bool, int, float64, float64) {
var zReal = real
var zImag = imag
var iteration int
for iteration = 0; iteration < config.maxIterations && (zReal*zReal+zImag*zImag) < config.bailout; iteration++ {
xtemp := zReal*zReal - zImag*zImag - real
newImag := math.Abs(2*zReal*zImag + imag)
newReal := math.Abs(xtemp)
zReal = newReal
zImag = newImag
}
return iteration < config.maxIterations, iteration, zReal, zImag
}
m.iterateOverPoints(c, plottedChannel, checkIfPointEscapes)
if c.filename == "" {
c.filename = "ship_" + strconv.FormatFloat(c.midX, 'E', -1, 64) + "_" + strconv.FormatFloat(c.midY, 'E', -1, 64) + "_" + strconv.FormatFloat(c.zoom, 'E', -1, 64) + ".jpg"
}
saveimage(mbi, c.output, c.filename)
fmt.Printf("%s/%s\n", c.output, c.filename)
}