go Tour练习( 二维切片)(图像)

https://tour.go-zh.org/moretypes/18

实现 Pic。它应当返回一个长度为 dy 的切片,其中每个元素是一个长度为 dx,元素类型为 uint8 的切片

package main

import "golang.org/x/tour/pic"

func Pic(dx, dy int) [][]uint8 {
	out:=make([][]uint8,dy)
	for i:=0;i<dy;i++{
		out[i]=make([]uint8,dx)
		for j:=0;j<dx;j++{
			out[i][j]=uint8(i*j)
		}
	}
	return out
}

func main() {
	pic.Show(Pic)
}

https://tour.go-zh.org/methods/25

定义你自己的 Image 类型,实现必要的方法并调用 pic.ShowImage

package main
import (
	"golang.org/x/tour/pic"
	"image/color"
	"image"
)

type Image struct{
	w int
	h int
}
func (img Image) ColorModel() color.Model{
	return color.RGBAModel
}
func (img Image) Bounds() image.Rectangle{
	return image.Rect(0,0,img.w,img.h)
}
func (img Image) At(x, y int) color.Color{
	v:=uint8(x*y)
	return color.RGBA{v,v, 255, 255}
}

func main() {
	m := Image{256,256}
	pic.ShowImage(m)
}

图像结果与上面一样

猜你喜欢

转载自blog.csdn.net/yaoyutian/article/details/89792923