00001
00002
00003
00004 package org.math.plot.plots;
00005
00006 import java.awt.Color;
00007
00008 import javax.swing.JFrame;
00009
00010 import org.math.plot.FrameView;
00011 import org.math.plot.Plot2DPanel;
00012 import org.math.plot.PlotPanel;
00013 import org.math.plot.render.AbstractDrawer;
00014
00015 public class CloudPlot2D extends Plot {
00016
00017 double[][] NW;
00018
00019 double[][] NE;
00020
00021 double[][] SW;
00022
00023 double[][] SE;
00024
00025 double[] width_constant = { -1, -1 };
00026
00027 double[][] XY;
00028
00029 float[] f;
00030
00031 boolean fill_shape = true;
00032
00033 public CloudPlot2D(String n, Color c, double[][] _XYcard, double wX, double wY) {
00034 super(n, c);
00035 splitXYf(_XYcard);
00036 width_constant = new double[] { wX, wY };
00037
00038 build();
00039 }
00040
00041 private void splitXYf(double[][] xycard) {
00042 XY = new double[xycard.length][2];
00043 f = new float[xycard.length];
00044 float normf = 0;
00045 for (int i = 0; i < xycard.length; i++) {
00046 XY[i][0] = xycard[i][0];
00047 XY[i][1] = xycard[i][1];
00048 f[i] = (float) xycard[i][2];
00049 normf += f[i];
00050 }
00051
00052 for (int i = 0; i < f.length; i++) {
00053 f[i] = f[i] / normf;
00054 }
00055
00056 }
00057
00058 private void build() {
00059 if (width_constant[0] > 0) {
00060 NW = new double[XY.length][];
00061 NE = new double[XY.length][];
00062 SW = new double[XY.length][];
00063 SE = new double[XY.length][];
00064 for (int i = 0; i < XY.length; i++) {
00065 NW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] + width_constant[1] / 2 };
00066 NE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] + width_constant[1] / 2 };
00067 SW[i] = new double[] { XY[i][0] - width_constant[0] / 2, XY[i][1] - width_constant[1] / 2 };
00068 SE[i] = new double[] { XY[i][0] + width_constant[0] / 2, XY[i][1] - width_constant[1] / 2 };
00069 }
00070 }
00071 }
00072
00073 public void plot(AbstractDrawer draw, Color c) {
00074 if (!visible)
00075 return;
00076
00077 draw.canvas.includeInBounds(SW[0]);
00078 draw.canvas.includeInBounds(NE[XY.length - 1]);
00079
00080 draw.setColor(c);
00081 draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
00082 for (int i = 0; i < XY.length; i++) {
00083 if (f[i] > 0) {
00084 draw.fillPolygon(f[i], NW[i], NE[i], SE[i], SW[i]);
00085 }
00086 }
00087 }
00088
00089 @Override
00090 public void setData(double[][] d) {
00091 splitXYf(d);
00092 }
00093
00094 @Override
00095 public double[][] getData() {
00096 return XY;
00097 }
00098
00099 public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
00100 for (int i = 0; i < XY.length; i++) {
00101 int[] screenCoord = draw.project(XY[i]);
00102
00103 if ((screenCoord[0] + note_precision > screenCoordTest[0]) && (screenCoord[0] - note_precision < screenCoordTest[0])
00104 && (screenCoord[1] + note_precision > screenCoordTest[1]) && (screenCoord[1] - note_precision < screenCoordTest[1]))
00105 return XY[i];
00106 }
00107 return null;
00108 }
00109
00110 public static void main(String[] args) {
00111 Plot2DPanel p = new Plot2DPanel();
00112
00113 double[][] cloud = new double[100][2];
00114 for (int i = 0; i < cloud.length; i++) {
00115 cloud[i][0] = Math.random() + Math.random();
00116 cloud[i][1] = Math.random() + Math.random();
00117 }
00118 p.addCloudPlot("cloud", Color.RED, cloud, 5, 5);
00119
00120 double[][] cloud2 = new double[100][2];
00121 for (int i = 0; i < cloud2.length; i++) {
00122 cloud2[i][0] = 2 + Math.random() + Math.random();
00123 cloud2[i][1] = 2 + Math.random() + Math.random();
00124 }
00125 p.addCloudPlot("cloud2", Color.RED, cloud2, 5, 5);
00126
00127 p.setLegendOrientation(PlotPanel.SOUTH);
00128 new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00129 }
00130
00131 }