재미지군!


package main


import (

        "image"

        "image/color"

        "image/png"

        "log"

        "math"

        "os"

)


const (

        width, height          = 1024, 1024

        amin, bmin, amax, bmax = -2, -2, 2, 2

        iterations             = 500

        contrast               = 20

)


var max int


func main() {

        file, err := os.Create("Mandelbrot_Fractal.png")

        if err != nil {

                log.Fatal(err)

        }

        defer file.Close()


        img := image.NewRGBA(image.Rect(0, 0, width, height))

        for y := 0; y < height; y++ {

                b := float64(y)/height*(bmax-bmin) + bmin

                for x := 0; x < width; x++ {

                        a := float64(x)/width*(amax-amin) + amin

                        img.Set(x, y, makeColor(a, b))

                }

        }


        if err := png.Encode(file, img); err != nil {

                log.Fatal(err)

        }

}


func makeColor(a, b float64) color.Color {

        var x, y float64

        for n := 0; n < iterations; n++ {

                x, y = x*x-y*y+a, 2*x*y+b

                if math.Hypot(x, y) > 2 {

                        return color.Gray{255 - contrast*uint8(n)}

                }

        }

        return color.Black

}