-
Notifications
You must be signed in to change notification settings - Fork 11
/
costume.go
132 lines (117 loc) · 2.76 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
121
122
123
124
125
126
127
128
129
130
131
132
package sprite
import (
"strings"
tm "github.com/pdevine/go-asciisprite/termbox"
)
// Block holds a rune at (X,Y).
type Block struct {
Char rune
Fg tm.Attribute
Bg tm.Attribute
X int
Y int
}
// Costume contains a slice of Block pointers.
type Costume struct {
Blocks []*Block
Width int
Height int
}
// NewCostume provides a new Costume from a string.
func NewCostume(t string, alpha rune) Costume {
c := Costume{}
c.ChangeCostume(t, alpha)
return c
}
// ChangeCostume replaces a Costume with a new Costume from a string.
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
}
// TopEdge returns the Y value of the heighest Block in a Costume.
func (c *Costume) TopEdge() int {
top := c.Blocks[0].Y
for _, b := range c.Blocks[1:] {
top = min(b.Y, top)
}
return top
}
// LeftEdge returns the X value of the furthest left Block in a Costume.
func (c *Costume) LeftEdge() int {
left := c.Blocks[0].X
for _, b := range c.Blocks[1:] {
left = min(b.X, left)
}
return left
}
// RightEdge returns the X value of the furthest right Block in a Costume.
func (c *Costume) RightEdge() int {
right := c.Blocks[0].X
for _, b := range c.Blocks[1:] {
right = max(b.X, right)
}
return right
}
// BottomEdge returns the Y value of the lowest Block in a Costume.
func (c *Costume) BottomEdge() int {
bottom := c.Blocks[0].Y
for _, b := range c.Blocks[1:] {
bottom = max(b.Y, bottom)
}
return bottom
}
// LeftEdgeByRow returns a map of the left most X value in each row of a Costume.
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
}
// RightEdgeByRow returns a map of the right most X value in each row of a Costume.
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
}
// BottomEdgeByColumn returns a map of the lowest Y value in each column of a Costume.
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
}
// TopEdgeByColumn returns a map of the heighest Y value in each column of a Costume.
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
}