-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlayout_primitives.go
72 lines (59 loc) · 1.97 KB
/
layout_primitives.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
package washeet
import (
"math"
)
// Generalized function for computing colStartXCoords/rowStartYCoords
// Reference is startCell (ie startColumn/startRow)
// "measure" here means col-width/row-height
func computeCellsCoordsRefStart(minCoord, maxCoord float64, startCell int64,
measureGetter cellMeasureGetter, defaultCellMeasure float64,
cellsCoordStorage []float64) (endCell int64, cellsCoords []float64) {
cellsCoords = cellsCoordStorage
cellStartCoord := minCoord
currCell := startCell
cellIdx := 0
for {
// We need the startX/startY of the "fully-out-of-screen" column/row too
if cellIdx >= len(cellsCoords) {
cellsCoords = append(cellsCoords, cellStartCoord)
} else {
cellsCoords[cellIdx] = cellStartCoord
}
if cellStartCoord > maxCoord {
break
}
cellStartCoord += math.Max(measureGetter(currCell), defaultCellMeasure)
currCell++
cellIdx++
}
endCell = currCell - 1
return
}
// Generalized function for computing colStartXCoords/rowStartYCoords
// Reference is endCell (ie startColumn/startRow)
// "measure" here means col-width/row-height
func computeCellsCoordsRefEnd(minCoord, maxCoord float64, endCell int64,
measureGetter cellMeasureGetter, defaultCellMeasure float64,
cellsCoordStorage []float64) (startCell int64, finalEndCell int64, cellsCoords []float64) {
// Two pass algorithm - first find startCell then use computeCellsCoordsRefStart
// to compute cellsCoords as usual.
currCell := endCell
cellStartCoord := maxCoord - math.Max(measureGetter(currCell), defaultCellMeasure)
if cellStartCoord < minCoord {
// There is space only for one cell
startCell = endCell
} else {
for {
currCell--
cellStartCoord -= math.Max(measureGetter(currCell), defaultCellMeasure)
if cellStartCoord < minCoord {
// No space for currCell
startCell = currCell + 1
break
}
}
}
finalEndCell, cellsCoords = computeCellsCoordsRefStart(minCoord, maxCoord,
startCell, measureGetter, defaultCellMeasure, cellsCoordStorage)
return
}