Practical No 10:-Clipping Algorithms. 1) Point Clipping Algorithm
Practical No 10:-Clipping Algorithms. 1) Point Clipping Algorithm
Practical No 10:-Clipping Algorithms. 1) Point Clipping Algorithm
:- 31
Output:-
Div.:- B Roll No.:- 31
if(y1>ywmax)
{start[3]=1; }
if(x2<xwmin)
{end[0]=1; }
if(x2>xwmax)
{end[1]=1; }
if(y2<ywmin)
{end[2]=1; }
if(y2>ywmax)
{end[3]=1; }
//performing and operation on start and end points code of the line
for(i=0;i<4;i++)
{code[i]=start[i]&&end[i];}
//Calculating slope
m=(y2-y1)/(x2-x1);
//top
if(start[3]==1)
{x1=x1+((ywmax-y1)/m);y1=ywmax;}
Div.:- B Roll No.:- 31
//botom
if(start[2]==1)
{x1=x1+((ywmin-y1)/m);y1=ywmin;}
//left
if(start[0]==1)
{y1=y1+((xwmin-x1)*m);x1=xwmin;}
//right
if(start[1]==1)
{y1=y1+((xwmax-x1)*m);x1=xwmax;}
if(end[3]==1)
{x2=x2+((ywmax-y2)/m);y2=ywmax;}
if(end[2]==1)
{x2=x2+((ywmin-y2)/m);y2=ywmin;}
if(end[0]==1)
{y2=y2+((xwmin-x2)*m);x2=xwmin;}
if(end[1]==1)
{y2=y2+((xwmax-x2)*m);x2=xwmax;}
}
}
else
{
cout<<"Line is Outside.";
}
rectangle(xwmin,ywmin,xwmax,ywmax);
line(x1,y1,x2,y2);
getch();
}
Div.:- B Roll No.:- 31
Output:-
Div.:- B Roll No.:- 31
}
void midpoint::drawwindow()
{rectangle(150,100,450,400);}
void midpoint::drawline (PT p1,PT p2)
{line(p1.x,p1.y,p2.x,p2.y);}
PT midpoint::setcode(PT p)
{
PT ptemp; if(p.y<=100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>=400)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if (p.x>=450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if (p.x<=150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y; return(ptemp);
}
int midpoint::visibility (PT p1,PT p2)
{
int i,flag=0; for(i=0;i<4;i++)
Div.:- B Roll No.:- 31
{
if((p1.code[i]!='0')||(p2.code[i]!='0')) flag=1;
}
if(flag==0)
return(0); for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) &&(p1.code[i]=='1')) flag=0;
}
if(flag==0)
return(1); return(2);
}
int main()
{
int gd=DETECT, gm,v; PT p1,p2,ptemp;
initgraph(&gd,&gm,"C:\\turboc3\\BGI ");
// setbkcolor(WHITE);
// setcolor(BLUE); cleardevice();
cout<<"\n\nENTER END-POINT 1 (x,y):\n"; cin>>p1.x>>p1.y;
cout<<"\n\nENTER END-POINT 2 (x,y): \n"; cin>>p2.x>>p2.y;
midpoint m; cleardevice(); m.drawwindow(); getch();
}
m.drawline(p1,p2); getch(); cleardevice(); m.drawwindow(); m.midsub(p1,p2); getch(); closegraph();
return(0);
Div.:- B Roll No.:- 31
Output:
Div.:- B Roll No.:- 31
4) Polygon Clipping
#include <iostream.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y); enum {
TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x3, LEFT = 0x4,
};
float xmin,xmax,ymin,ymax; class hodgeman
{
public:
void clip(float x0,float y0,float x1,float y1)
{
outcode code1,code2,codeout;
int accept = FALSE,done = FALSE; code1 = CompOutCode(x0,y0); code2 = CompOutCode(x1,y1); do
{
if(!(code1|code2))
{}
else
accept = TRUE; done = TRUE;
if(code1 & code2) done = TRUE;
else
{float x,y;
codeout = code1?code1:code2; if(codeout & TOP)
{y0);}
else
Div.:- B Roll No.:- 31
x = x0+(x1-x0)*(ymax-y0)/(y1- y = ymax;
if(codeout & BOTTOM)
{y0)/(y1-y0);
x0)/(x1-x0);x0)/(x1-x0);
}
else
x = x0+(x1-x0)*(ymin- y = ymin;
if(codeout & RIGHT)
{y = y0+(y1-y0)*(xmax-
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-
x = xmin;
}
if(codeout==code1)
{CompOutCode(x0,y0);}
else
x0 = x; y0 = y; code1 =
{CompOutCode(x1,y1);}
}
}
while(done==FALSE); if(accept)
line(x0,y0,x1,y1);
x1 = x; y1 = y; code2 =outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
Div.:- B Roll No.:- 31
hodgeman h;
cout<<"Enter the no of sides of polygon:"; cin>>n;
cout<<"\nEnter the coordinates of polygon\n"; for(i=0;i<2*n;i++)
{
cin>>poly[i];
}
poly[2*n]=poly[0]; poly[2*n+1]=poly[1];
cout<<"Enter the rectangular coordinates of clipping window\n";
cin>>xmin>>ymin>>xmax>>ymax; cleardevice();
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
Div.:- B Roll No.:- 31