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 {
s := make([][]uint8, dy)
for i := range s {
s[i] = make([]uint8, dx)
for j := range s[i] {
s[i][j] = uint8(i ^ j)
}
}
return s
}
func main() {
pic.Show(Pic)
}
本文介绍了一个使用Go语言实现的简单图片生成程序。通过填充二维切片来创建图像,并利用整数运算如XOR操作生成视觉效果。文章展示了如何定义Pic函数以生成由灰度值组成的图像,并使用了内置的显示函数进行展示。

被折叠的 条评论
为什么被折叠?



