Exercise: Slices

系统管理员 2021-12-16 02:39 363阅读 0赞

Exercise: Slices

题目:

  1. 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,
  2. it will display your picture, interpreting the integers as grayscale (well, bluescale) values.
  3. The choice of image is up to you. Interesting functions include (x+y)/2, x*y, and x^y.
  4. (You need to use a loop to allocate each []uint8 inside the [][]uint8.)
  5. (Use uint8(intValue) to convert between types.)
  6. 实现Pic。它应返回一个长度为dyslice,其中每个元素都是一个大小为dx8bit无符号整型slice。当你运行程序,将会显示你的图片。
  7. 图像的选择权在于你,有趣的函数包括 (x+y)/2, x*y, and x^y

代码:

  1. package main
  2. import "golang.org/x/tour/pic"
  3. func Pic(dx, dy int) [][]uint8 {
  4. image := make([][]uint8, dy)
  5. for y := 0; y < dy; y++ {
  6. image[y] = make([]uint8, dx)
  7. for x := 0; x < dx; x++ {
  8. image[y][x] = (uint8)(x^y) % 255
  9. }
  10. }
  11. return image
  12. }
  13. func main() {
  14. pic.Show(Pic)
  15. }

 结果:

809147-20160523233055756-456709570.png

转载于:https://www.cnblogs.com/chpx/p/5521943.html

发表评论

表情:
评论列表 (有 0 条评论,363人围观)

还没有评论,来说两句吧...

相关阅读

    相关 slice

    概念 Slice切片是对底层数组Array的封装,在内存中的存储本质就是数组,体现为连续的内存块,Go语言中的数组定义之后,长度就已经固定了,在使用过程中并不能改变其长度,而

    相关 Rust:slice

    > slice是String的一部分引用。类似切片。 字符串slice 字符串slice:是一些存储在别处的UTF-8编码字符串数据的引用。 slice获取值的使用