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)
}

图像结果与上面一样

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值