Desenhando curvas de Hermite em Java

Método para desenhar curvas de hermite em java, com detecção automática de tangentes. Os parâmetros enviados são: os vetores x e y, o tamanho dos vetores e um Graphics que irá desenhar os elementos.

public void hermite(double []x,double[] y, int TotMarks,Graphics g){
    int i=0;
    while(i+1 < TotMarks){
        double RangeX = ((x[i+1]-x[i])<0)?(-(x[i+1]-x[i])):(x[i+1]-x[i]);
        double RangeY = ((y[i+1]-y[i])<0)?(-(y[i+1]-y[i])):(y[i+1]-y[i]);
        double Step = (RangeX > RangeY)?(Step=(1.0/RangeX)):(Step=(1.0/RangeY));
        //Determinacao automatica das tangentes
        double t1x=x[0],t2x=x[1],t1y=y[0]-30,t2y=y[1]-30;
        if(i==0){
            t1x = x[i+1]-x[i];
            t2x = x[i+2]-x[i];
            t1y = y[i+1]-y[i];
            t2y = y[i+2]-y[i];
        }else{
            if((i!=0) && (i!=(TotMarks-2))){
                t1x = x[i+1]-x[i-1];
                t2x = x[i+2]-x[i];
                t1y = y[i+1]-y[i-1];
                t2y = y[i+2]-y[i];
            }else{
                t1x = x[i+1]-x[i-1];
                t2x = x[i+1]-x[i];
                t1y = y[i+1]-y[i-1];
                t2y = y[i+1]-y[i];
            }
        }
        double WG = 0.5,X=0,Y=0,Xant=0,Yant=0;
        for(double t=0;t<=1;t+=Step){
            X = 0.5 + ((2*Math.pow(t,3) -3*Math.pow(t,2) +1)* x[i] + (-2*Math.pow(t,3)+3*Math.pow(t,2))*x[i+1] + (1*Math.pow(t,3)-2*Math.pow(t,2)+t)*WG*t1x + (1*Math.pow(t,3)-1*Math.pow(t,2))*WG*t2x);
            Y = 0.5 + ((2*Math.pow(t,3) -3*Math.pow(t,2) +1)* y[i] + (-2*Math.pow(t,3)+3*Math.pow(t,2))*y[i+1] + (1*Math.pow(t,3)-2*Math.pow(t,2)+t)*WG*t1y + (1*Math.pow(t,3)-1*Math.pow(t,2))*WG*t2y);
            if(t == 0){
                Xant=X;
                Yant=Y;
            }else{
                g.drawLine((int)Xant,(int)Yant,(int)X,(int)Y);
                Xant=X;
                Yant=Y;
            }
        }
        i++;
    }
}
Compartilhe:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Live
  • MySpace
  • PDF
  • Rec6
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Identi.ca
  • Netvibes
  • Tumblr
  • blogmarks
  • Posterous
  • Yahoo! Buzz

Posts relacionados:

  1. Desenhando curvas de Bezier em Java
  2. Desenhando curvas Spline em Java
  3. Classe simples para uso de banco Db4o em Java
  4. Métodos de Gauss-Seidel e Gauss-Jacobi
  5. Algoritmo: Ajudando a prefeitura

Tags: , ,

This entry was posted on sábado, outubro 18th, 2008 at 7:32 and is filed under algoritmos. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.