1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.hicc;
19
20 import java.util.TreeMap;
21 import java.util.Map.Entry;
22
23 public class AreaCalculator {
24 public static TreeMap<String, Double> getAreas(
25 TreeMap<String, TreeMap<String, Double>> dataSet)
26 {
27 TreeMap<String, Double> areas = new TreeMap<String, Double>();
28 for (Entry<String, TreeMap<String, Double>> entry : dataSet.entrySet()) {
29 String key = entry.getKey();
30 Double area = getArea(entry.getValue());
31 areas.put(key, area);
32 }
33 return areas;
34 }
35
36 public static Double getArea(TreeMap<String, Double> data) {
37 double area = 0;
38 boolean first = true;
39 double x0, x1, y0, y1;
40 x0 = x1 = y0 = y1 = 0;
41 for (Entry<String, Double> entry : data.entrySet()) {
42 double x = Double.parseDouble(entry.getKey());
43 double y = entry.getValue();
44 if (first) {
45 x0 = x;
46 y0 = y;
47 first = false;
48 } else {
49 x1 = x;
50 y1 = y;
51 area += getArea(x0, y0, x1, y1);
52 x0 = x1;
53 y0 = y1;
54 }
55 }
56 return area;
57 }
58
59 public static Double getArea(double x0, double y0, double x1, double y1) {
60 return (x1 - x0) * (y0 + y1) / 2;
61 }
62 }