go语言练习题代写 go语言练习题辅导 go语言辅导
Exercise 10
Create .go files as described below. itself to be in package exer10.
Concurrent Fibonacci
In fibonacci.go, concurrently. The function: func Fib(n uint, cutoff uint) uint { ... }
... should calculate the n-th Fibonacci number. It should do this (using the worst algorithm in the
world which is) recursively using the identities:
The if n > cutoff and sequential otherwise.
Test values for cutoff and choose something good as a default in this function (and include it in your code). It should definitely be larger than zero:
func Fibonacci(n uint) uint {
return Fib(n, 0)
}
Go Interfaces
In the last exercises, we have created the Point struct, including .Rotate and .Scale methods. Copy your points.go to this exercise (and change the package to exer10).
Let's create another struct to represent a geometric thing where we could do similar operations:
type Triangle struct { A, B, C Point
}
func (t Triangle) String() string {
return fmt.Sprintf("[%s %s %s]", t.A, t.B, t.C)
In triangle.go, copy this code and .Scale and .Rotate that have similar behaviour to the ones
Transformable that includes any struct that has .Scale and .Rotate methods like
these structs do.
TurnDouble(transf, angle) that takes a Transformable instance and and angle. func TurnDouble(t Transformable, angle float64) { ... }
It should double the size (scale 2) and rotate by the given angle in-place. Then this code:
pt := Point{3, 4} TurnDouble(&pt, 3*math.Pi/2) fmt.Println(pt) tri := Triangle{Point{1, 2}, Point{-3, 4}, Point{5, -6}} TurnDouble(&tri, math.Pi) fmt.Println(tri)
Should output (probably with some numerical error that I edited out for simplicity):
(8, -6) [(-2, -4) (6, -8) (-10, 12)]
Images
In this question, we will explore some of the Go built-in pacakges :
func DrawCircle(outerRadius, innerRadius int, outputFile string) { ... }
. In image.go,
In that function, draw a 200×200 PNG image with a circle. The circle should inner- and outer-radius as specified. That is, if a pixel is between innerRadius and outerRadius pixels from the point (100,100), colour it black; else colour it white.
-
Create an image.Rectangle from (0,0) to (200,200) to describe the bounds of the image.
-
Use image.newRGBA to create a grid of pixels to draw on.
-
Iterate through each pixel and colour it appropriately. Always set the alpha (A) value to 255, so the
pixel is opaque.
-
Use os.Create to create the file handle to write.
-
Ensure that the file handle is always closed (defer).
-
Use png.Encode to write the image.
-
Handle all of the errors that might be returned (from the file writing) and deal with them. It's okay
for this exercise to panic, but you should at least acknowledge errors if they occur.
You will need things from the image ,
image/png , and os
packages for this.
The format of the RGBA struct might not be obvious: the pixel data is flattened into a one-dimensional slice of uint8 values. The first values in the array specify the first pixel's R, G, B, A (alpha channel) values. The next four are the second pixel, etc.
The first 4×width values describe the first row of the image, and so on. The struct's .Stride value gives the number of slice elements needed to describe one row (4×width). The docs give a formula for the position (of the R component) of a specific pixel.
You can compare my output for DrawCircle(40, 20, "out.png").
