Jump to content

graph plotting

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
sirine

sirine

    Newbie

  • Members
  • Pip
  • 3 posts
I'm new here and impressed from your forum. I'm quite new in Java and i need some help regarding the Frame and Graphics classes in java.

I have written 3 classes (function, parabola and polynomial), which represent functions with a variable . I must to extend my function class with a method named public void plot(double xmin, double xmax), which opens a frame with the graph of determined function. the parameter xmin and xmax give the x-area, where the graph should be drawn. A labeld coordinate axis must also appear in the frame. Can you help me?

Here are my classes :


/**

 * Class to respresent parabola functions

 */

public class Function{

	

    /**

     * some static constants 

     */

    public final static int n = 1000;

    public final static double eps = 0.000000001;

    public final static Function NULL=new Function(){

        public double map(double x){return 0.0; }

        public String toString() {return "0.0"; }

    };

    

    

    /**

     * Defines the mapping of the function. The default mapping is not-defined

     * so the return value is Double.NaN for instances of this class. Subclass

     * should override this method to define the mapping

     * @param x

     * @return

     */

    public double map(double x){return Double.NaN;}

    

    /**

     * Evaluates the function at each arguemt of the array

     * @param x array of function values

     * @return

     */

    public double[] map(double[] x){

        double[] y=new double[x.length];

        for (int i=0; i<x.length; i++) y[i]=map(x[i]);

        return y;

    }

    

    /**

     * Access to the derivation of the Function instances. Subclass should

     * override this method. Use Double.NaN at non-differntiable positions

     * @return derivative of this function instance

     */

    public Function derivate(){

        return this;

    }


    /**

     * a

     * approximate the integral of this function using n boxes

     * @param a stating of the integration

     * @param b ending of the integration

     * @param n number of boxes (equal width)

     * @return approximation of the integral over [a,b] of this function

     */    

    public double integrate(double a, double b, int n){

        double delta=(b-a)/(double)n;

        double area=0;

        for (double i=0; i<n; i++) area+=map(a+i*delta);

        return area*delta;

    }


    /**

     * If possible this method should return the exact integral value. 

     * Subclasses should override this method.

     * The default is to call the approximation integrate(a,b,n).

     * @param a stating of the integration

     * @param b ending of the integration

     * @param n number of boxes (equal width)

     * @return integral over [a,b] of this function

     */

    public double integrate(double a, double b){

        return this.integrate(a,b,this.n);

    }


    /** 

     * Computes a zero-crossing using the newton iteration

     * @param x_0 initial argument

     * @return Zero-Crossing or Double.NaN

     */

    public double newtonIteration(double x_0){

        double d=42.0;

        Function f_1=this.derivate();

        for (int i=0; ((++i<this.n) && (Math.abs(d)>eps)); x_0+=d)

            d=-map(x_0)/f_1.map(x_0);

        return (Math.abs(d)>eps) ? Double.NaN : x_0;

    }

    

    /**

     * String representation of this function

     * @return "undefined"

     */

    public String toString(){

        return "undefined";

    }

}



/**

 * Class to respresent parabola functions

 *

 */

public class Parabola extends Polynomial{

    

    /**

     * Constructor for a parabola object ax? + bx + c

     * @param a coefficient for x^2

     * @param b coefficient for x^1

     * @param c coefficient for x^0

     */

    public Parabola(double a, double b, double c){        

        super(new double[] {c,b,a});        

    }

    

    /**

     * Compute the Zero Crossings of the parabola

     * @return zero-crossings, i.e. x with f(x)=0

     */

    public double[] zeroCrossing(){

        double c=map(0);

        double b=map(0.5)-map(-0.5);

        double a=map(1)-b-c;


        //Diskriminante

        double d = b*b - 4.0*c*a;

        

        if (this.getDegree()<=1){

            if (b==0) return new double[0];

            else return new double[] {-c/b};        

        }

                

        if (d==0) return new double[] {-0.5*b/a};

        if (d<0) return new double[0];

        

        return new double[] { 0.5*(-b-Math.sqrt(d))/a, 0.5*(-b+Math.sqrt(d))/a};

    }


}



/**

 * Class to respresent polynomial functions

 *

 * 

 * Bug in limPosInf() for constant functions repaired

 * 

 */

public class Polynomial extends Function{


    // Koeffizienten des Polynoms

    private double[] a=new double[0];

    

    /**

     * Constructor for a polynomial function, sum a[i] * x^i

     * @param a coeffients of the polynomial function

     */

    public Polynomial(double[] a){

        int deg = a.length-1;

        for (int i=deg; ((i>=0) & (a[i]==0.0)) ; i--) deg--;

        this.a = new double[deg+1];

        for (int i=0; i<=deg; i++) this.a[i]=a[i];

    }

    

    /**

     * Access to the degree of the polynomial, i.e. the maximal "nonzero" power

     * @return degree of the polynomial

     */

    public int getDegree(){

        return a.length-1;

    }

    

    /**

     * Access to the derivative of the polynomial

     * @return the derivative of the polynomial

     */

    public Function derivate(){

        if (this.getDegree()==0) return Function.NULL;

        double[] b=new double[this.a.length-1];

        for (int i=0; i<b.length; )

            b[i]=((double)++i)*this.a[i];

        return new Polynomial(b);

    }

    

    /**

     * Computes the integral of the polynomial over an interval

     * @param a begining of the integration interval

     * @param b ending of the integration interval

     * @return integral over [a,b] of polynomial

     */

    public double integrate(double a, double b){

        double[] a_=new double[this.a.length+1];

        a_[0]=0.0;

        for (int i=1; i<a_.length; i++)

            a_[i]=this.a[i-1]/((double)i);

        Polynomial F = new Polynomial(a_);

        return F.map(b)-F.map(a);

    }

    

    /**

     * function value of the polynomial at x

     * @param x argument of the function

     * @return value f(x) of the function

     */

    public double map(double x){

        double y=0.0;

        for (int i=getDegree(); i>=0; i--) y=y*x+a[i];

        return y;

    }

    

    /**

     * computes the limit for x->oo

     * @return limit for x->oo

     */

    public double limPosInf(){

        if (getDegree()==0) return map(0);

        return (a[getDegree()]>0)

                ? Double.POSITIVE_INFINITY

                : Double.NEGATIVE_INFINITY;

    }


    /**

     * computes the limit for x->-oo

     * @return limit for x->-oo

     */

    public double limNegInf(){

        return (getDegree()%2==0)

                ?  limPosInf()

                : -limPosInf();

    }

    

    /** String representation

     * @return awesome string representation

     */

    public String toString(){

        //sorry for this bad code ;)

        String s="";

        String vz="";

        for (int i=0; i<a.length; i++)

            if (a[i]!=0) {

                s=((i==0) ? a[i] : (a[i]==1) ? "" : (a[i]==-1) ? "-" : ""+a[i])

                 +((i==0) ? "" :(i==1) ? "x" : "x^"+i)//aktuelle Potenz

                 +vz                                  //zusätzliches Vorzeichen

                 +s;                                  //Kleinere Potenzen

                

                //Berechnung des aktuellen Vorzeichens

                if (a[i]>0 ) vz="+"; else vz="";

            }            


        return s;

    }

}



#2
joaoricardo

joaoricardo

    Newbie

  • Members
  • Pip
  • 1 posts
Man, I am making a program to plot a graph on my cellphone. I think you need that class below. It adjustes your graph to your cellphone screen. Thanks man, your work will help me out.
public class Transformacoes {

  public static void main( String[] args) {
   AplicaTransformacoes();
  }
  public static void AplicaTransformacoes() {
    double Xmin,Xmax,Ymin,Ymax;
    int XLmax,YLmax;
    double DX,DY;
    int DXL,DYL;
    double x,y,z;
    int xl,yl,zl;
    double s,tx,ty;
    double S[][],T[][],M[][],trans[][],aux[][];
    S=new double[3][3];T=new double[3][3];M=new double[3][3];trans=new double[3][3];aux=new double[3][3];

    
    Xmin=-500;
    Xmax=1000;
    Ymin=-1000;
    Ymax=1000;

    DXL=200;//tamanho da tela do celular
    DYL=300;
    
    DX=Xmax-Xmin;//largura do gráfico definida pelo usuario
    DY=Ymax-Ymin;
    
    double rl,r;
    rl=DXL/DYL;//
    r=DX/DY;
    if (rl<r) 
      s=DXL/DX;//s é a escala, o fator multiplicador.
    else
      s=DYL/DY;
    tx=Math.abs(DXL*Xmin/DX);// obtem o valor absoluto(sem o sinal) 
    ty=Math.abs(DYL*Ymax/DY);
    System.out.println("Escala:"+s);
    System.out.println("Translacao X:"+tx);
    System.out.println("Translacao Y:"+ty);
   
    
    M[0][0]=1;M[0][1]=0;M[0][2]=0;  //Qual dessas matriz realiza a translacao, a rotacao e inversao?
    M[1][0]=0;M[1][1]=-1;M[1][2]=0;
    M[2][0]=0;M[2][1]=0;M[2][2]=1;

    S[0][0]=s;S[0][1]=0;S[0][2]=0;
    S[1][0]=0;S[1][1]=s;S[1][2]=0;
    S[2][0]=0;S[2][1]=0;S[2][2]=1;

    T[0][0]=1;T[0][1]=0;T[0][2]=tx;
    T[1][0]=0;T[1][1]=1;T[1][2]=ty;
    T[2][0]=0;T[2][1]=0;T[2][2]=1;
    
    for (int i=0;i<3;i++) {
      for (int j=0;j<3;j++) {
        aux[i][j]=0;
        for (int k=0;k<3;k++) {
          aux[i][j]=aux[i][j] + M[i][k]*S[k][j];
        }
      }
    }
    for (int i=0;i<3;i++) {
      for (int j=0;j<3;j++) {
        trans[i][j]=0;
        for (int k=0;k<3;k++) {
          trans[i][j]=trans[i][j] + T[i][k]*aux[k][j];
        }
      }
    }
    x=-100;
    y=-100;
    xl=(int)(trans[0][0]*x+trans[0][1]*y+trans[0][2]);
    yl=(int)(trans[1][0]*x+trans[1][1]*y+trans[1][2]);
    System.out.println("x="+x);
    System.out.println("xl="+xl);
    System.out.println("y="+y);
    System.out.println("yl="+yl);
    
    x=200;
    y=-100;
    xl=(int)(trans[0][0]*x+trans[0][1]*y+trans[0][2]);
    yl=(int)(trans[1][0]*x+trans[1][1]*y+trans[1][2]);
    System.out.println("x="+x);
    System.out.println("xl="+xl);
    System.out.println("y="+y);
    System.out.println("yl="+yl);

    x=50;
    y=200;
    xl=(int)(trans[0][0]*x+trans[0][1]*y+trans[0][2]);
    yl=(int)(trans[1][0]*x+trans[1][1]*y+trans[1][2]);
    System.out.println("x="+x);
    System.out.println("xl="+xl);
    System.out.println("y="+y);
    System.out.println("yl="+yl);

    double transinv[][]=new double[3][3];
    double c=1/(trans[0][0]*trans[1][1]-trans[0][1]*trans[1][0]);
    transinv[0][0]=c*trans[1][1];
    transinv[0][1]=-c*trans[0][1]; 
    transinv[0][2]=c*(trans[0][1]*trans[2][1]-trans[0][2]*trans[1][1]);
    transinv[1][0]=-c*trans[1][0];
    transinv[1][1]=c*trans[0][0]; 
    transinv[1][2]=c*(trans[0][2]*trans[1][0]-trans[0][0]*trans[1][2]);
    transinv[2][0]=0;
    transinv[2][1]=0; 
    transinv[2][2]=c*(trans[0][0]*trans[1][1]-trans[1][0]*trans[0][1]);
    xl=0;
    yl=0;
    x=(int)(transinv[0][0]*xl+transinv[0][1]*yl+transinv[0][2]);
    y=(int)(transinv[1][0]*xl+transinv[1][1]*yl+transinv[1][2]);
    System.out.println("xl="+xl);
    System.out.println("x="+x);
    System.out.println("yl="+yl);
    System.out.println("y="+y);
    xl=200;
    yl=300;
    x=(int)(transinv[0][0]*xl+transinv[0][1]*yl+transinv[0][2]);
    y=(int)(transinv[1][0]*xl+transinv[1][1]*yl+transinv[1][2]);
    System.out.println("xl="+xl);
    System.out.println("x="+x);
    System.out.println("yl="+yl);
    System.out.println("y="+y);
  
  }

}

Edited by WingedPanther, 03 May 2009 - 11:11 AM.
add code tags (the # button)