Advertisement
Guest User

Untitled

a guest
Oct 8th, 2009
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. /*AIM:TO IMPLEMENT LIANG BARSKY LINE CLIPPING ALGORITHM
  2.   NAME:HARSH HEMRAJANI
  3.   BATCH:C12
  4.   ROLL NO:33
  5. */
  6. import java.util.*;
  7. import java.applet.Applet;
  8. import java.awt.*;
  9. class Clipper
  10. {
  11.     int WXmin,WYmin,WXmax,WYmax;
  12.     double u1,u2;
  13.     public Clipper(Rectangle r)     //Constructor initializes window parameters
  14.     {
  15.         WXmax=(int)r.getMaxX();
  16.         WXmin=(int)r.getMinX();
  17.         WYmax=(int)r.getMaxY();
  18.         WYmin=(int)r.getMinY();
  19.     }
  20.     public void clip(Point p1,Point p2,Graphics g) //Clips the line
  21.     {
  22.         int dx,dy,a,b;
  23.         boolean done=false;
  24.         g.drawRect(WXmin,WYmin,WXmax-WXmin,WYmax-WYmin);    //Drawing window
  25.         u1=0;u2=1;
  26.         dx=p2.x-p1.x;
  27.         dy=p2.y-p1.y;
  28.         a=p1.x;b=p1.y;
  29.         if(clipTest(-dx,p1.x-WXmin))
  30.             if(clipTest(dx,WXmax-p1.x))
  31.                 if(clipTest(-dy,p1.y-WYmin))
  32.                     if(clipTest(dy,WYmax-p1.y))
  33.                     {
  34.                         done=true;
  35.                         if(u1>0)
  36.                         {
  37.                             p1.x=a+(int)(u1*(double)dx);
  38.                             p1.y=b+(int)(u1*(double)dy);
  39.                         }
  40.                         if(u2<1)
  41.                         {
  42.                             p2.x=a+(int)(u2*(double)dx);
  43.                             p2.y=b+(int)(u2*(double)dy);
  44.                         }
  45.                         if(u1==0 && u2==1)
  46.                         {
  47.                             System.out.println("Line is completely inside");
  48.                             g.drawLine(p1.x,p1.y,p2.x,p2.y);
  49.                             return;
  50.                         }
  51.                     }
  52.         if(!done)
  53.         {
  54.             System.out.println("Line is completely outside the window");
  55.             return;
  56.         }
  57.         System.out.println("\nCo-ordinates of clipped line:");
  58.         System.out.println(p1.x+" "+p1.y);
  59.         System.out.println(p2.x+" "+p2.y);
  60.         g.drawLine(p1.x,p1.y,p2.x,p2.y);
  61.     }
  62.     private boolean clipTest(double p,double q)
  63.     {
  64.         double r=q/p;
  65.         boolean okay=true;
  66.         if(p<0)
  67.         {
  68.             if(r>u2)
  69.                 okay=false;
  70.             else if(r>u1)
  71.                 u1=r;
  72.         }
  73.         else if(p>0)
  74.         {
  75.             if(r<u1)
  76.                 okay=false;
  77.             else if(r<u2)
  78.                 u2=r;
  79.         }
  80.         else if(q<0)
  81.             okay=false;
  82.         return okay;
  83.     }
  84. }
  85. public class LiangClip extends Applet
  86. {
  87.     Point p1,p2; Clipper win;
  88.     public void init()
  89.     {
  90.         this.setSize(800, 800);
  91.         Scanner sc=new Scanner(System.in);
  92.         System.out.println("Enter the parameters of clipping window:");
  93.         System.out.print("Left top point:");
  94.         int rx=sc.nextInt();
  95.         int ry=sc.nextInt();
  96.         System.out.print("Width:");
  97.         int w=sc.nextInt();
  98.         System.out.print("Height:");
  99.         int h=sc.nextInt();
  100.         Rectangle r=new Rectangle(rx,ry,w,h);
  101.         win=new Clipper(r);
  102.         System.out.println("\nEnter the end points of a line:");
  103.         p1=new Point(sc.nextInt(),sc.nextInt());
  104.         p2=new Point(sc.nextInt(),sc.nextInt());
  105.     }
  106.     public void paint(Graphics g)
  107.     {
  108.         win.clip(p1,p2,g);
  109.     }
  110. }
  111. /*OUTPUT:
  112. Enter the parameters of clipping window:
  113. Left top point:
  114. 225 225
  115. Width:180
  116. Height:135
  117.  
  118. Enter the end points of a line:
  119. 125 400
  120. 420 180
  121. Co-ordinates of clipped line:
  122. 225 326
  123. 359 225
  124. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement