src/org/math/plot/plots/HistogramPlot3D.java

00001 package org.math.plot.plots;
00002 
00003 import java.awt.*;
00004 
00005 import org.math.plot.*;
00006 import org.math.plot.render.*;
00007 
00008 public class HistogramPlot3D extends Plot {
00009 
00010         double[][] topNW;
00011 
00012         double[][] topNE;
00013 
00014         double[][] topSW;
00015 
00016         double[][] topSE;
00017 
00018         double[][] bottomNW;
00019 
00020         double[][] bottomNE;
00021 
00022         double[][] bottomSW;
00023 
00024         double[][] bottomSE;
00025 
00026         double[][] widths;
00027 
00028         double[] width_constant = { -1, -1 };
00029 
00030         double[][] XY;
00031 
00032         boolean fill_shape = true;
00033 
00034         public HistogramPlot3D(String n, Color c, double[][] _XY, double[][] w) {
00035                 super(n, c);
00036                 XY = _XY;
00037                 widths = w;
00038 
00039                 build();
00040         }
00041 
00042         public HistogramPlot3D(String n, Color c, double[][] _XY, double wX, double wY) {
00043                 super(n, c);
00044                 XY = _XY;
00045                 width_constant = new double[] { wX, wY };
00046 
00047                 build();
00048         }
00049 
00050         public HistogramPlot3D(String n, Color c, double[][] _XY, double[] w) {
00051                 super(n, c);
00052                 XY = _XY;
00053                 width_constant = w;
00054 
00055                 build();
00056         }
00057 
00058         private void build() {
00059                 if (width_constant[0] > 0) {
00060                         topNW = new double[XY.length][];
00061                         topNE = new double[XY.length][];
00062                         topSW = new double[XY.length][];
00063                         topSE = new double[XY.length][];
00064                         bottomNW = new double[XY.length][];
00065                         bottomNE = new double[XY.length][];
00066                         bottomSW = new double[XY.length][];
00067                         bottomSE = new double[XY.length][];
00068                         for (int i = 0; i < XY.length; i++) {
00069                                 topNW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, XY[i][2] };
00070                                 topNE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, XY[i][2] };
00071                                 topSW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, XY[i][2] };
00072                                 topSE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, XY[i][2] };
00073                                 bottomNW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, 0 };
00074                                 bottomNE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] + width_constant[1] / 2, 0 };
00075                                 bottomSW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, 0 };
00076                                 bottomSE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] - width_constant[1] / 2, 0 };
00077                         }
00078                 } else {
00079                         topNW = new double[XY.length][];
00080                         topNE = new double[XY.length][];
00081                         topSW = new double[XY.length][];
00082                         topSE = new double[XY.length][];
00083                         bottomNW = new double[XY.length][];
00084                         bottomNE = new double[XY.length][];
00085                         bottomSW = new double[XY.length][];
00086                         bottomSE = new double[XY.length][];
00087                         for (int i = 0; i < XY.length; i++) {
00088                                 topNW[i] = new double[] { XY[i][0] - widths[i][0] / 2, XY[i][1] + widths[i][1] / 2, XY[i][2] };
00089                                 topNE[i] = new double[] { XY[i][0] + widths[i][0] / 2, XY[i][1] + widths[i][1] / 2, XY[i][2] };
00090                                 topSW[i] = new double[] { XY[i][0] - widths[i][0] / 2, XY[i][1] - widths[i][1] / 2, XY[i][2] };
00091                                 topSE[i] = new double[] { XY[i][0] + widths[i][0] / 2, XY[i][1] - widths[i][1] / 2, XY[i][2] };
00092                                 bottomNW[i] = new double[] { XY[i][0] - widths[i][0] / 2, XY[i][1] + widths[i][1] / 2, 0 };
00093                                 bottomNE[i] = new double[] { XY[i][0] + widths[i][0] / 2, XY[i][1] + widths[i][1] / 2, 0 };
00094                                 bottomSW[i] = new double[] { XY[i][0] - widths[i][0] / 2, XY[i][1] - widths[i][1] / 2, 0 };
00095                                 bottomSE[i] = new double[] { XY[i][0] + widths[i][0] / 2, XY[i][1] - widths[i][1] / 2, 0 };
00096                         }
00097                 }
00098         }
00099 
00100         public void plot(AbstractDrawer draw, Color c) {
00101                 if (!visible)
00102                         return;
00103 
00104                 draw.canvas.includeInBounds(bottomSW[0]);
00105                 draw.canvas.includeInBounds(topNE[XY.length - 1]);
00106 
00107                 draw.setColor(c);
00108                 draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
00109                 for (int i = 0; i < XY.length; i++) {
00110                         if (topNW[i][2]!=bottomNW[i][2]) {
00111                         draw.drawLine(topNW[i], topNE[i]);
00112                         draw.drawLine(topNE[i], topSE[i]);
00113                         draw.drawLine(topSE[i], topSW[i]);
00114                         draw.drawLine(topSW[i], topNW[i]);
00115 
00116                         draw.drawLine(bottomNW[i], bottomNE[i]);
00117                         draw.drawLine(bottomNE[i], bottomSE[i]);
00118                         draw.drawLine(bottomSE[i], bottomSW[i]);
00119                         draw.drawLine(bottomSW[i], bottomNW[i]);
00120 
00121                         draw.drawLine(bottomNW[i], topNW[i]);
00122                         draw.drawLine(bottomNE[i], topNE[i]);
00123                         draw.drawLine(bottomSE[i], topSE[i]);
00124                         draw.drawLine(bottomSW[i], topSW[i]);
00125 
00126                         if (fill_shape) {
00127                                 draw.fillPolygon(0.2f,topNW[i], topNE[i], topSE[i], topSW[i]);
00128                                 //draw.fillPolygon(bottomNW[i], bottomNE[i], bottomSE[i], bottomSW[i]);
00129                                 /*draw.fillPolygon(topNW[i], topNE[i], bottomNE[i], bottomNW[i]);
00130                                 draw.fillPolygon(topSW[i], topSE[i], bottomSE[i], bottomSW[i]);
00131                                 draw.fillPolygon(topNE[i], topSE[i], bottomSE[i], bottomNE[i]);
00132                                 draw.fillPolygon(topNW[i], topSW[i], bottomSW[i], bottomNW[i]);*/
00133                         }
00134                         }
00135                 }
00136         }
00137 
00138         @Override
00139         public void setData(double[][] d) {
00140                 XY = d;
00141         }
00142 
00143         @Override
00144         public double[][] getData() {
00145                 return XY;
00146         }
00147 
00148         public void setDataWidth(double[][] w) {
00149                 widths = w;
00150         }
00151 
00152         public void setDataWidth(double... w) {
00153                 width_constant = w;
00154                 build();
00155         }
00156 
00157         public double[][] getDataWidth() {
00158                 if (width_constant[0] > 0) {
00159                         widths = new double[XY.length][2];
00160                         for (int i = 0; i < widths.length; i++) {
00161                                 widths[i][0] = width_constant[0];
00162                                 widths[i][1] = width_constant[1];
00163                         }
00164                 }
00165                 return widths;
00166         }
00167 
00168         public void setData(double[][] d, double[][] w) {
00169                 XY = d;
00170                 widths = w;
00171         }
00172 
00173         public void setData(double[][] d, double... w) {
00174                 XY = d;
00175                 setDataWidth(w);
00176         }
00177 
00178         public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
00179                 for (int i = 0; i < XY.length; i++) {
00180                         int[] screenCoord = draw.project(XY[i]);
00181 
00182                         if ((screenCoord[0] + note_precision > screenCoordTest[0]) && (screenCoord[0] - note_precision < screenCoordTest[0])
00183                                         && (screenCoord[1] + note_precision > screenCoordTest[1]) && (screenCoord[1] - note_precision < screenCoordTest[1]))
00184                                 return XY[i];
00185                 }
00186                 return null;
00187         }
00188 
00189         public static void main(String[] args) {
00190                 double[][] XY = new double[500][2];
00191                 for (int i = 0; i < XY.length; i++) {
00192                         XY[i][0] = Math.random()+Math.random();
00193                         XY[i][1] = Math.random()+Math.random();
00194                 }
00195                 Plot3DPanel p = new Plot3DPanel("SOUTH");
00196                 p.addHistogramPlot("test", XY, 4, 6);
00197                 new FrameView(p);
00198         }
00199 
00200 }

Generated on Wed Sep 5 21:44:02 2007 for jmathplot by  doxygen 1.5.1