Desenhando curvas de Bezier em Java

Método para desenhar curvas de bezier 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 bezier(double []x,double[] y, int TotMarks, Graphics g){
    int i=0;
    while(i+3 < TotMarks){
        double RangeX = ((x[i+3]-x[i])<0)?(-(x[i+3]-x[i])):(x[i+3]-x[i]);
        double RangeY = ((y[i+3]-y[i])<0)?(-(y[i+3]-y[i])):(y[i+3]-y[i]);
        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)+3*t)*x[i+1]  +  (-3*Math.pow(t,3)+3*Math.pow(t,2))*x[i+2]  +  (1*Math.pow(t,3))*x[i+3]));
            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)+3*t)*y[i+1]  +  (-3*Math.pow(t,3)+3*Math.pow(t,2))*y[i+2]  +  (1*Math.pow(t,3))*y[i+3]));
            if(t == 0){
                Xant=X;
                Yant=Y;
            }else{
                g.drawLine((int)Xant,(int)Yant,(int)X,(int)Y);
                Xant=X;
                Yant=Y;
            }
        }
        i+=3;
    }
}
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 Hermite em Java
  2. Desenhando curvas Spline em Java
  3. Métodos de Gauss-Seidel e Gauss-Jacobi
  4. Algoritmo: Ajudando a prefeitura
  5. Modificando Look and Feel Aparência em Interfaces Java

Tags: , ,

This entry was posted on sábado, outubro 25th, 2008 at 21:17 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.