数学真美


Mandelbrot Emit

// Mandelbrot emits a PNG image of the Mandelbrot fractal.
package main

import (
 “image”
 “image/color”
 “image/png”
 “math/cmplx”
 “os”
)

func main() {
 const (
 xmin, ymin, xmax, ymax = -2, -2, +2, +2
 width, height = 1024, 1024
 )

img := image.NewRGBA(image.Rect(0, 0, width, height))
 for py := 0; py < height; py++ {
 y := float64(py)/height*(ymax-ymin) + ymin
 for px := 0; px < width; px++ {
 x := float64(px)/width*(xmax-xmin) + xmin
 z := complex(x, y)
 // Image point (px, py_ represents complex value z.
 img.Set(px, py, mandelbrot(z))
 }
 }
 png.Encode(os.Stdout, img) // NOTE: ignoring errors
}

func mandelbrot(z complex128) color.Color {
 const (
 iterations = 200
 contrast = 15
 )
 var v complex128
 for n := uint8(0); n < iterations; n++ {
 v = v*v + z
 if cmplx.Abs(v) > 2 {
 return color.Gray{255 — contrast*n}
 }
 }
 return color.Black
}

Like what you read? Give 哈哈 a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.