Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ func BenchmarkGetColorFromMandelbrotUnlimited(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
getColorFromMandelbrot(true, magnitude, iterations)
iterResult := MandelbrotIterResult{
IsUnlimited: true,
Magnitude: magnitude,
Iterations: iterations,
}
getColorFromMandelbrot(iterResult)
}
}

Expand Down
6 changes: 6 additions & 0 deletions customTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ type ImageConfig struct {
Grayscale bool
RndGlobal uint64
}

type MandelbrotIterResult struct {
IsUnlimited bool
Magnitude float64
Iterations int
}
18 changes: 12 additions & 6 deletions mandelbrot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ func getColorForComplexNr(c complex128) color.RGBA {
return getColorFromMandelbrot(runMandelbrot(c))
}

func getColorFromMandelbrot(isUnlimited bool, magnitude float64, iterations int) color.RGBA {
if isUnlimited {
func getColorFromMandelbrot(iterResult MandelbrotIterResult) color.RGBA {
if iterResult.IsUnlimited {
// adapted http://linas.org/art-gallery/escape/escape.html
smooth := (float64(iterations) + 1 - math.Log(math.Log(magnitude))/math.Log(2)) / float64(imgConf.MaxIter)
smooth := (float64(iterResult.Iterations) + 1 - math.Log(math.Log(iterResult.Magnitude))/math.Log(2)) / float64(imgConf.MaxIter)
offset := smooth + imgConf.Offset
mod := math.Mod(offset, 1)
if imgConf.Grayscale {
Expand All @@ -26,15 +26,21 @@ func getColorFromMandelbrot(isUnlimited bool, magnitude float64, iterations int)
}
}

func runMandelbrot(c complex128) (bool, float64, int) {
func runMandelbrot(c complex128) MandelbrotIterResult {
var z complex128

for i := 1; i < imgConf.MaxIter; i++ {
z = z*z + c
magnitudeSquared := real(z)*real(z) + imag(z)*imag(z)
if magnitudeSquared > 4 {
return true, math.Sqrt(magnitudeSquared), i
return MandelbrotIterResult{
IsUnlimited: true,
Magnitude: math.Sqrt(magnitudeSquared),
Iterations: i,
}
}
}
return false, 0, 0
return MandelbrotIterResult{
IsUnlimited: false,
}
}