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

00001 /*
00002  * Created on 3 juin 2005 by richet
00003  */
00004 package org.math.plot.plots;
00005 
00006 import java.awt.*;
00007 
00008 import org.math.plot.*;
00009 import org.math.plot.render.*;
00010 
00011 public class GridPlot3D extends Plot {
00012 
00013         double[] X;
00014 
00015         double[] Y;
00016 
00017         double[][] Z;
00018 
00019         private double[][] XYZ_list;
00020 
00021         public boolean draw_lines = true;
00022 
00023         public boolean fill_shape = true;
00024 
00025         public GridPlot3D(String n, Color c, double[] _X, double[] _Y, double[][] _Z) {
00026                 super(n, c);
00027                 X = _X;
00028                 Y = _Y;
00029                 Z = _Z;
00030                 buildXYZ_list();
00031         }
00032 
00033         public void plot(AbstractDrawer draw, Color c) {
00034                 if (!visible)
00035                         return;
00036 
00037                 draw.setColor(c);
00038 
00039                 if (draw_lines) {
00040                         draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
00041                         for (int i = 0; i < X.length; i++)
00042                                 for (int j = 0; j < Y.length - 1; j++)
00043                                         draw.drawLine(new double[] { X[i], Y[j], Z[j][i] }, new double[] { X[i], Y[j + 1], Z[j + 1][i] });
00044 
00045                         for (int j = 0; j < Y.length; j++)
00046                                 for (int i = 0; i < X.length - 1; i++)
00047                                         draw.drawLine(new double[] { X[i], Y[j], Z[j][i] }, new double[] { X[i + 1], Y[j], Z[j][i + 1] });
00048                 } else {
00049                         draw.setDotType(AbstractDrawer.ROUND_DOT);
00050                         draw.setDotRadius(AbstractDrawer.DEFAULT_DOT_RADIUS);
00051                         for (int i = 0; i < X.length; i++)
00052                                 for (int j = 0; j < Y.length; j++)
00053                                         draw.drawDot(new double[] { X[i], Y[j], Z[j][i] });
00054                 }
00055 
00056                 if (fill_shape) {
00057                         for (int j = 0; j < Y.length - 1; j++)
00058                                 for (int i = 0; i < X.length - 1; i++)
00059                                         draw.fillPolygon(0.2f,new double[] { X[i], Y[j], Z[j][i] }, new double[] { X[i + 1], Y[j], Z[j][i + 1] }, new double[] { X[i + 1], Y[j + 1],
00060                                                         Z[j + 1][i + 1] }, new double[] { X[i], Y[j + 1], Z[j + 1][i] });
00061                 }
00062         }
00063 
00064         private void buildXYZ_list() {
00065                 XYZ_list = new double[X.length * Y.length][3];
00066                 for (int i = 0; i < X.length; i++) {
00067                         for (int j = 0; j < Y.length; j++) {
00068                                 XYZ_list[i + (j) * X.length][0] = X[i];
00069                                 XYZ_list[i + (j) * X.length][1] = Y[j];
00070                                 XYZ_list[i + (j) * X.length][2] = Z[j][i];
00071                         }
00072                 }
00073         }
00074 
00075         @Override
00076         public void setData(double[][] _Z) {
00077                 Z = _Z;
00078                 buildXYZ_list();
00079         }
00080 
00081         @Override
00082         public double[][] getData() {
00083                 return XYZ_list;
00084         }
00085 
00086         public void setDataZ(double[][] _Z) {
00087                 Z = _Z;
00088                 buildXYZ_list();
00089         }
00090 
00091         public double[][] getDataZ() {
00092                 return Z;
00093         }
00094 
00095         public void setDataX(double[] _X) {
00096                 X = _X;
00097                 buildXYZ_list();
00098         }
00099 
00100         public double[] getDataX() {
00101                 return X;
00102         }
00103 
00104         public void setDataY(double[] _Y) {
00105                 Y = _Y;
00106                 buildXYZ_list();
00107         }
00108 
00109         public double[] getDataY() {
00110                 return Y;
00111         }
00112 
00113         public void setDataXYZ(double[] _X, double[] _Y, double[][] _Z) {
00114                 X = _X;
00115                 Y = _Y;
00116                 Z = _Z;
00117                 buildXYZ_list();
00118         }
00119 
00120         public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
00121                 for (int i = 0; i < X.length; i++) {
00122                         for (int j = 0; j < Y.length; j++) {
00123                                 double[] XY = { X[i], Y[j], Z[j][i] };
00124                                 int[] screenCoord = draw.project(XY);
00125 
00126                                 if ((screenCoord[0] + note_precision > screenCoordTest[0]) && (screenCoord[0] - note_precision < screenCoordTest[0])
00127                                                 && (screenCoord[1] + note_precision > screenCoordTest[1]) && (screenCoord[1] - note_precision < screenCoordTest[1]))
00128                                         return XY;
00129                         }
00130                 }
00131                 return null;
00132         }
00133 
00134         public static void main(String[] args) {
00135 
00136                 int n = 14;
00137                 int m = 16;
00138                 Plot3DPanel p = new Plot3DPanel();
00139                 double[] X = new double[n];
00140                 double[] Y = new double[m];
00141                 double[][] Z = new double[m][n];
00142 
00143                 for (int i = 0; i < X.length; i++) {
00144                         X[i] = i / (double) X.length;
00145                         for (int j = 0; j < Y.length; j++) {
00146                                 Y[j] = j / (double) Y.length;
00147                                 Z[j][i] = Math.exp(X[i]) + Y[j];
00148                         }
00149                 }
00150                 p.addGridPlot("toto", X, Y, Z);
00151 
00152                 p.setLegendOrientation(PlotPanel.SOUTH);
00153                 new FrameView(p);
00154         }
00155 }

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