My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package org.lindenb.treemap;

import java.awt.geom.Rectangle2D;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* TreeMapModeler
* original java code from http://www.cs.umd.edu/hcil/treemap-history/Treemaps-Java-Algorithms.zip
* by
* - Martin Wattenberg, w(at)bewitched.com
* - Ben Bederson, bederson(at)cs.umd.edu
* University of Maryland, Human-Computer Interaction Lab
* http://www.cs.umd.edu/hcil
*/
public class TreeMapModeler
{
private static final Comparator<TreeMap> WEIGHT_COMPARE= new Comparator<TreeMap>()
{
public int compare(TreeMap o1, TreeMap o2)
{
double d1= o1.getWeight();
double d2= o2.getWeight();
return (d2==d1 ? 0 : (d1<d2?1:-1));//yes, inverse
};
};


public void layout(
List<? extends TreeMap> items,
Rectangle2D area
)
{
Collections.sort(items,WEIGHT_COMPARE);
layout(items,0,items.size(),area);
}


private double getWeight(final List<? extends TreeMap> items, int start, int end)
{
double sum=0;
while(start<end)
{
sum+=items.get(start++).getWeight();
}
return sum;
}

private void sliceLayout(
List<? extends TreeMap> items,
int start, int end,
Rectangle2D bounds)
{
end=Math.min(items.size(),end);

double total = getWeight(items,start, end);
double a=0;
boolean vertical=(bounds.getWidth()<bounds.getHeight() );
double pos= (vertical?bounds.getY():bounds.getX());

for (int i=start; i<end; i++)
{
Rectangle2D.Double r=new Rectangle2D.Double();
double b= items.get(i).getWeight()/total;
if (vertical)
{
r.x= bounds.getX();
r.width= bounds.getWidth();
r.y = pos;
double len = bounds.getHeight()*b;
r.height = len;
pos+=(len);
}
else
{
//r.x=(int)(bounds.x+bounds.width*a);
r.x = pos;
//r.width=(int)(bounds.width*b);
double len = bounds.getWidth()*b;
r.width = len;
r.y = bounds.getY();
r.height = bounds.getHeight();
pos+=(len);
}

items.get(i).setBounds(r);
a+=b;
}
}



private void layout(
List<? extends TreeMap> items,
int start,
int end,
Rectangle2D bounds
)
{
end= Math.max(end, items.size());
if (start>=end) return;

if (end-start<2)
{
sliceLayout(items,start,end,bounds);
return;
}

double x=bounds.getX(), y=bounds.getY(), w=bounds.getWidth(), h=bounds.getHeight();

double total=getWeight(items,start, end);
int mid=start;
double a= items.get(start).getWeight()/total;
double b=a;

if (w<h)
{
// height/width
while (mid<end)
{
double aspect=normAspect(h,w,a,b);
double q= items.get(mid).getWeight()/total;
if (normAspect(h,w,a,b+q)>aspect) break;
mid++;
b+=q;
}


sliceLayout(items,start,mid+1,new Rectangle2D.Double(x,y,w,(h*b)));
layout(items,mid+1,end,new Rectangle2D.Double(x,(y+h*b),w,(h*(1-b))));

}
else
{
// width/height
while (mid<end)
{
double aspect=normAspect(w,h,a,b);
double q= items.get(mid).getWeight()/total;
if (normAspect(w,h,a,b+q)>aspect) break;
mid++;
b+=q;
}


sliceLayout(items,start,mid+1,new Rectangle2D.Double(x,y,(w*b),h));
layout(items,mid+1,end,new Rectangle2D.Double((x+w*b),y,(w*(1-b)),h));


}

}

private static double aspect(double big, double small, double a, double b)
{
return (big*b)/(small*a/b);
}

private static double normAspect(double big, double small, double a, double b)
{
double x=aspect(big,small,a,b);
if (x<1) return 1/x;
return x;
}

}

Change log

r356 by plindenbaum on Aug 4, 2009   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r355 by plindenbaum on Jul 29, 2009   Diff
cont
All revisions of this file

File info

Size: 4494 bytes, 167 lines
Powered by Google Project Hosting