Created
September 21, 2018 01:07
-
-
Save benhoyt/24b9caf9715659c76f7b652bbdc8a834 to your computer and use it in GitHub Desktop.
Go program to print the Mandelbrot set on stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Runnable version: https://play.golang.org/p/oxo5iXlIzLe