Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 02:40
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • 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)
}
@waqasilyas
Copy link

uint8(j * 1000 / (i + 1))
image

or
uint8(j * 100000 / (i + 1))
image

@Kenny50
Copy link

Kenny50 commented Jun 13, 2021

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

func Pic(dx, dy int) [][]uint8 {
	s := make([][]uint8,dy)
	for i := range s{
		s[i] = make([]uint8, dx)
		for j := range s[i] {
			switch {
				case i%(dx/8) >= (dx/16) && j % (dx/8) >=(dx/16) :
					s[i][j] = uint8(1)
				case i%(dx/8) >= (dx/16) || j % (dx/8) >=(dx/16) :
					s[i][j] = uint8(255)
				default :
					s[i][j] = uint8(1)
			}
		}
	}
	return s
}


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

image

@Rubbybutton
Copy link

package main

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

func Pic(dx, dy int) [][]uint8 {
	var x [][]uint8
	for i:=0;i<dy;i++ {
		var tmp []uint8
		for j:=0;j<dx;j++{
			tmp = append(tmp,uint8(j))
		}
		x = append(x,tmp)
	}
	return x
}

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

@nazlicankurt
Copy link

nazlicankurt commented Jan 6, 2022

package main

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

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

    img := make([][]uint8, dy)

    for y := range img {

        img[y] = make([]uint8, dx)

        for x := range img[y] {
			img[y][x] = uint8( (x+y)/2 * (y-x) -220)
        }
    }

    return img
}


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

test1

@SimZhou
Copy link

SimZhou commented Feb 8, 2022

You guys are so creative

@mankeheaven
Copy link

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

	for j := 0; j < dy; j++ {
		// Allocate inner slice into outer slice
		inner := make([]uint8, dx)
		out[j] = inner

		// Store values
		for k := 0; k < dx; k++ {
			out[j][k] = uint8(j*j+k*k)
		}
	}

	return out
}

image

@mohitajampala
Copy link

mohitajampala commented Apr 18, 2022

func Pic(dx, dy int) [][]uint8 {
	result := make([][]uint8, dy)
	for i := 0; i < dy; i++ {
		temp := make ([]uint8, dx)
		result[i] = temp
	}
	
	for i := 0; i < dy; i++ {
		for j := 0; j < dx; j++ {
			switch {
			case j % 2 == 1:
				result[i][j] = uint8(i*j)
			default:
				result[i][j] = uint8(i+j)
			}
		}
	}	
	
	return result;
}

download

@luigiraffale
Copy link

//Magnetic Field (properly, Magnetic Induction) around straight conductors, with currents flowing in opposite directions.

package main

import (
"math"
//"fmt"
"golang.org/x/tour/pic"
)

func Pic(dx, dy int) (out [][]uint8) {
for i := 0; i < dy; i++ {
row := make([]uint8, dx)
for j := range row {
row[j] = uint8(Bfield(float64(i), float64(j)))
}
out = append(out, row)
}
return out
}

func Bfield(xp, yp float64) (Bvalue float64) {
const mu0 = 4e-7 * math.Pi //Vacuum permeability
const mu = 0.5 * mu0 / math.Pi //Multiplicative Factor
const I1, I2 = 8e4, -8e4 //current in conductors 1 & 2
const X1, Y1 = 128, 192 //abscissa, ordinate of conductor1
const X2, Y2 = 128, 64 //abscissa, ordinate of conductor2

Bxvalue1 := mu * I1 * (Y1 - yp) / (math.Pow(X1-xp, 2) + math.Pow(Y1-yp, 2))
Byvalue1 := mu * I1 * (xp - X1) / (math.Pow(X1-xp, 2) + math.Pow(Y1-yp, 2))
Bxvalue2 := mu * I2 * (Y2 - yp) / (math.Pow(X2-xp, 2) + math.Pow(Y2-yp, 2))
Byvalue2 := mu * I2 * (xp - X2) / (math.Pow(X2-xp, 2) + math.Pow(Y2-yp, 2))
Bvalue = (1e6 * (math.Pow(math.Pow(Bxvalue1+Bxvalue2, 2)+math.Pow(Byvalue1+Byvalue2, 2), 0.5)))
return Bvalue

}

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

download

@andremarcondesteixeira
Copy link

andremarcondesteixeira commented May 3, 2022

package main

import "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(x*x*y*y << 2)
		}
	}
	
	return pic
}

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

v1

@andremarcondesteixeira
Copy link

andremarcondesteixeira commented May 3, 2022

package main

import "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((x*x+y*y)/2 >> 2)
		}
	}
	
	return pic
}

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

v2

@andremarcondesteixeira
Copy link

andremarcondesteixeira commented May 3, 2022

package main

import "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((x*x+y*y))
		}
	}
	
	return pic
}

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

v3

@dumindu
Copy link

dumindu commented May 5, 2022

The Simplest :)

package main

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

func Pic(dx, dy int) [][]uint8 {
	eles := make([]uint8, dx)
	pic := make([][]uint8, dy)
	for k := range pic {
		pic[k] = eles
	}

	return pic
}

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

Unknown

@DanNduati
Copy link

DanNduati commented Jul 2, 2022

Basic:

package main

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

func Pic(dx, dy int) [][]uint8 {
	ss := make([][]uint8, dy)
	for y := 0; y < dy; y++ {
		s := make([]uint8, dx)
		for x := 0; x < dx; x++ {
			s[x] = uint8(x * y)
		}
		ss[y] = s
	}
	return ss
}

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

Output:
image

@thomsebastin
Copy link

thomsebastin commented Jul 8, 2022

package main

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

func Pic(dx, dy int) [][]uint8 {
	x := make([][]uint8, dy)
	for i:= range x {
		y := make([]uint8, dx)
		for j := range y {
			y[j] = uint8(i ^ j) * 4
		}
		x[i] = y
	}
	return x
}

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

image

@bradleyGamiMarques
Copy link

package main

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

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

	for i := range picture {
		picture[i] = make([]uint8, dx)
	}

	for i := 0; i < dy; i++ {
		for j := 0; j < dx; j++ {
			picture[i][j] = uint8((i^j) - (i+j))
		}
	}
	return picture
}

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

image

@ericboy0224
Copy link

package main

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

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

	for i := range result {
		result[i] = make([]uint8, dx)

		for j := range result[i] {
			result[i][j] = uint8(i ^ 2 + 2*i*j + j ^ 2)
		}
	}

	return result
}

goslice

@moalghifari
Copy link

moalghifari commented Oct 12, 2022

package main

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

func Pic(dx, dy int) [][]uint8 {
	ret := make([][]uint8, dy)
	for y := 0; y < dy; y++ {
		ret[y] = make([]uint8, dx)
	}
	for y := 0; y < dy; y++ {
		for x := 0; x < dx; x++ {
			ret[y][x] = uint8(x*x ^ y*y)
		}
	}
	return ret
}

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

download

@fatihayan61
Copy link

package main

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

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

	for i := 0; i < dy; i++ {
		res[i] = make([]uint8, dx)
		for j := range res[i] {
			res[i][j] = uint8((i * j * i * j / 2) * (j * i * j * i / 2))
		}

	}
	return res
}

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

Playground

@JPCodaLot
Copy link

package main

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

func Pic(dx, dy int) (picture [][]uint8) {
	picture = make([][]uint8, dy)
	for y := range picture {
		picture[y] = make([]uint8, dx)
		for x := range picture[y] {
			if y % 2 == 1 {
				picture[y][x] = uint8((y+x*2)^(y-x*2))
			} else {
				picture[y][x] = uint8((x+y*2)^(x-y*2))
			}
		}
	}
	return picture
}

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

download

@krapie
Copy link

krapie commented Dec 6, 2022

package main

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

func Pic(dx, dy int) [][]uint8 {
	s := make([][]uint8, dy)
	for y := range s {
		s[y] = make([]uint8, dx)
		for x := range s[y] {
			s[y][x] = interpretCoordinate(x,y)
		}
	}
	
	return s
}

func interpretCoordinate(x, y int) uint8 {
	n := x^y
	
	return uint8(n)
}

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

download

@odashi
Copy link

odashi commented Dec 8, 2022

package main

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

func Pic(dx, dy int) [][]uint8 {
	s := make([][]uint8, dy)
	for y := range s {
		s[y] = make([]uint8, dx)
		for x := range s[y] {
			s[y][x] = gopher(x, y)
		}
	}

	return s
}

func gopher(x, y int) uint8 {
    xx := float64(x)
	yy := float64(y)
	
	params := [][6]float64 {
		{180, 50, 3, 3, 0, 255},
		{175, 50, 10, 10, 0, 0},
		{105, 80, 3, 3, 0, 255},
		{100, 80, 10, 10, 0, 0},
		{155, 55, 30, 30, 0, 255},
		{155, 55, 32, 32, 0, 0},
		{80, 85, 30, 30, 0, 255},
		{80, 85, 32, 32, 0, 0},
		{130, 90, 7, 1, 24, 255},
		{125, 95, 13, 8, 24, 0},
		{133, 128, 12, 5, 114, 255},
		{133, 128, 13, 6, 114, 0},
		{143, 124, 12, 5, 116, 255},
		{143, 124, 13, 6, 116, 0},
		{130, 108, 22, 12, 23, 160},
		{130, 108, 24, 14, 23, 0},
		{183, 178, 14, 8, -20, 255},
		{183, 178, 15, 9, -20, 0},
		{131, 108, 96, 100, 0, 220},
		{151, 158, 90, 100, 0, 220},
		{151, 188, 90, 100, 0, 220},
		{243, 148, 14, 8, -20, 255},
		{243, 148, 15, 9, -20, 0},
		{205, 36, 2, 2, 0, 255},
		{203, 42, 10, 10, 0, 0},
		{206, 44, 21, 21, 0, 255},
		{206, 44, 23, 23, 0, 0},
		{33, 102, 10, 10, 0, 0},
		{36, 104, 21, 21, 0, 255},
		{36, 104, 23, 23, 0, 0},
	}
    
	for _, p := range params {
		if judge(xx, yy, p[0], p[1], p[2], p[3], p[4]) {
			return uint8(p[5])
		}
	}
	
	return 255
}

func judge(x, y, cx, cy, a, b, deg float64) bool {
	t := deg * math.Pi / 180
	ct := math.Cos(t)
	st := math.Sin(t)
	x2 := (x - cx)
	y2 := (y - cy)
	x3 := (ct * x2 - st * y2) / a
	y3 := (st * x2 + ct * y2) / b
    return x3 * x3 + y3 * y3 < 1
}

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

download

@nreynis
Copy link

nreynis commented Mar 7, 2023

package main

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

func Pic(dx, dy int) [][]uint8 {
	bitmap := [][]uint8{}

	for i := 0; i < dy; i++ {
		line := make([]uint8, dx)
		for j := range line {
			line[j] = uint8(i * j % (j + 3))
		}
		bitmap = append(bitmap, line)
	}
	return bitmap
}

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

image

@yongkangc
Copy link

package main

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

func Pic(dx, dy int) [][]uint8 {
	pictureArray := make([][]uint8, dy)
	for i := range pictureArray {
		pictureArray[i] = make([]uint8, dx)
		for j := range pictureArray[i] {
			pictureArray[i][j] = uint8((i * j / 4) + (i ^ j + 1))
		}
	}
	return pictureArray
}

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

image

@yongkangc
Copy link

package main

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

func Pic(dx, dy int) [][]uint8 {
	s := make([][]uint8, dy)
	for y := range s {
		s[y] = make([]uint8, dx)
		for x := range s[y] {
			s[y][x] = gopher(x, y)
		}
	}

	return s
}

func gopher(x, y int) uint8 {
    xx := float64(x)
	yy := float64(y)
	
	params := [][6]float64 {
		{180, 50, 3, 3, 0, 255},
		{175, 50, 10, 10, 0, 0},
		{105, 80, 3, 3, 0, 255},
		{100, 80, 10, 10, 0, 0},
		{155, 55, 30, 30, 0, 255},
		{155, 55, 32, 32, 0, 0},
		{80, 85, 30, 30, 0, 255},
		{80, 85, 32, 32, 0, 0},
		{130, 90, 7, 1, 24, 255},
		{125, 95, 13, 8, 24, 0},
		{133, 128, 12, 5, 114, 255},
		{133, 128, 13, 6, 114, 0},
		{143, 124, 12, 5, 116, 255},
		{143, 124, 13, 6, 116, 0},
		{130, 108, 22, 12, 23, 160},
		{130, 108, 24, 14, 23, 0},
		{183, 178, 14, 8, -20, 255},
		{183, 178, 15, 9, -20, 0},
		{131, 108, 96, 100, 0, 220},
		{151, 158, 90, 100, 0, 220},
		{151, 188, 90, 100, 0, 220},
		{243, 148, 14, 8, -20, 255},
		{243, 148, 15, 9, -20, 0},
		{205, 36, 2, 2, 0, 255},
		{203, 42, 10, 10, 0, 0},
		{206, 44, 21, 21, 0, 255},
		{206, 44, 23, 23, 0, 0},
		{33, 102, 10, 10, 0, 0},
		{36, 104, 21, 21, 0, 255},
		{36, 104, 23, 23, 0, 0},
	}
    
	for _, p := range params {
		if judge(xx, yy, p[0], p[1], p[2], p[3], p[4]) {
			return uint8(p[5])
		}
	}
	
	return 255
}

func judge(x, y, cx, cy, a, b, deg float64) bool {
	t := deg * math.Pi / 180
	ct := math.Cos(t)
	st := math.Sin(t)
	x2 := (x - cx)
	y2 := (y - cy)
	x3 := (ct * x2 - st * y2) / a
	y3 := (st * x2 + ct * y2) / b
    return x3 * x3 + y3 * y3 < 1
}

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

download

this is amazing!

@19valentin99
Copy link

package main

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

func Pic(dx, dy int) [][]uint8 {
img := make([][]uint8, dy)
for y := range img {
img[y] = make([]uint8, dx)
for x := range img[y] {
i := uint8(y)
j := uint8(x)

	    value := (i + j)
		
		if value < 125{
			img[i][j] = j * j
		} else{
			img[i][j] = i * i
		}
	}
}
return img

}

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

image

@Rivgad
Copy link

Rivgad commented May 13, 2023

Mandelbrot set

package main

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

const (
	maxIter    uint8      = 100
	brightness uint8      = 20
	min        complex128 = -2 + 1.4i
	max        complex128 = 0.8 - 1.4i
)

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

	for i := range picture {
		picture[i] = make([]uint8, dx)
	}

	rMin := real(min)
	iMin := imag(max)
	sX := (real(max) - real(min)) / float64(dx-1)
	sY := (imag(min) - imag(max)) / float64(dy-1)

	for y := 0; y < dy; y++ {
		for x := 0; x < dx; x++ {
			realPart := float64(x)*sX + rMin
			imagPart := float64(y)*sY + iMin

			picture[y][x] = Mandelbrot(complex(realPart, imagPart)) * brightness
		}
	}

	return picture
}

func Mandelbrot(c complex128) uint8 {
	z := c

	for it := uint8(0); it < maxIter; it++ {
		z = z*z + c

		if r, i := real(z), imag(z); r*r+i*i > 4 {
			return it
		}
	}

	return maxIter
}

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

mandelbrot

@Max95Cohen
Copy link

package main

import (
	"image"

	"github.com/Max95Cohen/gopher"
	"golang.org/x/tour/pic"
)

func Pic(dx, dy int) [][]uint8 {
	var a = make([][]uint8, dy)

	for i := 0; i < dy; i++ {
		a[i] = make([]uint8, dx)
	}

	for i := 0; i < dy; i++ {
		for j := 0; j < dx; j++ {
			a[i][j] = 255
		}
	}

	var p = &a

	return gopher.GetPixels(*p)
}

func show(f func(dx, dy int) [][]uint8) {
	const (
		dx = 256
		dy = 256
	)
	data := f(dx, dy)
	m := image.NewNRGBA(image.Rect(0, 0, dx, dy))
	for y := 0; y < dy; y++ {
		for x := 0; x < dx; x++ {
			v := data[y][x]
			i := y*m.Stride + x*4
			
			var r, g, b uint8
			
			if v == 255 {
				r = 255
				g = 0
				b = 255
			} else {
				r = 0
				g = 255
				b = 0
			}
			
			m.Pix[i] = r
			m.Pix[i+1] = g
			m.Pix[i+2] = b
			m.Pix[i+3] = 255
		}
	}
	pic.ShowImage(m)
}

func main() {
	show(Pic)
}

image

@bryceleue
Copy link

package main

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

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

		for j := range s[i] {
			s[i][j] = uint8((i + j) / 2 >> 8 + (i + j) / 2 % (i + 2) % (j + 2))
		}
	}
	return s
}

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

golangthangs

@mikurei
Copy link

mikurei commented Oct 12, 2023

Very suspicious pattern

package main

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

const (
	VAL_MAX uint8 = 255
	AMOG_DX uint8 = 10
	AMOG_DY uint8 = 10
)
// ඞ
var AMOGUS = [AMOG_DY][AMOG_DX]uint8{
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 2, 2, 2, 0, 0, 0},
	{0, 0, 0, 2, 2, 1, 1, 1, 0, 0},
	{0, 0, 2, 2, 2, 2, 2, 2, 0, 0},
	{0, 0, 2, 2, 2, 2, 2, 2, 0, 0},
	{0, 0, 0, 2, 2, 2, 2, 2, 0, 0},
	{0, 0, 0, 2, 0, 0, 0, 2, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}

func Shader(x, y int) uint8 {
	x_u := uint8(x) % AMOG_DX
	y_u := uint8(y) % AMOG_DY
	return AMOGUS[y_u][x_u]*(VAL_MAX>>1) + 1

}

func Pic(dx, dy int) [][]uint8 {
	buf := make([][]uint8, dy)
	for y := range buf {
		column := make([]uint8, dx)
		for x := range column {
			column[x] = Shader(x, y)
		}
		buf[y] = column
	}
	return buf
}

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

sus

@hello2333
Copy link

package main

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

func pointValue1(x, y int) uint8 {
	return uint8((x + y) / 2)
}

func pointValue2(x, y int) uint8 {
	return uint8(x * y)
}

func pointValue3(x, y int) uint8 {
	return uint8(x ^ y)
}

func Pic(dx, dy int) [][]uint8 {
	// initialize slice rows with dy length
	var matrix [][]uint8
	
	// initialize the i'th element of rows with dx length slice
	for i := 0; i < dy; i ++ {
		// var array_rows [dx]uint8
		var array_rows []uint8
		// update the value of each point
		for j := 0; j < dx; j ++ {
			// array_rows[j] = pointValue1(i, j)
			array_rows = append(array_rows, pointValue3(i, j))
		}
		// matrix = append(matrix, array_rows[0:])
		matrix = append(matrix, array_rows)
	}
	
	// fmt.Println(matrix)
	return matrix
}

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

go测试

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