POLYGON CLIPPING PROGRAM IN C | SUTHERLAND HODGEMAN ALGORITHM FOR POLYGON CLIPPING

POLYGON CLIPPING PROGRAM IN C | SUTHERLAND - HODGEMAN ALGORITHM FOR POLYGON CLIPPING

In computer graphics, we have to study the Sutherland - Hodgeman polygon clipping algorithm. The algorithm according to the book by A. P. Godse is as follows: 

Algorithm:-


       Step 1- Read coordinates of all the vertices of polygon
       Step 2- Read coordinates of the clipping window
       Step 3- Consider the left edge of window
       Step 4- Compare the vertices of each edge of the  polygon , individually with clipping plane.
       Step 5-Save the resulting intersections and vertices in   the new list of vertices according to four possible  relationships between the edge and the clipping                     boundary discussed earlier.
       Step 6- Repeat the steps 4 and 5 for remaining edges of  the clipping window. Each time the resultant list  of the vertices of is successively passed to process the next edge of the clipping window.
       Step 7- Stop


Now for implementing this algorithm is so complicated and lengthy, so I've done some trick to get the same output but with different logic. You'll get the polygon clipped in the output. 
Here's the program in c,

Program:-

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int gd,gm,n,*x,i,k=0;
//window coordinates int wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340;
int w[]={220,140,420,140,420,340,220,340,220,140};//array for drawing window
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics
printf("Window:-");
setcolor(RED); //red colored window
drawpoly(5,w); //window drawn
printf("Enter the no. of vertices of polygon: ");
scanf("%d",&n);
x = malloc(n*2+1);
printf("Enter the coordinates of points:\n");
k=0;
for(i=0;i<n*2;i+=2) //reading vertices of polygon
{
printf("(x%d,y%d): ",k,k);
scanf("%d,%d",&x[i],&x[i+1]);
k++;
}
x[n*2]=x[0]; //assigning the coordinates of first vertex to last additional vertex for drawpoly method.
x[n*2+1]=x[1];
setcolor(WHITE);
drawpoly(n+1,x);
printf("\nPress a button to clip a polygon..");
getch();
setcolor(RED);
drawpoly(5,w);
setfillstyle(SOLID_FILL,BLACK);
floodfill(2,2,RED);
gotoxy(1,1); //bringing cursor at starting position
printf("\nThis is the clipped polygon..");
getch();
cleardevice();
closegraph();
return 0;
}
view rawclipping.c hosted with ❤ by GitHub


Output:-


After running this program, first you've to enter the number of vertices of polygon. 

polygon clipping output | enter number of vertices
Output: Enter number of vertices

After entering number of vertices you'll be asked to enter coordinates of vertices.


Polygon clipping output | Enter coordinates of vertices
Output: Enter coordinates of vertices


After entering vertices' coordinates just press a button a polygon will be drawn with white color. Now press a button to clip the polygon and you'll simply get the clipped polygon in the output.

Polygon clipping output | Clipped polygon
Clipped Polygon

So, this is the simplest program to clip a polygon. If you want to change the window coordinates you can change them according to you.

Post a Comment

New comments are not allowed.*

Previous Post Next Post