1 37 38 package org.jfree.data; 39 40 43 public class NonGridContourDataset extends DefaultContourDataset { 44 45 46 final int DEFAULT_NUM_X = 50; 47 48 49 final int DEFAULT_NUM_Y = 50; 50 51 52 final int DEFAULT_POWER = 4; 53 54 57 public NonGridContourDataset() { 58 super(); 59 } 60 61 70 public NonGridContourDataset(String seriesName, 71 Object [] xData, Object [] yData, Object [] zData) { 72 super(seriesName, xData, yData, zData); 73 buildGrid(DEFAULT_NUM_X, DEFAULT_NUM_Y, DEFAULT_POWER); 74 } 75 76 87 public NonGridContourDataset(String seriesName, Object [] xData, Object [] yData, Object [] zData, 88 int numX, int numY, int power) { 89 super(seriesName, xData, yData, zData); 90 buildGrid(numX, numY, power); 91 } 92 93 102 protected void buildGrid(int numX, int numY, int power) { 103 104 double[] xGrid = null; 105 double[] yGrid = null; 106 double[] zGrid = null; 107 108 int numValues = numX * numY; 109 xGrid = new double[numValues]; 110 yGrid = new double [numValues]; 111 zGrid = new double [numValues]; 112 113 double xMin = 1.e20; 115 for (int k = 0; k < xValues.length; k++) { 116 xMin = Math.min(xMin, xValues[k].doubleValue()); 117 } 118 119 double xMax = -1.e20; 120 for (int k = 0; k < xValues.length; k++) { 121 xMax = Math.max(xMax, xValues[k].doubleValue()); 122 } 123 124 double yMin = 1.e20; 125 for (int k = 0; k < yValues.length; k++) { 126 yMin = Math.min(yMin, yValues[k].doubleValue()); 127 } 128 129 double yMax = -1.e20; 130 for (int k = 0; k < yValues.length; k++) { 131 yMax = Math.max(yMax, yValues[k].doubleValue()); 132 } 133 134 Range xRange = new Range(xMin, xMax); 135 Range yRange = new Range(yMin, yMax); 136 137 xRange.getLength(); 138 yRange.getLength(); 139 140 double dxGrid = xRange.getLength() / (numX - 1); 142 double dyGrid = yRange.getLength() / (numY - 1); 143 144 double x = 0.0; 146 for (int i = 0; i < numX; i++) { 147 if (i == 0) { 148 x = xMin; 149 } 150 else { 151 x += dxGrid; 152 } 153 double y = 0.0; 154 for (int j = 0; j < numY; j++) { 155 int k = numY * i + j; 156 xGrid[k] = x; 157 if (j == 0) { 158 y = yMin; 159 } 160 else { 161 y += dyGrid; 162 } 163 yGrid[k] = y; 164 } 165 } 166 167 for (int kGrid = 0; kGrid < xGrid.length; kGrid++) { 169 double dTotal = 0.0; 170 zGrid[kGrid] = 0.0; 171 for (int k = 0; k < xValues.length; k++) { 172 double xPt = xValues[k].doubleValue(); 173 double yPt = yValues[k].doubleValue(); 174 double d = distance(xPt, yPt, xGrid[kGrid], yGrid[kGrid]); 175 if (power != 1) { 176 d = Math.pow(d, power); 177 } 178 d = Math.sqrt(d); 179 if (d > 0.0) { 180 d = 1.0 / d; 181 } 182 else { d = 1.e20; 184 } 185 if (zValues[k] != null) { 186 zGrid[kGrid] += zValues[k].doubleValue() * d; 188 } 189 dTotal += d; 190 } 191 zGrid[kGrid] = zGrid[kGrid] / dTotal; } 193 194 initialize(formObjectArray(xGrid), formObjectArray(yGrid), formObjectArray(zGrid)); 196 197 } 198 199 209 protected double distance(double xDataPt, double yDataPt, double xGrdPt, double yGrdPt) { 210 double dx = xDataPt - xGrdPt; 211 double dy = yDataPt - yGrdPt; 212 return Math.sqrt(dx * dx + dy * dy); 213 } 214 215 } 216 | Popular Tags |