Skip to content

Instantly share code, notes, and snippets.

@benhoyt
Created September 21, 2018 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhoyt/24b9caf9715659c76f7b652bbdc8a834 to your computer and use it in GitHub Desktop.
Save benhoyt/24b9caf9715659c76f7b652bbdc8a834 to your computer and use it in GitHub Desktop.
Go program to print the Mandelbrot set on stdout
// Print the Mandelbrot set on stdout
package main
import (
"fmt"
"math/cmplx"
)
const (
width = 160
height = 60
min = -2.1 - 1.2i
max = 0.6 + 1.2i
iterations = 32
colors = ".-+*%#$@ "
)
func main() {
incX := complex(real(max-min)/width, 0)
incY := complex(0, imag(max-min)/height)
c := min
for row := 0; row < height; row++ {
for col := 0; col < width; col++ {
z := 0 + 0i
var i int
for i = 0; i < iterations; i++ {
z = z*z + c // The core Mandelbrot equation!
if cmplx.Abs(z) > 2 {
break
}
}
fmt.Printf("%c", colors[i*(len(colors)-1)/iterations])
c += incX
}
fmt.Printf("\n")
c += incY - real(max-min)
}
}
@benhoyt
Copy link
Author

benhoyt commented Oct 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment