A Tour of Go Exercise: Images

Remember the picture generator you wrote earlier? Let‘s write another one, but this time it will return an implementation of image.Image instead of a slice of data.

Define your own Image type, implement the necessary methods, and callpic.ShowImage.

Bounds should return a image.Rectangle, like image.Rect(0, 0, w, h).

ColorModel should return color.RGBAModel.

At should return a color; the value v in the last picture generator corresponds to color.RGBA{v, v, 255, 255} in this one.

package main

import (
    "code.google.com/p/go-tour/pic"
    "image"
    "image/color"
)

type Image struct{}

func (img Image) ColorModel() color.Model {
    return color.RGBAModel
}

func (img Image) Bounds() image.Rectangle {
    return image.Rect(0, 0, 100, 100)
}

func (img Image) At(x, y int) color.Color {
    return color.RGBA{uint8(x), uint8(y), 255, 255}
}

func main() {
    m := Image{}
    pic.ShowImage(m)
}
时间: 2024-10-21 05:49:01

A Tour of Go Exercise: Images的相关文章

A Tour of Go Exercise: HTTP Handlers

Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server. type String string type Struct struct { Greeting string Punct string Who string } For example, you should be able to regist

A Tour of Go Exercise: Loops and Functions

As a simple way to play with functions and loops, implement the square root function using Newton's method. In this case, Newton's method is to approximate Sqrt(x) by picking a starting point z and then repeating: To begin with, just repeat that calc

A Tour of Go Exercise: Fibonacci closure

Let's have some fun with functions. Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers. package main import "fmt" // fibonacci is a function that returns // a function that returns an int

A Tour of Go Exercise: Errors

Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers. Create a new type type ErrNegativeSqrt float64 an

A Tour of Go 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

Exercise: Maps (单词统计)

A Tour of Go Exercise: Maps https://tour.golang.org/moretypes/23 WordCount (单词统计) 是一个很经典的小程序了,在很多编程入门教程中都会出现. 这道题比较简单,但也有一些知识点值得一提. 上面这个答案我是参考了网上别人写的.但在参考别人之前我也自己解题了,其中,唯一不同之处是这一句: m[word]++ 我本来写的是: _, ok := m[word] if ok { m[word]++ } else { m[word]

Exercise: Slices (画图)

A Tour of Go Exercise: Slices https://tour.golang.org/moretypes/18 这道题目,提供了一个画图函数 (pic.Show), 可以生成图片. 这个函数,即 pic.Show(f func(int, int) [][]uint8), 可见,它接受一个函数做参数,题目要求的正是编写这个参数.答案如下: 这里面,依赖一个 package, 即 "golang.org/x/tour/pic" 我上 https://github.co

Golang,开车上路。

2017年1月29日,开始学习 golang, 一边学习一边做笔记. ◆ Golang 的开发环境非常容易搭建,双击安装包安装后,设置一个系统变量即可. ◆ 利用当前时间来做随机数的种子. ◆ 教程里说: A nil map has no keys, nor can keys be added. 但是没有给出具体的例子,于是我做了以下试验: var m map[string]int // 构建一个空map m["abc"] = 123 // 对空map添加key fmt.Println

A Tour of Go Advanced Exercise: Complex cube roots

Let's explore Go's built-in support for complex numbers via the complex64 and complex128 types. For cube roots, Newton's method amounts to repeating: Find the cube root of 2, just to make sure the algorithm works. There is a Pow function in the math/