You are on page 1of 7

PROGRAM FOR POLYGON CLIPPING USING SUTHERLAND-

HODGEMAN ALGORITHM 

#include <iostream.h>

#include <conio.h>

#include <graphics.h>

//#include "graph.h"

//#include "illegal.h" //debugging purposes...

#define MAXVERTICES 11

#define MAXCORDINATES ((MAXVERTICES * 2) + 1)

int corArray[MAXCORDINATES],nVertices=0;

int corArray2[MAXCORDINATES],nVertices2=0;

int wx_min,wx_max,wy_min,wy_max;

enum boundry {left=0,right,bottom,top};

void initialize()

int gDriver = DETECT,gMode;

initgraph(&gDriver,&gMode,"");

cleardevice();

void inputPolygon()

cout<<"\nEnter No of Vertices (Min. 3,Max. "<<MAXVERTICES<<"): ";

cin>>nVertices;

if(nVertices < 3 || nVertices > MAXVERTICES) nVertices = 3;


for(int i=0;i<nVertices;i++)

cout<<"\nEnter Cordinates of Vertex "<<i+1<<" (x,y) : ";

cin>>corArray[2*i]>>corArray[(2*i)+1];

corArray[2*(nVertices)] = corArray[0];

corArray[(2*(nVertices))+1] = corArray[1];

void samplePolygon()

int midx = getmaxx()/2;

int midy = getmaxy()/2;

int polygon_vertices[]={ midx,midy-125,

midx-200,midy-50,

midx-75,midy-50,

midx-75,midy+200,

midx+75,midy+200,

midx+75,midy-50,

midx+200,midy-50,

midx,midy-125};

nVertices = 8;

for(int i = 0;i<nVertices*2;i++)

corArray[i] = polygon_vertices[i];

int insideWindow(int x,int y,boundry b)


{

switch(b)

case left:

return (x > wx_min);

case right:

return (x < wx_max);

case bottom:

return (y < wy_min);

case top:

return (y > wy_max);

return 0;

int calcCondition(int x1,int y1,int x2,int y2,boundry b)

int stat1 = insideWindow(x1,y1,b);

int stat2 = insideWindow(x2,y2,b);

if(!stat1 && stat2) return 1;

if(stat1 && stat2) return 2;

if(stat1 && !stat2) return 3;

if(!stat1 && !stat2) return 4;

return 0; //never executed

void solveIntersection(boundry b,int x1,int y1,int x2,int y2,int &x,int &y)


{

float m = 0;

if(x2 != x1) m = ((float)(y2-y1)/(float)(x2-x1));

switch(b)

case left:

x = wx_min;

y = y1 + m * (x - x1);

break;

case right:

x = wx_max;

y = y1 + m * (x - x1);

break;

case bottom:

y = wy_min;

if(x1 != x2)

x = x1 + (float)(1/m) * (y - y1);

else

x = x1;

break;

case top:

y = wy_max;

if(x1 != x2)

x = x1 + (float)(1/m) * (y - y1);

else

x = x1;

break;
}

void addNewPoint(int x,int y)

corArray2[(2 * nVertices2)] = x;

corArray2[(2 * nVertices2)+1] = y;

nVertices2++;

void clipPolygon()

int j,k;

boundry i;

for(i=left;i<=top;i++)

nVertices2 = 0;

for(j=0;j<nVertices-1;j++)

int x1 = corArray[(2*j)];

int y1 = corArray[(2*j)+1];

int x2 = corArray[(2*(j+1))];

int y2 = corArray[((2*(j+1))+1)];

int condition = calcCondition(x1,y1,x2,y2,i);

int _x=0,_y=0;

switch(condition)
{

case 1:

solveIntersection(i,x1,y1,x2,y2,_x,_y);

addNewPoint(_x,_y);

addNewPoint(x2,y2);

break;

case 2:

addNewPoint(x2,y2);

break;

case 3:

solveIntersection(i,x1,y1,x2,y2,_x,_y);

addNewPoint(_x,_y);

break;

case 4:

break;

addNewPoint(corArray2[0],corArray2[1]);

//update corArray and nvertices...

nVertices = nVertices2;

for(j=0;j<nVertices*2;j++)

corArray[j] = corArray2[j];

void main()
{

clrscr();

initialize();

// inputPolygon();

samplePolygon();

// cout<<"\nEnter Window Parameters (wxmin,wxmax,wymin,wymax) : ";

// cin>>wx_min>>wx_min>>wy_min>>wy_max;

wx_min = getmaxx()/2 - 100;

wx_max = getmaxx()/2 + 100;

wy_min = getmaxy()/2 + 100;

wy_max = getmaxy()/2 - 100;

rectangle(wx_min,wy_max,wx_max,wy_min);

drawpoly(nVertices+1,corArray);

getch();

clipPolygon();

clrscr();

cleardevice();

rectangle(wx_min,wy_max,wx_max,wy_min);

drawpoly(nVertices+1,corArray);

getch();

You might also like