-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgetMinimumCrossingRows.js
More file actions
50 lines (46 loc) · 1.18 KB
/
getMinimumCrossingRows.js
File metadata and controls
50 lines (46 loc) · 1.18 KB
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
/**
* Callback for iterating rows
*
* @callback onRowCallback
* @param {number} row The y coordinate of the horizontal line
*/
/**
* Finds minimum number of horizontal lines crossing all rectangles
*
* @param {Object} thisArg The callback function invocation context
* @param {React[]} rectangles Collection of rectangles
* @param {onRowCallback} onItem Callback function to call for every found row
*/
export default function getMinimumCrossingRows(thisArg, rectangles, onItem) {
var from = null;
var to = null;
if (onItem != null) {
rectangles.sort(function (a, b) {
return a.y - b.y;
});
for (var index = 0; index < rectangles.length; index += 1) {
var rect = rectangles[index];
var bottom = rect.bottom();
if (from === null) {
from = rect.y;
to = bottom;
} else {
if (rect.y >= to) {
onItem.call(thisArg, from);
from = rect.y;
to = bottom;
} else {
if (rect.y > from) {
from = rect.y;
}
if (bottom < to) {
to = bottom;
}
}
}
}
if (from !== null) {
onItem.call(thisArg, from);
}
}
};