Skip to content

Commit

Permalink
Improve performance of Mandelbrot iteration (#14)
Browse files Browse the repository at this point in the history
* Skip obsolete escape check for start value z0

* Drop complex number primitive to avoid expensive logarithm operation
  • Loading branch information
joweich authored Jan 1, 2024
1 parent 8ddb4a8 commit 6ec22da
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions mandelbrot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"image/color"
"math"
"math/cmplx"
)

func getColorForComplexNr(c complex128) color.RGBA {
Expand All @@ -30,12 +29,12 @@ func getColorFromMandelbrot(isUnlimited bool, magnitude float64, iterations int)
func runMandelbrot(c complex128) (bool, float64, int) {
var z complex128

for i := 0; i < imgConf.MaxIter; i++ {
magnitude := cmplx.Abs(z)
if magnitude > 2 {
return true, magnitude, i
}
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 false, 0, 0
}

0 comments on commit 6ec22da

Please sign in to comment.