comparison gnv-artifacts/src/main/java/de/intevation/gnv/jfreechart/CompactXYItems.java @ 1119:7c4f81f74c47

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

http://dive4elements.wald.intevation.org