👍 go run mandelbrot.go > m.png
👍 cat mandelbrot.go
package main
import (
"image"
"image/color"
"image/png"
"math/cmplx"
"os"
)
func main() {
const (
xmin,ymin,xmax,ymax = -2,-2,2,2
w, h = 1024, 1024
)
img := image.NewRGBA(
image.Rect(0, 0, w, h))
for py := 0; py < h; py++ {
y:=float64(py)/h*(ymax-ymin)+ymin
for px := 0; px < w; px++ {
x:=float64(px)/w*(xmax-xmin)+xmin
z:=complex(x, y)
img.Set(px, py, m(z))
}
}
png.Encode(os.Stdout, img)
}
func m(z complex128)color.Color{
const itrs = 200
const contrast = 15
var v complex128
for n := uint8(0); n < itrs; n++ {
v = v*v + z
if cmplx.Abs(v) > 2 {
return color.Gray{255-contrast*n}
}
}
return color.Black
}
ch3.3 Complex Numbers p80/61 of