Created
August 29, 2022 20:23
-
-
Save tkanerva/387f3782e285a6985b08845f72dbba48 to your computer and use it in GitHub Desktop.
gomand
This file contains 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 ( | |
"bufio" | |
"log" | |
"os" | |
//"fmt" | |
"unsafe" | |
) | |
func mand(c complex64) int { | |
var z complex64 | |
z = 0+0i | |
iter := 0 | |
for { | |
z = z*z + c | |
iter += 1 | |
zr := real(z) | |
zi := imag(z) | |
if (iter >= 250 || (zr*zr + zi*zi > 4.0)) { | |
break | |
} | |
} | |
return iter | |
} | |
func xform(x int, y int) complex64 { | |
var a float32 | |
var b float32 | |
a = float32(x) | |
b = float32(y) | |
return complex(-2.0 + 4.0*(a/500.0), 2.0 - 4.0*(b/500.0)) | |
} | |
type RGB24 struct { | |
Red uint8 | |
Green uint8 | |
Blue uint8 | |
} | |
type ByteSliceRGB struct { | |
Addr *RGB24 | |
Len int | |
Cap int | |
} | |
func output(w *bufio.Writer, i int) { | |
pix := RGB24{ uint8(i), uint8(i/2), 0, } | |
slice := &ByteSliceRGB{&pix, 3, 3} // struct 3 bytes long | |
var byteSlice []byte = *(*[]byte)(unsafe.Pointer(slice)) | |
w.Write(byteSlice) | |
} | |
func main() { | |
var i int | |
var x int | |
var y int | |
file, err := os.Create("./kuva.ppm") | |
if err != nil { | |
log.Fatal(err) | |
} | |
writer := bufio.NewWriter(file) | |
writer.WriteString("P6\n500 500\n255\n") | |
y = 0 | |
for y < 500 { | |
x = 0 | |
for x < 500 { | |
i = mand(xform(x, y)) | |
output(writer, i) | |
x++ | |
} | |
y++ | |
} | |
writer.Flush() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment