Created
January 2, 2018 12:08
-
-
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)
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 "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