1 42 43 package org.jfree.data.contour; 44 45 import org.jfree.data.Range; 46 47 51 public class NonGridContourDataset extends DefaultContourDataset { 52 53 54 static final int DEFAULT_NUM_X = 50; 55 56 57 static final int DEFAULT_NUM_Y = 50; 58 59 60 static final int DEFAULT_POWER = 4; 61 62 65 public NonGridContourDataset() { 66 super(); 67 } 68 69 78 public NonGridContourDataset(String seriesName, 79 Object [] xData, Object [] yData, 80 Object [] zData) { 81 super(seriesName, xData, yData, zData); 82 buildGrid(DEFAULT_NUM_X, DEFAULT_NUM_Y, DEFAULT_POWER); 83 } 84 85 96 public NonGridContourDataset(String seriesName, 97 Object [] xData, Object [] yData, 98 Object [] zData, 99 int numX, int numY, int power) { 100 super(seriesName, xData, yData, zData); 101 buildGrid(numX, numY, power); 102 } 103 104 115 protected void buildGrid(int numX, int numY, int power) { 116 117 int numValues = numX * numY; 118 double[] xGrid = new double[numValues]; 119 double[] yGrid = new double [numValues]; 120 double[] zGrid = new double [numValues]; 121 122 double xMin = 1.e20; 124 for (int k = 0; k < this.xValues.length; k++) { 125 xMin = Math.min(xMin, this.xValues[k].doubleValue()); 126 } 127 128 double xMax = -1.e20; 129 for (int k = 0; k < this.xValues.length; k++) { 130 xMax = Math.max(xMax, this.xValues[k].doubleValue()); 131 } 132 133 double yMin = 1.e20; 134 for (int k = 0; k < this.yValues.length; k++) { 135 yMin = Math.min(yMin, this.yValues[k].doubleValue()); 136 } 137 138 double yMax = -1.e20; 139 for (int k = 0; k < this.yValues.length; k++) { 140 yMax = Math.max(yMax, this.yValues[k].doubleValue()); 141 } 142 143 Range xRange = new Range(xMin, xMax); 144 Range yRange = new Range(yMin, yMax); 145 146 xRange.getLength(); 147 yRange.getLength(); 148 149 double dxGrid = xRange.getLength() / (numX - 1); 151 double dyGrid = yRange.getLength() / (numY - 1); 152 153 double x = 0.0; 155 for (int i = 0; i < numX; i++) { 156 if (i == 0) { 157 x = xMin; 158 } 159 else { 160 x += dxGrid; 161 } 162 double y = 0.0; 163 for (int j = 0; j < numY; j++) { 164 int k = numY * i + j; 165 xGrid[k] = x; 166 if (j == 0) { 167 y = yMin; 168 } 169 else { 170 y += dyGrid; 171 } 172 yGrid[k] = y; 173 } 174 } 175 176 for (int kGrid = 0; kGrid < xGrid.length; kGrid++) { 178 double dTotal = 0.0; 179 zGrid[kGrid] = 0.0; 180 for (int k = 0; k < this.xValues.length; k++) { 181 double xPt = this.xValues[k].doubleValue(); 182 double yPt = this.yValues[k].doubleValue(); 183 double d = distance(xPt, yPt, xGrid[kGrid], yGrid[kGrid]); 184 if (power != 1) { 185 d = Math.pow(d, power); 186 } 187 d = Math.sqrt(d); 188 if (d > 0.0) { 189 d = 1.0 / d; 190 } 191 else { d = 1.e20; 194 } 195 if (this.zValues[k] != null) { 196 zGrid[kGrid] += this.zValues[k].doubleValue() * d; 198 } 199 dTotal += d; 200 } 201 zGrid[kGrid] = zGrid[kGrid] / dTotal; } 203 204 initialize( 206 formObjectArray(xGrid), formObjectArray(yGrid), 207 formObjectArray(zGrid) 208 ); 209 210 } 211 212 222 protected double distance(double xDataPt, 223 double yDataPt, 224 double xGrdPt, 225 double yGrdPt) { 226 double dx = xDataPt - xGrdPt; 227 double dy = yDataPt - yGrdPt; 228 return Math.sqrt(dx * dx + dy * dy); 229 } 230 231 } 232 | Popular Tags |