comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/CompactXYItems.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents 05bf8534a35a
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.jfreechart;
2
3 import java.io.Serializable;
4
5 /**
6 * This class is used to represent geometries (e.g. point, line, polygon). Each
7 * geometry is made up of multiple xy points stored in a single array. A line
8 * composed by start- and endpoint is stored in the following order in that
9 * array: [x1, y1, x2, y2].
10 *
11 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha Teichmann</a>
12 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
13 */
14 public class CompactXYItems
15 implements Serializable
16 {
17 /**
18 * Array storing the xy items.
19 */
20 protected double [] data;
21
22 /**
23 * Constructs a new CompactXYItems object with the given data.
24 *
25 * @param data An array with xy values.
26 */
27 public CompactXYItems(double [] data) {
28 this.data = data;
29 }
30
31 /**
32 * Retrieves the x coordinate of the point with the given index.
33 *
34 * @param index Index
35 * @return X coordinate.
36 */
37 public double getX(int index) {
38 return data[index << 1];
39 }
40
41 /**
42 * Retrieves the y coordinate of the point with the given index.
43 *
44 * @param index Index
45 * @return Y coordinate.
46 */
47 public double getY(int index) {
48 return data[(index << 1)+1];
49 }
50
51 /**
52 * Write the tupel of xy-values at a specific index into the given array.
53 *
54 * @param index Index used to specify the xy-value.
55 * @param xy the xy coordinate is written into this array with the following
56 * order: [x,y]
57 */
58 public void get(int index, double [] xy) {
59 xy[0] = data[index = (index << 1) + 1];
60 xy[1] = data[index + 1];
61 }
62
63 /**
64 *
65 * @return the data array.
66 */
67 public double [] getData() {
68 return data;
69 }
70
71 /**
72 *
73 * @param data
74 */
75 public void setData(double [] data) {
76 this.data = data;
77 }
78
79 /**
80 *
81 * @return the number of data points.
82 */
83 public int size() {
84 return data.length >> 1;
85 }
86
87 /**
88 * Retrieves the bounding box spaned by the coordinates in the data array.
89 *
90 * @param bbox
91 * @return the calculated bounding box.
92 */
93 public double [] calculateBoundingBox(double [] bbox) {
94 for (int i = 0; i < data.length;) {
95 double x = data[i++];
96 double y = data[i++];
97 if (x < bbox[0]) bbox[0] = x;
98 if (y < bbox[1]) bbox[1] = y;
99 if (x > bbox[2]) bbox[2] = x;
100 if (y > bbox[3]) bbox[3] = y;
101 }
102 return bbox;
103 }
104
105 /**
106 *
107 * @return the coordinates as string.
108 */
109 @Override
110 public String toString() {
111 StringBuilder sb = new StringBuilder();
112 for (int i = 0; i < data.length;) {
113 if (i > 0) sb.append("; ");
114 sb.append('(');
115 sb.append(data[i++]);
116 sb.append(", ");
117 sb.append(data[i++]);
118 sb.append(')');
119 }
120 return sb.toString();
121 }
122
123
124 /**
125 *
126 * @return the lowest x value.
127 */
128 public double getMinX() {
129 double lower = Double.POSITIVE_INFINITY;
130
131 for (int i = 0; i < data.length; i += 2) {
132 double x = data[i];
133
134 if (!Double.isNaN(x)) {
135 lower = Math.min(lower, x);
136 }
137 }
138
139 return lower;
140 }
141
142
143 /**
144 *
145 * @return the highest x value.
146 */
147 public double getMaxX() {
148 double upper = Double.NEGATIVE_INFINITY;
149
150 for (int i = 0; i < data.length; i += 2) {
151 double x = data[i];
152
153 if (!Double.isNaN(x)) {
154 upper = Math.max(upper, x);
155 }
156 }
157
158 return upper;
159 }
160
161
162 /**
163 *
164 * @return the lowest y value.
165 */
166 public double getMinY() {
167 double lower = Double.POSITIVE_INFINITY;
168
169 for (int i = 1; i < data.length; i += 2) {
170 double y = data[i];
171
172 if (!Double.isNaN(y)) {
173 lower = Math.min(lower, y);
174 }
175 }
176
177 return lower;
178 }
179
180
181 /**
182 *
183 * @return the highest y value.
184 */
185 public double getMaxY() {
186 double upper = Double.NEGATIVE_INFINITY;
187
188 for (int i = 1; i < data.length; i += 2) {
189 double y = data[i];
190
191 if (!Double.isNaN(y)) {
192 upper = Math.max(upper, y);
193 }
194 }
195
196 return upper;
197 }
198 }
199 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org