Showing posts with label LINE CLIPPING. Show all posts
Showing posts with label LINE CLIPPING. Show all posts

Saturday, October 26, 2013

LIYAN BASKY LINE CLIPPING ALGORITHM IN JAVA

/*
Topic-LIYAN BASKY LINE CLIPPING ALGORITHM IN JAVA
*/
import java.awt.*;
import java.applet.*;
import java.math.*;
import java.io.*;
import java.util.*;

public class LB extends Applet
{
Scanner d=new Scanner(System.in);
 public void paint(Graphics g)
 {
  System.out.println("enter value of xmin");
  int xmin=d.nextInt();
   System.out.println("enter value of ymin");
  int ymin=d.nextInt();
  System.out.println("enter value of xmax");
  int xmax=d.nextInt();
  System.out.println("enter value of ymax");
  int ymax=d.nextInt();
  int p[]=new int[4];
  int q[]=new int[4];
  System.out.println("enter value of x1");
  int x1=d.nextInt();
System.out.println("enter value of y1");
  int y1=d.nextInt();
  System.out.println("enter value of x2");
  int x2=d.nextInt();
  System.out.println("enter value of y2");
  int y2=d.nextInt();
  g.drawLine(xmin,ymin,xmin,ymax);
  g.drawLine(xmin,ymax,xmax,ymax);
  g.drawLine(xmax,ymax,xmax,ymin);
  g.drawLine(xmax,ymin,xmin,ymin);
  g.drawLine(x1,y1,x2,y2);
  p[0]=-(x2-x1);
  p[1]=x2-x1;
  p[2]=-(y2-y1);
  p[3]=y2-y1;
  q[0]=x1-xmin;
  q[1]=xmax-x1;
  q[2]=y1-ymin;
  q[3]=ymax-y1;
  float t1=0,t2=1;
  int flag=0;
  for(int i=0;i<4;i++)
  {
   if(p[i]==0)
   {
    if(q[i]>=0)
    {
     if(i<2)
     {
      if(y1<ymin)
       y1=ymin;
      if(y2>ymax)
       y2=ymax;
     }
     if(i>1)
     {
      if(x1<xmin)
      x1=xmin;
      if(x2>xmax)
      x2=xmax;
     }
     flag=1;
     g.drawLine(x1,y1,x2,y2);
    }
   }
  }
  if(flag==0)
  {
   for(int i=0;i<4;i++)
   {
    float t=(float)q[i]/(float)p[i];
    if(p[i]<0)
    {
     if(t1<t)
     t1=t;
    }
    else
    {
     if(t2>t)
     t2=t;
    }
   }
  if(t1<t2)
  {
   int xx1=(int)(x1+t1*p[1]);
   int xx2=(int)(x1+t2*p[1]);
   int yy1=(int)(y1+t1*p[3]);
   int yy2=(int)(y1+t2*p[3]);
  xmin =xmin + 200;
   ymin =ymin + 400;
  xmax =xmax + 200;
  ymax =ymax + 400;
   xx1 =xx1 + 200;
   xx2 =xx2 + 200;
   yy1 =yy1 + 400;
   yy2 =yy2 + 400;
    g.drawLine(xmin,ymin,xmin,ymax);
  g.drawLine(xmin,ymax,xmax,ymax);
  g.drawLine(xmax,ymax,xmax,ymin);
  g.drawLine(xmax,ymin,xmin,ymin);
  g.drawLine(xx1,yy1,xx2,yy2);
  }
  }
 }
}
/*
<applet code="LB.class" width=700 height=700>
</applet>
*/



COHEN-SUTHERLAND LINE CLIPPING ALGORITHM IN JAVA

/*
COHEN-SUTHERLAND ALGORITHM
*/
import java.applet.*;
import java.awt.*;
import java.util.*;
public class CohenS extends Applet
{
    Scanner sc=new Scanner(System.in);
            int xmax,ymax,xmin,ymin;
            public int[] set(int x,int y)
            {
                        int a[]=new int[4];
                        if(x<xmin)
                                    a[3]=1;
                        else
                                    a[3]=0;
                        if(x>xmax)
                                    a[2]=1;
                        else
                                    a[2]=0;
                        if(y<ymin)
                                    a[0]=1;
                        else
                                    a[0]=0;
                        if(y>ymax)
                                    a[1]=1;
                        else
                                    a[1]=0;
                        return a;
            }
            void setminmax()
            {
            System.out.println("Enter xmax of the Rectangle");
            xmax=sc.nextInt();
            System.out.println("Enter ymax of the Rectangle");
            ymax=sc.nextInt();
            System.out.println("Enter xmin of the Rectangle");
            xmin=sc.nextInt();
            System.out.println("Enter ymin of the Rectangle");
            ymin=sc.nextInt();
           
            }
            boolean check(int a[])
            {
                        for(int i=0;i<a.length;i++)
                                    if(a[i]==1)
                                                return false;
                        return true;
            }
            int[] produceXY(int i,int x1,int y1,float m)
            {
                        int a[]=new int [2];
                        float x=0,y=0;
                        switch(i)
                        {
                                    case 0:
                                                x=xmin;
                                                y=y1+m*(x-x1);
                                                break;
                                  
                                    case 1:
                                                x=xmax;         
                                                y=y1+m*(x-x1);
                                                break;
                                    case 3:
                                                y=ymin;
                                                x=x1+(y-y1)/m;
                                                break;
                                    case 2:
                                                y=ymax;
                                                x=x1+(y-y1)/m;
                                                break;
                        }
                        a[0]=(int)x;
                        a[1]=(int)y;
                        return a;
            }
            boolean doAnd(int a[],int b[])
            {
                        for(int i=0;i<a.length;i++)
                        {
                                    int k=a[i]&b[i];
                                    if(k==1)
                                                return false;
                        }
                        return true;
            }
            public void paint(Graphics g)
            {
                setminmax();
                        g.drawRect(xmin,ymin,xmax-xmin,ymax-ymin);
                       g.drawRect(xmin+100,ymin,xmax-xmin,ymax-ymin);
                        int a[][]=new int[2][4];
                        int b[][]=new int[2][4];
                        int c[]=new int[2];
                        int c1=20;
                        int x1,y1,x2,y2;
                        System.out.println("Enter x1 of the line");
                x1=sc.nextInt();
                    System.out.println("Enter y1");
                    y1=sc.nextInt();
                    System.out.println("Enter x2");
                    x2=sc.nextInt();
                    System.out.println("Enter y2");
                y2=sc.nextInt();
           
                        float m=(y2-y1)*1.0f/(x2-x1);
                       
                        g.drawLine(x1,y1,x2,y2);
                        a[0]=set(x1,y1);
                        a[1]=set(x2,y2);
                        if(check(a[0])&&check(a[1]))
                        {
                                    g.drawLine(x1,y1,x2,y2);
                        }
                        else
                        {
                                    if( doAnd(a[0]  , b[1]) )
                                    {
                                                for(int i=a[0].length-1;i>=0;i--)
                                                {
                                                            if(a[0][i]==1)

                                                            {
                                                                        c=produceXY(a[0].length-1-i,x1,y1,m);
                                                                        b[0]=set(c[0],c[1]);
                                                         
                                                                        if(check(b[0]))
                                                                        {
                                                                                    x1=c[0];
                                                                                    y1=c[1];
                                                                                    break;
                                                                        }
                                                                        c1+=20;
                                                            }
                                                }
                                                for(int i=a[0].length-1;i>=0;i--)
                                                {
                                                            if(a[1][i]==1)
                                                            {
                                                                        c=produceXY(a[0].length-1-i,x1,y1,m);
                                                                        b[1]=set(c[0],c[1]);
                                              
                                                                        if(check(b[1]))
                                                                        {
                                                                                    x2=c[0];
                                                                                    y2=c[1];
                                                                                    break;
                                                                        }
                                                                        c1+=20;
                                                            }
                                                }
                                                g.drawLine(x1+100,y1,x2+100,y2);
                                    }
                        }
            }
}
/*<applet code="CohenS.class" width=1000 height=1000>
</applet>*/