forked from mum4k/termdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributes.go
285 lines (238 loc) · 8.17 KB
/
attributes.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2019 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sixteen
// attributes.go calculates attributes needed when determining placement of
// segments.
import (
"fmt"
"image"
"math"
"github.com/mum4k/termdash/internal/numbers"
"github.com/mum4k/termdash/internal/segdisp"
"github.com/mum4k/termdash/internal/segdisp/segment"
)
// hvSegType maps horizontal and vertical segments to their type.
var hvSegType = map[Segment]segment.Type{
A1: segment.Horizontal,
A2: segment.Horizontal,
B: segment.Vertical,
C: segment.Vertical,
D1: segment.Horizontal,
D2: segment.Horizontal,
E: segment.Vertical,
F: segment.Vertical,
G1: segment.Horizontal,
G2: segment.Horizontal,
J: segment.Vertical,
M: segment.Vertical,
}
// diaSegType maps diagonal segments to their type.
var diaSegType = map[Segment]segment.DiagonalType{
H: segment.LeftToRight,
K: segment.RightToLeft,
N: segment.RightToLeft,
L: segment.LeftToRight,
}
// Attributes contains attributes needed to draw the segment display.
// Refer to doc/segment_placement.svg for a visual aid and explanation of the
// usage of the square roots.
type Attributes struct {
// segSize is the width of a vertical or height of a horizontal segment.
segSize int
// diaGap is the shortest distance between slopes on two neighboring
// perpendicular segments.
diaGap float64
// segPeakDist is the distance between the peak of the slope on a segment
// and the point where the slope ends.
segPeakDist float64
// diaLeg is the leg of a square whose hypotenuse is the diaGap.
diaLeg float64
// peakToPeak is a horizontal or vertical distance between peaks of two
// segments.
peakToPeak int
// shortLen is length of the shorter segment, e.g. D1.
shortLen int
// longLen is length of the longer segment, e.g. F.
longLen int
// horizLeftX is the X coordinate where the area of the segment horizontally
// on the left starts, i.e. X coordinate of F and E.
horizLeftX int
// horizMidX is the X coordinate where the area of the segment horizontally in
// the middle starts, i.e. X coordinate of J and M.
horizMidX int
// horizRightX is the X coordinate where the area of the segment horizontally
// on the right starts, i.e. X coordinate of B and C.
horizRightX int
// vertCenY is the Y coordinate where the area of the segment vertically
// in the center starts, i.e. Y coordinate of G1 and G2.
vertCenY int
// VertBotY is the Y coordinate where the area of the segment vertically
// at the bottom starts, i.e. Y coordinate of D1 and D2.
VertBotY int
}
// NewAttributes calculates attributes needed to place the segments for the
// provided pixel area.
func NewAttributes(bcAr image.Rectangle) *Attributes {
segSize := segdisp.SegmentSize(bcAr)
// diaPerc is the size of the diaGap in percentage of the segment's size.
const diaPerc = 40
// Ensure there is at least one pixel diagonally between segments so they
// don't visually blend.
_, dg := numbers.MinMaxInts([]int{
int(float64(segSize) * diaPerc / 100),
1,
})
diaGap := float64(dg)
segLeg := float64(segSize) / math.Sqrt2
segPeakDist := segLeg / math.Sqrt2
diaLeg := diaGap / math.Sqrt2
peakToPeak := diaLeg * 2
if segSize == 2 {
// Display that has segment size of two looks more balanced with peak
// distance of two.
peakToPeak = 2
}
if peakToPeak > 3 && int(peakToPeak)%2 == 0 {
// Prefer odd distances to create centered look.
peakToPeak++
}
twoSegHypo := 2*segLeg + diaGap
twoSegLeg := twoSegHypo / math.Sqrt2
edgeSegGap := twoSegLeg - segPeakDist
spaces := int(math.Round(2*edgeSegGap + peakToPeak))
shortLen := (bcAr.Dx()-spaces)/2 - 1
longLen := (bcAr.Dy()-spaces)/2 - 1
ptp := int(math.Round(peakToPeak))
horizLeftX := int(math.Round(edgeSegGap))
// Refer to doc/segment_placement.svg.
// Diagram labeled "A mid point".
offset := int(math.Round(diaLeg - segPeakDist))
horizMidX := horizLeftX + shortLen + offset
horizRightX := horizLeftX + shortLen + ptp + shortLen + offset
vertCenY := horizLeftX + longLen + offset
vertBotY := horizLeftX + longLen + ptp + longLen + offset
return &Attributes{
segSize: segSize,
diaGap: diaGap,
segPeakDist: segPeakDist,
diaLeg: diaLeg,
peakToPeak: ptp,
shortLen: shortLen,
longLen: longLen,
horizLeftX: horizLeftX,
horizMidX: horizMidX,
horizRightX: horizRightX,
vertCenY: vertCenY,
VertBotY: vertBotY,
}
}
// hvSegArea returns the area for the specified horizontal or vertical segment.
func (a *Attributes) hvSegArea(s Segment) image.Rectangle {
var (
start image.Point
length int
)
switch s {
case A1:
start = image.Point{a.horizLeftX, 0}
length = a.shortLen
case A2:
a1 := a.hvSegArea(A1)
start = image.Point{a1.Max.X + a.peakToPeak, 0}
length = a.shortLen
case F:
start = image.Point{0, a.horizLeftX}
length = a.longLen
case J:
start = image.Point{a.horizMidX, a.horizLeftX}
length = a.longLen
case B:
start = image.Point{a.horizRightX, a.horizLeftX}
length = a.longLen
case G1:
start = image.Point{a.horizLeftX, a.vertCenY}
length = a.shortLen
case G2:
g1 := a.hvSegArea(G1)
start = image.Point{g1.Max.X + a.peakToPeak, a.vertCenY}
length = a.shortLen
case E:
f := a.hvSegArea(F)
start = image.Point{0, f.Max.Y + a.peakToPeak}
length = a.longLen
case M:
j := a.hvSegArea(J)
start = image.Point{a.horizMidX, j.Max.Y + a.peakToPeak}
length = a.longLen
case C:
b := a.hvSegArea(B)
start = image.Point{a.horizRightX, b.Max.Y + a.peakToPeak}
length = a.longLen
case D1:
start = image.Point{a.horizLeftX, a.VertBotY}
length = a.shortLen
case D2:
d1 := a.hvSegArea(D1)
start = image.Point{d1.Max.X + a.peakToPeak, a.VertBotY}
length = a.shortLen
default:
panic(fmt.Sprintf("cannot determine area for unknown horizontal or vertical segment %v(%d)", s, s))
}
return a.hvArFromStart(start, s, length)
}
// hvArFromStart given start coordinates of a segment, its length and its type,
// determines its area.
func (a *Attributes) hvArFromStart(start image.Point, s Segment, length int) image.Rectangle {
st := hvSegType[s]
switch st {
case segment.Horizontal:
return image.Rect(start.X, start.Y, start.X+length, start.Y+a.segSize)
case segment.Vertical:
return image.Rect(start.X, start.Y, start.X+a.segSize, start.Y+length)
default:
panic(fmt.Sprintf("cannot create area for segment of unknown type %v(%d)", st, st))
}
}
// diaSegArea returns the area for the specified diagonal segment.
func (a *Attributes) diaSegArea(s Segment) image.Rectangle {
switch s {
case H:
return a.diaBetween(A1, F, J, G1)
case K:
return a.diaBetween(A2, B, J, G2)
case N:
return a.diaBetween(G1, M, E, D1)
case L:
return a.diaBetween(G2, M, C, D2)
default:
panic(fmt.Sprintf("cannot determine area for unknown diagonal segment %v(%d)", s, s))
}
}
// diaBetween given four segments (two horizontal and two vertical) returns the
// area between them for a diagonal segment.
func (a *Attributes) diaBetween(top, left, right, bottom Segment) image.Rectangle {
topAr := a.hvSegArea(top)
leftAr := a.hvSegArea(left)
rightAr := a.hvSegArea(right)
bottomAr := a.hvSegArea(bottom)
// hvToDiaGapPerc is the size of gap between horizontal or vertical segment
// and the diagonal segment between them in percentage of the diaGap.
const hvToDiaGapPerc = 30
hvToDiaGap := a.diaGap * hvToDiaGapPerc / 100
startX := int(math.Round(float64(topAr.Min.X) + a.segPeakDist - a.diaLeg + hvToDiaGap))
startY := int(math.Round(float64(leftAr.Min.Y) + a.segPeakDist - a.diaLeg + hvToDiaGap))
endX := int(math.Round(float64(bottomAr.Max.X) - a.segPeakDist + a.diaLeg - hvToDiaGap))
endY := int(math.Round(float64(rightAr.Max.Y) - a.segPeakDist + a.diaLeg - hvToDiaGap))
return image.Rect(startX, startY, endX, endY)
}