Desenhando curvas Spline em Java

Método para desenhar curvas spline em java. 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 spline(double []x,double[] y, int TotMarks,Graphics g){
    int i=0;
    while(i+3 < TotMarks){
        double RangeX = ((x[i+2]-x[i+1])<0)?(-(x[i+2]-x[i+1])):(x[i+2]-x[i+1]);
        double RangeY = ((y[i+2]-y[i+1])<0)?(-(y[i+2]-y[i+1])):(y[i+2]-y[i+1]);
        double Step = (RangeX > RangeY)?(Step=(1.0/RangeX)):(Step=(1.0/RangeY));
        double X=0,Y=0,Xant=0,Yant=0;
        for(double t=0;t<=1;t+=Step){
            X = (((-1*Math.pow(t,3)+3*Math.pow(t,2)-3*t+1)*x[i]  +  (3*Math.pow(t,3)-6*Math.pow(t,2)+4)*x[i+1]  +  (-3*Math.pow(t,3)+3*Math.pow(t,2)+3*t+1)*x[i+2]  +  (1*Math.pow(t,3))*x[i+3])/6);
            Y = (((-1*Math.pow(t,3)+3*Math.pow(t,2)-3*t+1)*y[i]  +  (3*Math.pow(t,3)-6*Math.pow(t,2)+4)*y[i+1]  +  (-3*Math.pow(t,3)+3*Math.pow(t,2)+3*t+1)*y[i+2]  +  (1*Math.pow(t,3))*y[i+3])/6);
            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 this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Live
  • MySpace
  • Turn this article into a PDF!
  • Rec6
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Bookmarks
  • Identi.ca
  • Netvibes
  • Tumblr
  • Twitthis

Nenhum post relacionado.

Tagged : , ,

Deixe uma resposta




Spam Protection by WP-SpamFree