Created
September 5, 2015 10:46
-
-
Save ptrelford/7f395ab6a96665208c28 to your computer and use it in GitHub Desktop.
Lambda Art
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 ( | |
"image" | |
"image/color" | |
"image/png" | |
"math" | |
"os" | |
) | |
type expr func(x,y float64) (r,g,b float64) | |
func toByte(x float64) uint8 { | |
return uint8(math.Max(0.0, math.Min(255.0, 128.0*(x+1.0)))) | |
} | |
func createImage(width int, height int, e expr) image.Image { | |
img := image.NewRGBA(image.Rect(0, 0, width, height)) | |
for y := 0; y < height; y++ { | |
for x := 0; x < width; x++ { | |
u := (float64(x-width/2)+0.5)/float64(width) | |
v := (float64(y-height/2)+0.5)/float64(height) | |
r, g, b := e(u, v) | |
c := color.RGBA{toByte(r), toByte(g), toByte(b), 255} | |
img.SetRGBA(x, y, c) | |
} | |
} | |
return img | |
} | |
func main() { | |
out, _ := os.Create("./output.png") | |
defer out.Close() | |
e := func(x,y float64) (r,g,b float64) { | |
r = math.Sin(y) | |
g = math.Sin(x) | |
b = x * y | |
return | |
} | |
img := createImage(640, 480, e) | |
png.Encode(out, img) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment