Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

benhoyt commented Oct 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.