forked from pdevine/go-asciisprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
costume.go
121 lines (105 loc) · 1.95 KB
/
costume.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package sprite
import (
"strings"
tm "github.com/gdamore/tcell/termbox"
)
type Block struct {
Char rune
Fg tm.Attribute
Bg tm.Attribute
X int
Y int
}
type Costume struct {
Blocks []*Block
Width int
Height int
}
func NewCostume(t string, alpha rune) Costume {
c := Costume{}
c.ChangeCostume(t, alpha)
return c
}
func (c *Costume) ChangeCostume(t string, alpha rune) {
c.Blocks = []*Block{}
var width int
var height int
for y, line := range strings.Split(t, "\n") {
for x, ch := range line {
if ch != alpha {
b := &Block{
Char: ch,
X: x,
Y: y,
}
c.Blocks = append(c.Blocks, b)
width = max(x, width)
}
}
height = y
}
c.Width = width + 1
c.Height = height
}
func (c *Costume) TopEdge() int {
top := c.Blocks[0].Y
for _, b := range c.Blocks[1:] {
top = min(b.Y, top)
}
return top
}
func (c *Costume) LeftEdge() int {
left := c.Blocks[0].X
for _, b := range c.Blocks[1:] {
left = min(b.X, left)
}
return left
}
func (c *Costume) RightEdge() int {
right := c.Blocks[0].X
for _, b := range c.Blocks[1:] {
right = max(b.X, right)
}
return right
}
func (c *Costume) BottomEdge() int {
bottom := c.Blocks[0].Y
for _, b := range c.Blocks[1:] {
bottom = max(b.Y, bottom)
}
return bottom
}
func (c *Costume) LeftEdgeByRow() map[int]int {
t := make(map[int]int)
for _, b := range c.Blocks {
if _, ok := t[b.Y]; ok == false {
t[b.Y] = b.X
}
t[b.Y] = min(t[b.Y], b.X)
}
return t
}
func (c *Costume) RightEdgeByRow() map[int]int {
t := make(map[int]int)
for _, b := range c.Blocks {
t[b.Y] = max(t[b.Y], b.X)
}
return t
}
func (c *Costume) BottomEdgeByColumn() map[int]int {
t := make(map[int]int)
for _, b := range c.Blocks {
t[b.X] = max(t[b.X], b.Y)
}
return t
}
func (c *Costume) TopEdgeByColumn() map[int]int {
t := make(map[int]int)
for _, b := range c.Blocks {
if _, ok := t[b.X]; ok == false {
t[b.X] = b.Y
}
t[b.X] = min(t[b.X], b.Y)
}
return t
}