Skip to content

Instantly share code, notes, and snippets.

@will-ockmore
Created January 2, 2018 12:08
Show Gist options
  • Save will-ockmore/cb448bca58a318c50a2800fa2fe2270f to your computer and use it in GitHub Desktop.
Save will-ockmore/cb448bca58a318c50a2800fa2fe2270f to your computer and use it in GitHub Desktop.
Solution to golang tour slices exercise (https://tour.golang.org/moretypes/18)
package main
import "golang.org/x/tour/pic"
import "math"
func makePic(f func(x, y int) uint8) func(dx, dy int) [][]uint8 {
pic := func(dx, dy int) [][]uint8 {
// 2d array of pixel values
p := make([][]uint8, dy)
for y := range p {
p[y] = make([]uint8, dx)
for x := range p[y] {
p[y][x] = f(x, y)
}
}
return p
}
return pic
}
func a(x, y int) uint8 {
return uint8((x+y)/2)
}
func b(x, y int) uint8 {
return uint8(y - int(16 * math.Sin(float64(x*2))) - 130)
}
func c(x, y int) uint8 {
return (uint8(x) << (uint8(y)/32))
}
func main() {
pic.Show(makePic(c))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment