Experiment 7: Aim: Theory
Experiment 7: Aim: Theory
Theory :
This algorithm uses the clipping window as shown in the following figure. The
minimum coordinate for the clipping region is (XWmin,YWmin) and the
maximum coordinate for the clipping region is (XWmax,YWmax).We will use
4-bits to divide the entire region. These 4 bits represent the Top, Bottom,
Right, and Left of the region as shown in the following figure. Here, the TOP
and LEFT bit is set to 1 because it is the TOP-LEFT corner.
There are 3 possibilities for the line
Line can be completely inside the window (This line should be
accepted).
Line can be completely outside of the window (This line will be
completely removed from the region).
Line can be partially inside the window (We will find intersection point
and draw only that portion of line that is inside region).
Algorithm :
Step 1 Assign a region code for each endpoints.
Step 2 If both endpoints have a region code 0000 then accept this line.
Step 3 Else, perform the logical ANDoperation for both region codes.
Step 3.1 If the result is not 0000, then reject the line.
Step 3.2 Else you need clipping.
Step 3.2.1 Choose an endpoint of the line that is outside the window.
Step 3.2.2 Find the intersection point at the window boundary (base
on region code).
Step 3.2.3 Replace endpoint with the intersection point and update
the region code.
Step 3.2.4 Repeat step 2 until we find a clipped line either trivially
accepted or trivially rejected.
Step 4 Repeat step 1 for other lines.
Code :
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
int i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
int start[4],end[4],code[4];
initgraph(&gd,&gm,NULL);
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%d %d",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%d %d",&xmax,&ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%d %d",&x1,&y1);
printf("\nEnter the coordinates for ending point of line: ");
scanf("%d %d",&x2,&y2);
for(i=0;i<4;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");
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;
}
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
OUTPUT :
Discussion :
The brute-force approach to clipping a line that cannot be trivially accepted is
to intersect that line with each of the four clip-rectangle edges to see whether
any intersection points lie on those edges; if so, the line cuts the clip
rectangle and is partially inside. For each line and clip-rectangle edge, we
therefore take the two mathematically infinite lines that contain them and
intersect them. Next, we test whether this intersection point is "interior" -- that
is, whether it lies within both the clip rectangle edge and the line; if so, there
is an intersection with the clip rectangle.
Disadvantages:
Fixed-order decision can do needless work
Can improve using more regions
Can generate more efficient rejection tests
Clipping window must be rectangular