Created
May 16, 2015 02:47
-
-
Save nanki/53ed40d5a4b6157896a6 to your computer and use it in GitHub Desktop.
Vintage and modern Mandelbrot set in Go.
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 "fmt" | |
import "math/cmplx" | |
func dot(bits uint8) string { | |
bits = bits & 7 | (bits & 112) >> 1 | (bits & 8) << 3 | bits & 128 | |
return string(0x2800 + uint32(bits)) | |
} | |
func mandelbrot(x float64, y float64, iteration int) uint8 { | |
var c complex128 = complex(x, y) | |
var r = c | |
for i := 0; i < iteration; i ++ { | |
r = r * r + c | |
if cmplx.Abs(r) > 2 { | |
return 0 | |
} | |
} | |
return 1 | |
} | |
func main() { | |
var R = 40 | |
for y := -R / 2; y < R / 2; y++ { | |
for x := -R; x < R; x++ { | |
if mandelbrot(-0.7 + 0.7 * float64(x * 2) / float64(R), 0.7 * float64(y *4) / float64(R), 1000) > 0 { | |
fmt.Print("*") | |
} else { | |
fmt.Print(" ") | |
} | |
} | |
fmt.Println() | |
} | |
for y := -R / 2; y < R / 2; y++ { | |
for x := -R; x < R; x++ { | |
var r uint8 = 0 | |
for i := 0; i < 8; i++ { | |
r |= mandelbrot(-0.7 + 0.7 * float64(x * 2 + ((i & 4) >> 2)) / float64(R), 0.7 * float64(y * 4 + (i & 3)) / float64(R), 1000) << uint8(i) | |
} | |
fmt.Print(dot(uint8(r))) | |
} | |
fmt.Println() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment