Exercise Slices
题目:
Implement Pic
. 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.)
练习程序:
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
dxy := make([][]uint8, dy)
for i:=0; i<dy; i++{
dz:= make([]uint8, dx)
for j:=0; j<dx; j++{
dz[j]=uint8((i*j))
}
dxy[i]=dz
}
return dxy
}
func main() {
pic.Show(Pic)
}
程序运行结果:
笔记:该程序实现了一个存储无符号8位整数的二维slice,二维阵列中的每一个整数都代表了图像的灰度值,因此程序的输出可以转化为一个二维的图像。通过改变整数的计算公式(目前这里用的是i*j, 就可以得到不同的图像)