A
A
Anton2019-01-09 08:42:14
go
Anton, 2019-01-09 08:42:14

How to understand the task condition https://go-tour-en-us.appspot.com/moretypes/18?

How to understand the task condition https://go-tour-en-us.appspot.com/moretypes/18 ?
I wrote the function like this.

package main

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

array = [][]uint8

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

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

the phrase "The choice of the image is yours. Among the interesting functions are (x + y) / 2, x * y and x ^ y."

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2019-01-09
@deliro

Return a two-dimensional array uint8, where the values ​​are colors (a number from 0 to 255). For example like this:

func Pic(dx, dy int) [][]uint8 {
  result := make([][]uint8, dy)
  
  for i := range result {
    result[i] = make([]uint8, dx)
  }
  
  for i := range result {
    for j := range result[i] {
      result[i][j] = uint8((i + j) / 2)
    }
  }
  
  return result
}

A
Alexander Pavlyuk, 2019-01-09
@pav5000

You need to create a slice of slices (two-dimensional array) of the given size.

More or less like this
package main

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

func Pic(dx, dy int) [][]uint8 {
  array := make([][]uint8, dy)
  for y := 0; y < dy; y++ {
    array[y] = make([]uint8, dx)
    for x := 0; x < dx; x++ {
      pixel := uint8((x + y) / 2)
      array[y][x] = pixel
    }
  }
  return array
}

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

The function sets the dependence of the color of a point on its coordinates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question