-
Notifications
You must be signed in to change notification settings - Fork 39
/
LayerSort.java
54 lines (47 loc) · 1.36 KB
/
LayerSort.java
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
/**
* LayerSort
* Interface to sorting layers
*
* @author Lee Byron
* @author Martin Wattenberg
*/
public abstract class LayerSort {
abstract String getName();
abstract Layer[] sort(Layer[] layers);
/**
* Creates a 'top' and 'bottom' collection.
* Iterating through the previously sorted list of layers, place each layer
* in whichever collection has less total mass, arriving at an evenly
* weighted graph. Reassemble such that the layers that appeared earliest
* end up in the 'center' of the graph.
*/
protected Layer[] orderToOutside(Layer[] layers) {
int j = 0;
int n = layers.length;
Layer[] newLayers = new Layer[n];
int topCount = 0;
float topSum = 0;
int[] topList = new int[n];
int botCount = 0;
float botSum = 0;
int[] botList = new int[n];
// partition to top or bottom containers
for (int i=0; i<n; i ) {
if (topSum < botSum) {
topList[topCount ] = i;
topSum = layers[i].sum;
} else {
botList[botCount ] = i;
botSum = layers[i].sum;
}
}
// reassemble into single array
for (int i = botCount - 1; i >= 0; i--) {
newLayers[j ] = layers[botList[i]];
}
for (int i = 0; i < topCount; i ) {
newLayers[j ] = layers[topList[i]];
}
return newLayers;
}
}