Write a Program in C++ to clip a line using Liang Barsky Method
- #include<iostream.h>
- #include <stdio.h>
- #include<conio.h>
- #include<graphics.h>
- #include<process.h>
- class liang
- {
- float x1,x2,y1,y2,u1,u2,dx,dy,xm,ym;
- float xmin, ymin, xmax, ymax,p[4],q[4],r[5],r[5];
- int gd, gm, test, s, k;
- public:
- void clipliang();
- void get();
- void map();
- void graph();
- };
- void liang :: map()
- {
- gd=DETECT;
- initgraph (&gd, &gm, "");
- int errorcode = graphresult();
- /*an error occurred */
- if (errorcode!=grOK)
- {
- printf("Graphics error: %s \n",grapherrormsg (errorcode));
- printf("Press and key to halt: ");
- getch();
- exit(1); /* terminate with an error code */
- }
- }
- void liang::graph()
- {
- xm=getmaxx()/2;
- ym=getmaxy()/2;
- line (xm, 0, xm, 2 * ym);
- line (0, ym, 2 * xm, ym);
- }
- void liang :: get()
- {
- cout<<"\n ENTER WINDOW COORDINATES xwmin, ywmin, xwmax, ywmax";
- cin>>xmin>>ymin>>xmax>>ymax>>;
- rectangle (xmin+xm,-ymin+ym,xmax+xm, -ymax+ym);
- // rectangle (320+xwmin, 240-ywmax, 320+xwmax, 240-ywmin);
- cout<<"\n ENTER END POINTS OF LINE (x1, y1)(x2, y2) ";
- cin>>x1>>y1>>x2>>y2;
- line (x1+xm,-y1+ym, x2+xm,-y2+ym);
- getch();
- }
- // line (x1, y1, x2, y2);
- void liang :: clipliang()
- {
- float x=0, y=1;
- dx=x2-x1;
- dy=y2-y1;
- p[0]=-dx;
- p[1]=dx;
- p[2]=-dy;
- p[3]=dy;
- q[0]=x1-xmin;
- q[1]=xmax-x1;
- q[2]=y1-ymin;
- q[3]=ymax-y1;
- for (k=0;k<4;k++)
- {
- if (p[k]==0&&q[k]<0)
- {
- cout<<"\n OUTSIDE";
- getch();
- exit(0);
- }
- if (p[k]<0)
- {
- r[k]=q[k]/p[k];
- if (r1[k]<y)
- y=r1[k];
- u2=y;
- }
- }
- if (u1>u2)
- {
- cout<<"\n COMPLETE OUTSIDE WINDOW";
- getch();
- exit(0);
- }
- void main ()
- {
- liang1;
- clrscr();
- l.map();
- l.graph();
- l.get();
- l.clipliang();
- getch();
- }
Output