Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "golang.org/x/tour/pic"
- )
- // Used in func duplicate
- var P = Pascal(11)
- // The x, y of the pic
- const SIZE = 256
- // Add a 0 at the beginning(right shift) or at the end (left shift)
- func Shift(x []uint8, left_or_right int) []uint8 {
- if left_or_right == -1 {
- return append(x, 0)
- }
- L := []uint8{0}
- n := len(x)
- for i := 0; i < n; i++ {
- L = append(L, x[i])
- }
- return L
- }
- // Add the two shifts to form pascal triangle
- /*
- 1
- (1) 1 1
- (2) 1 2 1
- 1 3 3 1
- ............................. so on ...
- [1 0] +
- [0 1] =
- [1 1] (1) --> Shift --> [1 1 0] +
- [0 1 1]
- [1 2 1] (2)
- ............................. so on ...
- */
- func Add(x, y []uint8) []uint8 {
- L := make([]uint8, len(x))
- for i := range L {
- L[i] = x[i] + y[i]
- }
- return L
- }
- func Pascal(x int) []uint8 {
- if x == 1 {
- return []uint8{1}
- }
- L := make([]uint8, x)
- L = Pascal(x - 1)
- return Add(Shift(L, -1), Shift(L, 1))
- }
- // Take the P = Pascal(11) and copy it on each
- // row as many times as it can be copied
- func duplicate(x []uint8, d int) []uint8 {
- if d < 11 {
- for i := SIZE - d; i < SIZE; i++ {
- x[i] = P[(i+d)-SIZE]
- }
- return x
- }
- for i := SIZE - d; i < SIZE-d+11; i++ {
- x[i] = P[(i+d)-SIZE]
- }
- return duplicate(x, d-11)
- }
- func Pic(dx, dy int) [][]uint8 {
- rows := make([][]uint8, dy)
- // Init of each row
- for i := range rows {
- rows[i] = make([]uint8, dx)
- }
- // Fill the rows
- for i := range rows {
- rows[i] = duplicate(rows[i], cap(rows[i]))
- }
- return rows
- }
- func main() {
- pic.Show(Pic)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement