# How To Write Source Code For Polygon Clipping POLYGON CLIPPING

To write a C program to implement polygon clipping.
Procedure Algorithm For Polygon Clipping
1. Get the minimum and maximum coordinates of both window and view port.
2. Get the number of sides of a polygon and its corresponding coordinates.
3. If the polygon lies within region code window, display it.
4. If any one of the polygon side is neither inside nor outside the boundary, find point of intersection and clip the regions that lies outside the boundary.
5. Display the polygon after clipping
Source Code Programming in C
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
typedef enum { left,right,bottom,top } edge;
#define N_EDGE 4
#define TRUE 1
#define FALSE 0
struct point
{
int x;
int y;
}p,wmin,wmax,p1,p2,ipt,i,pin[50],pout[50],first[50],s[50],i;
int inside(struct point p,int b,struct point wmin,struct point wmax)
{
switch(b)
{
case left:
if(p.x<wmin.x)
return (FALSE);
break;
case right:
if(p.x>wmax.x)
return (FALSE);
break;
case bottom:
if(p.y<wmin.y)
return (FALSE);
break;
case top:
if(p.y>wmax.y)
return (FALSE);
break;
}
return (TRUE);
}
int cross(struct point p1,struct point p2,int b,struct point wmin,struct point wmax)
{
if(inside(p1,b,wmin,wmax)==inside(p2,b,wmin,wmax))
return (FALSE);
else
return (TRUE);
}
struct point intersect(struct point p1,struct point p2,int b,struct point wmin,struct point wmax)
{
float m;
if(p1.x!=p2.x)
m=(p1.y-p2.y)/(p1.x-p2.x);
switch(b)
{
case left:
ipt.x=wmin.x;
ipt.y=p2.y+(wmin.x-p2.x)*m;
break;
case right:
ipt.x=wmax.x;
ipt.y=p2.y+(wmax.x-p2.x)*m;
break;
case bottom:
ipt.y=wmin.y;
if(p1.x!=p2.x)
ipt.x=p2.x+(wmin.y-p2.y)/m;
else
ipt.x=p2.x;
break;
case top:
ipt.y=wmax.y;
if(p1.x!=p2.x)
ipt.x=p2.x+(wmax.y-p2.y)/m;
else
ipt.x=p2.x;
break;
}
return(ipt);
}
void clippoint(struct point p,int b,struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point *first[],struct point *s)
{
if(!first[b])
first[b]=&p;
else
if(cross(p,s[b],b,wmin,wmax))
{
ipt=intersect(p,s[b],b,wmin,wmax);
if(b<top)
clippoint(ipt,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=ipt;
(*cnt)++;
}
}
s[b]=p;
if(inside(p,b,wmin,wmax))
if(b<top)
clippoint(p,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=p;
(*cnt)++;
}
}
void closeclip(struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point *first[],struct point *s)
{
int b;
for(b=left;b<=top;b++)
{
if(cross(s[b],*first[b],b,wmin,wmax))
{
i=intersect(s[b],*first[b],b,wmin,wmax);
if(b<top)
clippoint(i,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=i;
(*cnt)++;
}
}
}
}
int clippolygon(struct point wmin,struct point wmax,int n,struct point *pin,struct point *pout)
{
struct point *first[N_EDGE]={0,0,0,0},s[N_EDGE];
int i,cnt=0;
for(i=0;i<n;i++)
clippoint(pin[i],left,wmin,wmax,pout,&cnt,first,s);
closeclip(wmin,wmax,pout,&cnt,first,s);
return(cnt);
}
void main()
{
int c,gm,gr,n,j,np;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the window minimum coordinates");
scanf("%d%d",&wmin.x,&wmin.y);
printf("Enter the window max coordinates");
scanf("%d%d",&wmax.x,&wmax.y);
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
printf("Enter the no of sides in polygon:\n");
scanf("%d",&n);
printf("Enter the coordinates(x,y)for pin ,pout:\n");
for(j=0;j<n;j++)
{
scanf("%d%d",&pin[j].x,&pin[j].y);
scanf("%d%d",&pout[j].x,&pout[j].y);
}
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
for(j=0;j<n;j++)
{
if(j!=n-1)
line(pin[j].x,pin[j].y,pout[j].x,pout[j].y);
else
line(pin[j].x,pin[j].y,pout[j].x,pout[j].y);
}
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
printf("\n1.polygon clipping 2.exit");
scanf("%d",&c);
switch(c)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
np=clippolygon(wmin,wmax,n,pin,pout);
for(j=0;j<np;j++)
{
line(pout[j].x,pout[j].y,pout[(j+1)].x,pout[(j+1)].y);
}
break;
case 2:
exit(0);
}
getch();
}

Project Example Output
Enter the window minimum coordinates
200
200
Enter the window max coordinates
400
400
Enter the no of sides in polygon
3
Enter the coordinates(x,y)for pin ,pout
150
300
250
175
250
175
350
410
350
410
150
300
1.polygon clipping 2.exit
RESULT Thus the c program to implement polygon clipping was coded and executed successfully.