forked from stathissideris/ditaa
-
Notifications
You must be signed in to change notification settings - Fork 5
/
abstractiongrid.go
77 lines (70 loc) · 1.61 KB
/
abstractiongrid.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
package main
type AbstractionGrid struct {
Rows [][]rune
}
func EmptyAbstractionGrid(w, h int) *AbstractionGrid {
return &AbstractionGrid{Rows: BlankRows(3*w, 3*h)}
}
func NewAbstractionGrid(t *TextGrid, cells *CellSet) *AbstractionGrid {
g := EmptyAbstractionGrid(3*t.Width(), 3*t.Height())
for c := range cells.Set {
if t.IsBlank(c) {
continue
}
for _, x := range abstractionChecks {
if x.check(t, c) {
g.Set(c, x.result)
break
}
}
}
return g
}
func (g *AbstractionGrid) Set(c Cell, brush AbstractCell) {
x, y := 3*c.X, 3*c.Y
for dy := 0; dy < 3; dy++ {
for dx := 0; dx < 3; dx++ {
if brush.Get(dx, dy) {
g.Rows[y+dy][x+dx] = '*'
}
}
}
}
func (g *AbstractionGrid) Height() int {
return len(g.Rows) / 3
}
func (g *AbstractionGrid) Width() int {
if len(g.Rows) == 0 {
return 0
}
return len(g.Rows[0]) / 3
}
func (g *AbstractionGrid) GetAsTextGrid() *TextGrid {
t := NewTextGrid(g.Width(), g.Height())
for y := range g.Rows {
for x, ch := range g.Rows[y] {
if ch != ' ' {
t.Set(Cell{x / 3, y / 3}, '*')
}
}
}
return t
}
var abstractionChecks = []struct {
check func(*TextGrid, Cell) bool
result AbstractCell
}{
{(*TextGrid).IsCross, abCross},
{(*TextGrid).IsT, abT},
{(*TextGrid).IsK, abK},
{(*TextGrid).IsInverseT, abInvT},
{(*TextGrid).IsInverseK, abInvK},
{(*TextGrid).IsCorner1, abCorner1},
{(*TextGrid).IsCorner2, abCorner2},
{(*TextGrid).IsCorner3, abCorner3},
{(*TextGrid).IsCorner4, abCorner4},
{(*TextGrid).IsHorizontalLine, abHLine},
{(*TextGrid).IsVerticalLine, abVLine},
{(*TextGrid).IsCrossOnLine, abCross},
{(*TextGrid).IsStarOnLine, abStar},
}