Don't Wait,Do it yourself.

Latest courses

Line Drawing Algorithm for BLA of all case

PROGRAM :-

import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
class Bsr extends JFrame{
    int x11,y11,x22,y22;
    Bsr( double x1,double y1,double x2,double y2)
    {
        this.x11=(int)x1;
        this.y11=(int)y1;
        this.x22=(int)x2;
        this.y22=(int)y2;
        setTitle("DDR");                         // jframe title
        setSize( 2000,2000);                  // jframe size
        setVisible(true);                       //set visibility
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }


    public void paint(Graphics g)
    {

        g.setColor(Color.BLACK);
//         g.drawLine(350,500,1000,500);
     g.drawLine(x11,y11,x22,y22);

    }

    public static void main(String [] args)
    {
        Scanner r=new Scanner(System.in);

        System.out.println("enter the four point");
        int  flag;
        double x1,y1,x2,y2,delx,dely;
        x1=r.nextInt();
        y1=r.nextInt();
        x2=r.nextInt();
        y2=r.nextInt();
        Bsr sss=new Bsr(x1,y1,x2,y2);
        delx=x2-x1;
        dely=y2-y1;
        double slope=(dely)/(delx);
        if(slope>1)
        {
            flag=1;
        }
        else
        {
            flag=0;
        }
        switch(flag)
        {

            case 0: //for m<1
            {
                double p= 2*dely*x1-2*delx*y1;
                while(x1<=x2)
                {

                    System.out.println(x1+", "+y1);
                    System.out.println();
                    x1=x1+1;
                    if(p<0)
                        p=p+2*dely;
                    else
                    {
                        p=p+2*dely-2*delx;
                        y1=y1+1;
                    }

                }
            }
            case 1:    //for m>1
            {

                double p= 2*delx*y1-2*dely*x1;
                while(y1<=y2)
                {

                    System.out.println(x1+", "+y1);
                     System.out.println();
                    y1=y1+1;
                    if(p<0)
                        p=p+2*delx;
                    else
                    {
                        p=p+2*dely-2*dely;
                        x1=x1+1;
                    }

                }
            }
        }

    }
}

No comments:

Post a Comment

Please do not enter any spam link in the comment Box