Professional Documents
Culture Documents
ROLL NO : 70
CLASS : 8 (CMPN-2)
CG EXPERIMENT NO : 10
0000 represents the clip window and all other areas outside are to be clipped. If
both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies completely
inside the window: pass the endpoints to the draw routine. If both codes have a 1
in the same bit position (bitwise AND of the codes is not 0000), the line lies
outside the window. It can be trivially rejected. Step 3: If a line cannot be trivially
accepted or rejected, at least one of the two endpoints must lie outside the
window and the line segment crosses a window edge. This line must be clipped at
the window edge before being passed to the drawing routine. Step 4: Examine
one of the endpoints, say P0 and read P0 's 4-bit code in order: Left-to-Right,
Bottom-to-Top. Step 5: When a set bit (1) is found, compute the intersection I of
the corresponding window edge with the line from P0 to P1 . Replace P0 with I
and repeat the algorithm.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr()
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TURBOC4\\TC\\BGI");
printf("\n\tPlease enter the bottom left co-ordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tPlease enter the top right co-ordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nPlease enter the co-ordinates for starting point of line: ");
scanf("%f %f",&x1,&y1); printf("\nPlease enter the co-ordinates for ending point
of line: ");
scanf("%f %f",&x2,&y2);;
for(i=0;i
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1<xmin) start[0]=1;
if(x1>xmax) start[1]=1;
if(y1>ymax) start[2]=1;
if(y1<ymin) start[3]=1;
if(x2<xmin) end[0]=1;
if(x2>xmax) end[1]=1;
if(y2>ymax) end[2]=1;
if(y2<ymin) end[3]=1;
for(i=0;i<4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0) &&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)
&&(start[3]==0)&&(end[0]==0)&&(end[1]==0)
& &(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2); getch();
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m; y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1); x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2); x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1); x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2); getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
OUPUT:
Liang-Barsky Line Clipping Algorithm:
THEORY:
Liang and Barsky have created an algorithm that uses floating-point arithmetic but
finds the appropriate end points with at most four computations. This algorithm
uses the parametric equations for a line and solves four inequalities to find the
range of the parameter for which the line is in the viewport.
ALGORITHM:
Step 1: Read 2 endpoints of line as p1 (x1, y1) & p2 (x2, y2).
Step 2: Read 2 corners (left-top & right-bottom) of the clipping window as (xwmin,
ywmin, xwmax, ywmax).
Step 3: Calculate values of parameters pi and qi for i = 1, 2, 3, 4 such that
p1 = -dx, q1 = x1 – xwmin
p2 = dx, q2 = xwmax – x1
p3 = -dy, q3 = y1 – ywmin
p4 = dy, q4 = ywmax – y1
Step 4: if pi = 0 then line is parallel to its boundary
if qi < 0 then line is completely outside boundary so discard line
else, check whether line is horizontal or vertical and then check the line endpoints
with the corresponding boundaries.
Step 5: Initialize t1 & t2 as t1 = 0 & t2 = 1
Step 6: Calculate values for qi/pi for i = 1, 2, 3, 4.
Step 7: Select values of qi/pi where pi < 0 and assign maximum out of them as t1.
Step 8: Select values of qi/pi where pi > 0 and assign minimum out of them as t2.
Step 9: if (t1 < t2)
{
xx1 = x1 + t1dx
xx2 = x1 + t2dx
yy1 = y1 + t1dy
yy2 = y1 + t2dy
line (xx1, yy1, xx2, yy2) }
Step 10: Stop
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int i,gd,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TURBOC4\\TC\\BGI");
printf("\nPlease enter the co-ordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nPlease enter the co-ordinates for ending point of line: ");
xmin=100;
ymin=100;
xmax=250;
ymax=250;
rectangle(xmin,ymin,xmax,ymax);
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(i=0;i=0)
{
for(i=0;i<4;i++)
if(p[i]==0)
{
printf("line is parallel to one of the clipping boundary");
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}
if(y2>ymax)
{
y2=ymax;
}
line(x1,y1,x2,y2);
}
if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}
if(x2>xmax)
{
x2=xmax;
}
line(x1,y1,x2,y2);
}
}
}
}
t1=0;
t2=1;
for(i=0;i<4;i++)
{
temp=q[i]/p[i];
(p[i]<0)
{
if(t1<=temp)
t1=temp
}
else
{
if(t2>temp)
t2=tem;
}
}
if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}
delay(5000);
getch();
closegraph();
}
OUTPUT: