-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
291 lines (251 loc) · 6.72 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"flag"
"fmt"
"image/color"
"image/color/palette"
"math"
"math/cmplx"
"os"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/lucasb-eyer/go-colorful"
)
var (
heightFlag = flag.Int("h", 400, "height (in pixels) for rendering")
widthFlag = flag.Int("w", 600, "width (in pixels) for rendering")
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
)
// default parameters for Mandelbrot.
var zoom = float64(*widthFlag) / (rMax - rMin)
const (
zoomFactor = 1.1
panFactor = 10.0
iterationStep = 5
rMin = -2.0
rMax = 1.0
iMin = -1.0
iMax = 1.8
defaultIterations = 256
)
type panDirection int
const (
left panDirection = iota
right
up
down
)
type mandelbrotViewer struct {
maxIterations int
rMax float64
rMin float64
iMin float64
iMax float64
zoom float64
displayDebug bool
redraw bool
screenBuffer []byte
}
// mandelbrot computes the number of iterations necessary to determine whether
// the supplied complex number c and the Mandelbrot orbit sequence tends to
// infinity or not.
func mandelbrot(c complex128, maxIterations int) float64 {
var n int
var z complex128
for cmplx.Abs(z) <= 2 && n < maxIterations {
z = z*z + c
n++
}
return float64(n) - math.Log(math.Log2(cmplx.Abs(z)))
}
// interpolate is responsible for determining the new co-ordinates for the
// complex plane.
// See: https://stackoverflow.com/questions/41796832/smooth-zoom-with-mouse-in-mandelbrot-set-c
func interpolate(start, end, interpolation float64) float64 {
return start + ((end - start) * interpolation)
}
// mouseLocation returns the location on the complex plane where the mouse
// pointer is currently at.
func (v *mandelbrotViewer) mouseLocation() (float64, float64) {
mX, mY := ebiten.CursorPosition()
mouseRe := float64(mX)/(float64(*widthFlag)/(v.rMax-v.rMin)) + v.rMin
mouseIm := float64(mY)/(float64(*heightFlag)/(v.iMax-v.iMin)) + v.iMin
return mouseRe, mouseIm
}
// color returns a colour based on the current value of m.
func (v *mandelbrotViewer) color(m float64) color.Color {
_, f := math.Modf(m)
ncol := len(palette.Plan9)
c1, _ := colorful.MakeColor(palette.Plan9[int(m)%ncol])
c2, _ := colorful.MakeColor(palette.Plan9[int(m+1)%ncol])
r, g, b := c1.BlendHcl(c2, f).Clamped().RGB255()
return color.RGBA{r, g, b, 255}
}
func (v *mandelbrotViewer) debugPrint(screen *ebiten.Image) {
if !v.displayDebug {
return
}
var sb strings.Builder
x, y := v.mouseLocation()
sb.WriteString(fmt.Sprintf("TPS: %f\n", ebiten.CurrentTPS()))
sb.WriteString(fmt.Sprintf("Location: %f, %f\n", x, y))
sb.WriteString(fmt.Sprintf("Zoom: %f\n", v.zoom))
sb.WriteString(fmt.Sprintf("Max Iterations: %d\n", v.maxIterations))
// always return nil.
ebitenutil.DebugPrint(screen, sb.String())
}
func (v *mandelbrotViewer) zoomIn() {
v.redraw = true
v.zoom *= zoomFactor
}
func (v *mandelbrotViewer) zoomOut() {
v.redraw = true
v.zoom /= zoomFactor
}
func (v *mandelbrotViewer) pan(d panDirection) {
v.redraw = true
p := panFactor / v.zoom
switch d {
case up:
v.iMin -= p
case left:
v.rMin -= p
case down:
v.iMin += p
case right:
v.rMin += p
}
}
// reset sets the mandelbrot to how it was at the start.
func (v *mandelbrotViewer) reset() {
v.redraw = true
v.maxIterations = defaultIterations
v.rMin = rMin
v.rMax = rMax
v.iMin = iMin
v.iMax = iMax
v.zoom = zoom
}
func (v *mandelbrotViewer) increaseMaxIterations() {
v.redraw = true
v.maxIterations += iterationStep
}
func (v *mandelbrotViewer) decreaseMaxIterations() {
v.redraw = true
if v.maxIterations <= iterationStep {
return
}
v.maxIterations -= iterationStep
}
// Update handles input that manipulates the complex plan.
func (v *mandelbrotViewer) Update() error {
// Click to zoom and pan.
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
interpolation := 1.0 / zoomFactor
mouseRe, mouseIm := v.mouseLocation()
v.rMin = interpolate(mouseRe, v.rMin, interpolation)
v.iMin = interpolate(mouseIm, v.iMin, interpolation)
v.rMax = interpolate(mouseRe, v.rMax, interpolation)
v.iMax = interpolate(mouseIm, v.iMax, interpolation)
v.zoomIn()
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
v.pan(left)
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
v.pan(right)
}
if ebiten.IsKeyPressed(ebiten.KeyUp) {
v.pan(up)
}
if ebiten.IsKeyPressed(ebiten.KeyDown) {
v.pan(down)
}
// Increase/Decrease the max iterations.
if ebiten.IsKeyPressed(ebiten.KeyEqual) {
v.increaseMaxIterations()
}
if ebiten.IsKeyPressed(ebiten.KeyMinus) {
v.decreaseMaxIterations()
}
// Zoom in and out based on the scroll wheel, preserving location on the
// complex plane.
_, dY := ebiten.Wheel()
if dY < 0.0 {
v.zoomOut()
}
if dY > 0.0 {
v.zoomIn()
}
// Zoom out based on the scroll wheel, preserving location on the complex
// plane.
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
v.zoomOut()
}
// Reset back to factory defaults.
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
v.reset()
}
// Exit the proram if 'q' is pressed.
if inpututil.IsKeyJustPressed(ebiten.KeyQ) {
os.Exit(0)
}
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
ebiten.SetFullscreen(!ebiten.IsFullscreen())
}
if inpututil.IsKeyJustPressed(ebiten.KeyD) {
v.displayDebug = !v.displayDebug
}
// draw to screen buffer.
if len(v.screenBuffer) == 0 || v.redraw {
v.screenBuffer = v.render()
v.redraw = false
}
return nil
}
func (v *mandelbrotViewer) render() []byte {
pix := make([]byte, *widthFlag**heightFlag*4)
l := *widthFlag * *heightFlag
for i := 0; i < l; i++ {
x := i % *widthFlag
y := i / *widthFlag
cx := float64(x)/v.zoom + v.rMin
cy := float64(y)/v.zoom + v.iMin
c := complex(cx, cy)
m := mandelbrot(c, v.maxIterations)
r, g, b, a := v.color(m).RGBA()
pix[4*i] = byte(r)
pix[4*i+1] = byte(g)
pix[4*i+2] = byte(b)
pix[4*i+3] = byte(a)
}
return pix
}
// Draw displays the mandelbrot set.
func (v *mandelbrotViewer) Draw(screen *ebiten.Image) {
screen.ReplacePixels(v.screenBuffer)
v.debugPrint(screen)
}
// Layout takes the outside size (e.v., the window size) and returns the (logical) screen size.
func (v *mandelbrotViewer) Layout(outsideWidth, outsideHeight int) (int, int) {
return *widthFlag, *heightFlag
}
func main() {
flag.Parse()
ebiten.SetWindowTitle("Mandelbrot")
ebiten.SetWindowSize(*widthFlag, *heightFlag)
v := &mandelbrotViewer{
maxIterations: defaultIterations,
rMin: rMin,
rMax: rMax,
iMin: iMin,
iMax: iMax,
zoom: zoom,
displayDebug: true,
}
if err := ebiten.RunGame(v); err != nil {
panic(err)
}
}