sascha@422: package de.intevation.gnv.jfreechart; sascha@422: sascha@422: import java.io.Serializable; sascha@422: sascha@422: /** ingo@795: * This class is used to represent geometries (e.g. point, line, polygon). Each ingo@795: * geometry is made up of multiple xy points stored in a single array. A line ingo@795: * composed by start- and endpoint is stored in the following order in that ingo@795: * array: [x1, y1, x2, y2]. ingo@795: * sascha@780: * @author Sascha Teichmann sascha@780: * @author Ingo Weinzierl sascha@422: */ sascha@422: public class CompactXYItems sascha@422: implements Serializable sascha@422: { ingo@795: /** ingo@795: * Array storing the xy items. ingo@795: */ sascha@422: protected double [] data; sascha@422: ingo@795: /** ingo@795: * Constructs a new CompactXYItems object with the given data. ingo@795: * ingo@795: * @param data An array with xy values. ingo@795: */ sascha@422: public CompactXYItems(double [] data) { sascha@422: this.data = data; sascha@422: } sascha@422: ingo@795: /** ingo@795: * Retrieves the x coordinate of the point with the given index. ingo@795: * ingo@795: * @param index Index ingo@795: * @return X coordinate. ingo@795: */ sascha@422: public double getX(int index) { sascha@422: return data[index << 1]; sascha@422: } sascha@422: ingo@795: /** ingo@795: * Retrieves the y coordinate of the point with the given index. ingo@795: * ingo@795: * @param index Index ingo@795: * @return Y coordinate. ingo@795: */ sascha@422: public double getY(int index) { sascha@422: return data[(index << 1)+1]; sascha@422: } sascha@422: ingo@795: /** ingo@795: * Write the tupel of xy-values at a specific index into the given array. ingo@795: * ingo@795: * @param index Index used to specify the xy-value. ingo@795: * @param xy the xy coordinate is written into this array with the following ingo@795: * order: [x,y] ingo@795: */ sascha@422: public void get(int index, double [] xy) { sascha@422: xy[0] = data[index = (index << 1) + 1]; sascha@422: xy[1] = data[index + 1]; sascha@422: } sascha@422: ingo@795: /** ingo@795: * ingo@795: * @return the data array. ingo@795: */ sascha@422: public double [] getData() { sascha@422: return data; sascha@422: } sascha@422: ingo@795: /** ingo@795: * ingo@795: * @param data ingo@795: */ sascha@422: public void setData(double [] data) { sascha@422: this.data = data; sascha@422: } sascha@422: ingo@795: /** ingo@795: * ingo@815: * @return the number of data points. ingo@795: */ sascha@422: public int size() { sascha@422: return data.length >> 1; sascha@422: } sascha@422: ingo@795: /** ingo@795: * Retrieves the bounding box spaned by the coordinates in the data array. ingo@795: * ingo@795: * @param bbox ingo@815: * @return the calculated bounding box. ingo@795: */ sascha@422: public double [] calculateBoundingBox(double [] bbox) { sascha@422: for (int i = 0; i < data.length;) { sascha@422: double x = data[i++]; sascha@422: double y = data[i++]; sascha@422: if (x < bbox[0]) bbox[0] = x; sascha@422: if (y < bbox[1]) bbox[1] = y; sascha@422: if (x > bbox[2]) bbox[2] = x; sascha@422: if (y > bbox[3]) bbox[3] = y; sascha@422: } sascha@422: return bbox; sascha@422: } sascha@422: ingo@795: /** ingo@795: * ingo@795: * @return the coordinates as string. ingo@795: */ ingo@795: @Override sascha@422: public String toString() { sascha@422: StringBuilder sb = new StringBuilder(); sascha@422: for (int i = 0; i < data.length;) { sascha@422: if (i > 0) sb.append("; "); sascha@422: sb.append('('); sascha@422: sb.append(data[i++]); sascha@422: sb.append(", "); sascha@422: sb.append(data[i++]); sascha@422: sb.append(')'); sascha@422: } sascha@422: return sb.toString(); sascha@422: } sascha@422: sascha@422: ingo@795: /** ingo@795: * ingo@795: * @return the lowest x value. ingo@795: */ sascha@422: public double getMinX() { sascha@422: double lower = Double.POSITIVE_INFINITY; sascha@422: sascha@422: for (int i = 0; i < data.length; i += 2) { sascha@422: double x = data[i]; sascha@422: sascha@422: if (!Double.isNaN(x)) { sascha@422: lower = Math.min(lower, x); sascha@422: } sascha@422: } sascha@422: sascha@422: return lower; sascha@422: } sascha@422: sascha@422: ingo@795: /** ingo@795: * ingo@795: * @return the highest x value. ingo@795: */ sascha@422: public double getMaxX() { sascha@422: double upper = Double.NEGATIVE_INFINITY; sascha@422: sascha@422: for (int i = 0; i < data.length; i += 2) { sascha@422: double x = data[i]; sascha@422: sascha@422: if (!Double.isNaN(x)) { sascha@422: upper = Math.max(upper, x); sascha@422: } sascha@422: } sascha@422: sascha@422: return upper; sascha@422: } sascha@422: sascha@422: ingo@795: /** ingo@795: * ingo@795: * @return the lowest y value. ingo@795: */ sascha@422: public double getMinY() { sascha@422: double lower = Double.POSITIVE_INFINITY; sascha@422: sascha@422: for (int i = 1; i < data.length; i += 2) { sascha@422: double y = data[i]; sascha@422: sascha@422: if (!Double.isNaN(y)) { sascha@422: lower = Math.min(lower, y); sascha@422: } sascha@422: } sascha@422: sascha@422: return lower; sascha@422: } sascha@422: sascha@422: ingo@795: /** ingo@795: * ingo@795: * @return the highest y value. ingo@795: */ sascha@422: public double getMaxY() { sascha@422: double upper = Double.NEGATIVE_INFINITY; sascha@422: sascha@422: for (int i = 1; i < data.length; i += 2) { sascha@422: double y = data[i]; sascha@422: sascha@422: if (!Double.isNaN(y)) { sascha@422: upper = Math.max(upper, y); sascha@422: } sascha@422: } sascha@422: sascha@422: return upper; sascha@422: } sascha@422: } sascha@836: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :