Created
April 4, 2016 15:24
-
-
Save a-h/5202d8b95a2e834c1c371ea27754d322 to your computer and use it in GitHub Desktop.
Mandelbrot Set (The Go Programming Language)
This file contains hidden or 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
| 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) | |
| img.Set(px, py, mandelbrot(z)) | |
| } | |
| } | |
| op, _ := os.Create("op.png") | |
| png.Encode(op, img) | |
| op.Close() | |
| } | |
| func mandelbrot(z complex128) color.Color { | |
| const iterations = 200 | |
| const 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 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment