//======================================================
// Numerical Analysis package in java
// Plot3D_JM class to plot 3D data
// Dr. Turhan Coban
// EGE University, Engineering School, Mechanical Eng. Div.
// turhan.coban@ege.edu.tr
// =====================================================
// utilizes JMath  3D Plot
// http://jmathtools.sourceforge.net/doku.php
// to use this program please add to : javahome/jre/lib/ext 
// jmathio.jar jmathplot.jar ve jmatharray.jar jar files

import javax.swing.*;
import org.math.plot.*;
import static java.lang.Math.*;
import static org.math.array.DoubleArray.*;
import java.awt.*;
import java.awt.event.*;



public class Plot3D_JM{
   
	Plot3DPanel plot;
   
   public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY,String sz,Color c)
   {   plot = new Plot3DPanel("SOUTH");
       add(f,x1min,x1max,x2min,x2max,NX,NY,sz,c);
   }
   public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY)
   {   plot = new Plot3DPanel("SOUTH");
       add(f,x1min,x1max,x2min,x2max,NX,NY,"z=f(x,y)",Color.blue);
   }
   
   public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max)
   {   plot = new Plot3DPanel("SOUTH");
       add(f,x1min,x1max,x2min,x2max,20,20,"z=f(x,y)",Color.blue);
   }
   
   public Plot3D_JM(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz)
   {   plot = new Plot3DPanel("SOUTH");
       add(f,x1min,x1max,x2min,x2max,20,20,sz,Color.blue);
   }
   
   public void add(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY,String sz,Color c)
   {   double dx1=(x1max-x1min)/(double)NX;
   double dx2=(x2max-x2min)/(double)NY; ;
   double[] xx0 = increment(x1min, dx1, x1max);
   double[] xx1 = increment(x2min, dx2, x2max);
   double x[]=new double[2];
   double[][] z = new double[NY][NX];
		for (int i = 0; i < NX; i++)
			for (int j = 0; j < NY; j++)
				{x[0]=xx0[i];x[1]=xx1[j];z[j][i] = f.func(x);}
		// add grid plot to the PlotPanel
		plot.addGridPlot(sz,c, xx0, xx1, z);
   }
   
   public void add(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz,Color c)
   {add(f,x1min,x1max,x2min,x2max,20,20,sz,c);}
   
   public void add(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz)
   {add(f,x1min,x1max,x2min,x2max,20,20,sz,Color.blue);}

   public void add(f_xj f, double x1min,double x1max,double x2min,double x2max)
   {add(f,x1min,x1max,x2min,x2max,20,20,"z=f(x,y)",Color.blue);}
   public void plot(String sz)
   {JFrame frame = new JFrame(sz);
		frame.setSize(600,600);
		frame.setContentPane(plot);
		frame.setVisible(true);
   }
   public void plot()
   {plot("z=f(x,y)");}
   public static void func(f_xj f, double x1min,double x1max,double x2min,double x2max,int NX,int NY,String sz)
   {	   
   double dx1=(x1max-x1min)/(double)NX;
   double dx2=(x2max-x2min)/(double)NY; ;
   double[] xx0 = increment(x1min, dx1, x1max);
   double[] xx1 = increment(x2min, dx2, x2max);
   double x[]=new double[2];
   double[][] z = new double[NY][NX];
		for (int i = 0; i < NX; i++)
			for (int j = 0; j < NY; j++)
				{x[0]=xx0[i];x[1]=xx1[j];z[j][i] = f.func(x);}
        Plot3DPanel plot = new Plot3DPanel("SOUTH");
		// add grid plot to the PlotPanel
		plot.addGridPlot("z=f(x,y)",Color.black, xx0, xx1, z);
		JFrame frame = new JFrame(sz);
		frame.setSize(600,600);
		frame.setContentPane(plot);
		frame.setVisible(true);
   }
   public static void func(f_xj f, double x1min,double x1max,double x2min,double x2max)
   {func(f, x1min,x1max,x2min,x2max,40,40,"z=f(x,y)");}
   public static void func(f_xj f, double x1min,double x1max,double x2min,double x2max,String sz)
   {func(f, x1min,x1max,x2min,x2max,40,40,sz);}   
  
  public static void main(String[] args)
  { f2 f=new f2();
    func(f,-10.0,10.0,0.0,20.0,"fx");
  }
} //end of Plot3D
