Sie sind auf Seite 1von 14

Div.:- B Roll No.

:- 31

Practical No 10:- Clipping Algorithms.


1) Point clipping Algorithm.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\bgi");
float xwmin,ywmin,xwmax,ywmax,x1,y1;

//entering coordinates of window


cout<<"Enter xwmin and ywmin: ";
cin>>xwmin>>ywmin;
cout<<"Enter xwmax and ywmax: ";
cin>>xwmax>>ywmax;

//entering point detail


cout<<"Enter x1 and y1: ";
cin>>x1>>y1;
if(x1>=xwmin && x1<=xwmax && x1>=ywmin && y1<=ywmax)
{
cout<<"Point is Clipped. ";
rectangle(xwmin,ywmin,xwmax,ywmax);
circle(x1,y1,1);
getch();
}
else
{
cout<<"Point is Outside Window.";
rectangle(xwmin,ywmin,xwmax,ywmax);
circle(x1,y1,1);
getch();
}
}
Div.:- B Roll No.:- 31

Output:-
Div.:- B Roll No.:- 31

2) Cohen Sutherland Line Clipping Algorithm.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\bgi");
float m,xwmin,ywmin,xwmax,ywmax,x1,y1,x2,y2,i,start[4],end[4],code[4];
cout<<"Sutherland Cohan Line Clipping Algorithm:- "<<endl;

//entering coordinates of window


cout<<"Enter xwmin and ywmin: ";
cin>>xwmin>>ywmin;
cout<<"Enter xwmax and ywmax: ";
cin>>xwmax>>ywmax;

//entering line detail


cout<<"Enter x1 and y1: ";
cin>>x1>>y1;
cout<<"Enter x2 and y2: ";
cin>>x2>>y2;

cout<<"Before Clipping: "<<endl;


line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
for(i=0;i<4;i++)
{start[i]=0;end[i]=0;}
if(x1<xwmin)
{start[0]=1; }
if(x1>xwmax)
{start[1]=1; }
if(y1<ywmin)
{start[2]=1; }
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);

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();
cout<<"The Line is Completely Visible.";
rectangle(xwmin,ywmin,xwmax,ywmax);
line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
cout<<"Line is Partially Visible.";
rectangle(xwmin,ywmin,xwmax,ywmax);
//line(x1,y1,x2,y2);

//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

3) Midpoint Subdivision Line Clipping


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
typedef struct coordinate
{
}PT;
int x,y;
char code[4];
class midpoint
{
public:
void midsub(PT p1,PT p2); void drawwindow();
void drawline (PT p1,PT p2); PT setcode(PT p);
int visibility (PT p1,PT p2); PT resetendpt (PT p1,PT p2);
};
void midpoint::midsub(PT p1,PT p2)
{PT mid; int v;
p1=setcode(p1); p2=setcode(p2); v=visibility(p1,p2); switch(v)
{
case 0: drawline(p1,p2); break;
case 1: break;
case 2:
mid.x = p1.x + (p2.x-p1.x)/2;
mid.y = p1.y + (p2.y-p1.y)/2; midsub(p1,mid);
mid.x = mid.x+1; mid.y = mid.y+1; midsub(mid,p2); break;
}
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

outcode code = 0; if(y>ymax)


code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}};
void main( )
{
float x1,y1,x2,y2;
int gdriver = DETECT, gmode, n,poly[20],i; clrscr( );
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
//setbkcolor(WHITE); setcolor(BLUE);

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

drawpoly(n+1,poly); rectangle(xmin,ymin,xmax,ymax); getch( );


cleardevice( ); for(i=0;i<n;i++)
h.clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); getch( );
restorecrtmode( );
}
Output:

Das könnte Ihnen auch gefallen