Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 02:40
Show Gist options
  • Save tetsuok/2280162 to your computer and use it in GitHub Desktop.
Save tetsuok/2280162 to your computer and use it in GitHub Desktop.
An answer of the exercise: Slices on a tour of Go
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
// Allocate two-dimensioanl array.
a := make([][]uint8, dy)
for i := 0; i < dy; i++ {
a[i] = make([]uint8, dx)
}
// Do something.
for i := 0; i < dy; i++ {
for j := 0; j < dx; j++ {
switch {
case j % 15 == 0:
a[i][j] = 240
case j % 3 == 0:
a[i][j] = 120
case j % 5 == 0:
a[i][j] = 150
default:
a[i][j] = 100
}
}
}
return a
}
func main() {
pic.Show(Pic)
}
@joksdz
Copy link

joksdz commented Jun 26, 2024

package main

import (
	"math"

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

func Pic(dx, dy int) [][]uint8 {
	pic := make([][]uint8, dy, dy)

	for y := range pic {
		pic[y] = make([]uint8, dx)

		for x := range pic[y] {
			pic[y][x] = uint8(float64(x^y-y^x) / math.Pi)
		}
	}

	return pic
}

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

image

@joksdz
Copy link

joksdz commented Jun 26, 2024

package main

import (
	"math"

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

func Pic(dx, dy int) [][]uint8 {
	pic := make([][]uint8, dy, dy)
	i := 10
	for y := range pic {
		pic[y] = make([]uint8, dx)

		for x := range pic[y] {

			i++
			pic[y][x] = uint8(float64((x+y)*i) / math.Pi)
		}
	}

	return pic
}

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

image

@sturmenta
Copy link

package main

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

func Pic(dx, dy int) [][]uint8 {
	var image [][]uint8

	for y := 0; y < dy; y++ {
		var row []uint8

		for x := 0; x < dx; x++ {
			row = append(row, uint8(x*10 + y*100))
		}

		image = append(image, row)
	}

	return image
}

func main() {
	pic.Show(Pic)
}
Screenshot 2024-07-16 at 1 42 41 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment