Algoritmo GraficoLinea


public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(color.red);
        int dx = x1 - x0;
        int dy = y1 - y0;

         if (Math.abs(dx) > Math.abs(dy))
        {        
            float m = (float) dy / (float) dx;
            float b = y0 - m * x0;
            if(dx<0)
                dx =  -1;
            else
                dx =  1;
            while (x0 != x1)
            {
                x0 += dx;
                y0 = Math.round(m*x0 + b);
                g.drawLine( x0, y0, x0, y0);
            }
        } else
        if (dy != 0)
        {                            
            float m = (float) dx / (float) dy;    
            float b = x0 - m*y0;
            if(dy<0)
                dy =  -1;
            else
                dy =  1;
            while (y0 != y1)
            {
                y0 += dy;
                x0 = Math.round(m * y0 + b);
                g.drawLine( x0, y0, x0, y0);
            }
        }
    }
   
    public void DibujarPrimitivaLinea()
    {
      repaint();
    }

0 comentarios:

Publicar un comentario

COMPARTIR

Twitter Delicious Facebook Digg Stumbleupon Favorites More