This repository was archived by the owner on Mar 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.go
More file actions
90 lines (82 loc) · 1.83 KB
/
matrix.go
File metadata and controls
90 lines (82 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package types
import "fmt"
func CreateMat(hei, wid int, defaultv interface{}) (pt [][]interface{}) {
for i := 0; i < hei; i++ {
w := []interface{}{}
for j := 0; j < wid; j++ {
w = append(w, defaultv)
}
pt = append(pt, w)
}
return
}
func PrintMat(pt [][]interface{}) {
for i := 0; i < len(pt); i++ {
for j := 0; j < len(pt[i]); j++ {
fmt.Print(pt[i][j])
}
fmt.Println()
}
return
}
func CopyMat(mat, updater [][]interface{}, startx, starty int) {
for i := 0; i < len(updater); i++ {
for j := 0; j < len(updater[i]); j++ {
mat[startx+i][starty+j] = updater[i][j]
}
}
}
var ExpandMatCellReWriteFunc = make(map[string]func(cell string, size int, rst [][]interface{}))
func init() {
ExpandMatCellReWriteFunc["/"] = func(cell string, expand int, rst [][]interface{}) {
for i := 0; i < expand; i++ {
rst[i][expand-i-1] = "/"
}
}
ExpandMatCellReWriteFunc["\\"] = func(cell string, expand int, rst [][]interface{}) {
for i := 0; i < expand; i++ {
rst[i][i] = "\\"
}
}
}
func ExpandMatCell(cell interface{}, size int) (rst [][]interface{}) {
expand := size*2 - 1
rst = CreateMat(expand, expand, " ")
rst[size-1][size-1] = cell
c := fmt.Sprint(cell)
if f, have := ExpandMatCellReWriteFunc[c]; have {
f(c, expand, rst)
} else {
l, r := 0, 0
for i := 0; i < len(c)-1; i++ {
if l > r {
if size-1+r < expand {
r++
rst[size-1][size-1+r] = ""
}
} else {
if size-1-l > 0 {
l++
rst[size-1][size-1-l] = ""
}
}
}
}
return
}
func ExpandMat(mat [][]interface{}, size int) (rst [][]interface{}) {
w := len(mat)
var h = 0
if w > 0 {
h = len(mat[0])
}
expand := size*2 - 1
rst = CreateMat(w*expand, h*expand, " ")
for i := 0; i < w; i++ {
for j := 0; j < h; j++ {
extcell := ExpandMatCell(mat[i][j], size)
CopyMat(rst, extcell, i*expand, j*expand)
}
}
return
}