Exercise: Slices
ImplementPic
. It should return a slice of length dy
, each element of which is a slice of dx
8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. The choice of image is up to you. Interesting functions include
(x+y)/2
, x*y
, and x^y
. (You need to use a loop to allocate each
[]uint8
inside the [][]uint8
.) (Use
uint8(intValue)
to convert between types.) .
Solution:
package main
import "golang.org/x/tour/pic"
//import "fmt"
func Pic(dx, dy int) [][]uint8 {
var la_pic [][]uint8
var la_x []uint8
la_x = make([]uint8, dx)
//la_pic = make([][]uint8, 0)
for i := 0; i < dy; i++ {
la_pic = append(la_pic, la_x)
for j := 0; j < dx; j++ {
la_pic[i][j] = uint8((i ^ j))
//fmt.Println(i, j)
}
}
return la_pic
}
func main() {
pic.Show(Pic)
}
Reference:
http://tour.golang.org/moretypes/15
No comments:
Post a Comment