SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM:-

PROGRAM:

#include <graphics.h

#include <conio.h

#include <string.h

int minx, miny, maxx, maxy;

int mypoly[22] = {200,50,250,100,300,100,250,150,250,200,200,150,

150,200,150,150,100,100,150,100,200,50};

int newpoly[50];

int cutx, cuty;

void clippoly(int, int []);

void set_intersection(char *, int, int, int, int);

void main()

{

int gd=0, gm;

initgraph (&gd, &gm,"C:\\TCC\\BGI");

minx = 125; miny = 75; maxx=275; maxy=175;

rectangle(minx, miny, maxx, maxy);

getch();

drawpoly(11,mypoly);

getch();

clippoly(11, mypoly);

getch();

closegraph();

}

void clippoly(int edges, int mypoly[])

{

int temppoly[40];

int i, counter=0;

for (i=0; i<edges*2; i+=2)

{

// left clip...

if ( mypoly[i] > minx )

{

temppoly[counter++] = mypoly[i];

temppoly[counter++] = mypoly[i+1];

}

else

{

if ( i==0 )

{

set_intersection("left",mypoly[i],mypoly[i+1],mypoly[edges*2-4],mypoly[edges*2-3]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

set_intersection("left",mypoly[i],mypoly[i+1],mypoly[i+2],mypoly[i+3]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

}

else

{

set_intersection("left",mypoly[i-2],mypoly[i-1],mypoly[i],mypoly[i+1]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

set_intersection("left",mypoly[i],mypoly[i+1],mypoly[i+2],mypoly[i+3]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

}

}

}

edges = counter/2;

counter = 0;

for (i=0; i<edges*2; i+=2)

{

// right clip...

if ( temppoly[i] < maxx )

{

newpoly[counter++] = temppoly[i];

newpoly[counter++] = temppoly[i+1];

}

else

{

if ( i==0 )

{

set_intersection("right",temppoly[i],temppoly[i+1],temppoly[edges*2-4],temppoly[edges*2-3]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

set_intersection("right",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

}

else

{

set_intersection("right",temppoly[i-2],temppoly[i-1],temppoly[i],temppoly[i+1]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

set_intersection("right",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

}

}

}

edges = counter/2;

counter = 0;

for (i=0; i<edges*2; i+=2)

{

// bottom clip...

if ( newpoly[i+1] < maxy )

{

temppoly[counter++] = newpoly[i];

temppoly[counter++] = newpoly[i+1];

}

else

{

if ( i==0 )

{

set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[edges*2-4],newpoly[edges*2-3]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[i+2],newpoly[i+3]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

}

else

{

set_intersection("bottom",newpoly[i-2],newpoly[i-1],newpoly[i],newpoly[i+1]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[i+2],newpoly[i+3]);

temppoly[counter++] = cutx;

temppoly[counter++] = cuty;

}

}

}

edges = counter/2;

counter = 0;

for (i=0; i<edges*2; i+=2)

{

// top clip...

if ( temppoly[i+1] > miny )

{

newpoly[counter++] = temppoly[i];

newpoly[counter++] = temppoly[i+1];

}

else

{

if ( i==0 )

{

set_intersection("top",temppoly[i],temppoly[i+1],temppoly[edges*2-4],temppoly[edges*2-3]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

set_intersection("top",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

}

else

{

set_intersection("top",temppoly[i-2],temppoly[i-1],temppoly[i],temppoly[i+1]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

set_intersection("top",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);

newpoly[counter++] = cutx;

newpoly[counter++] = cuty;

}

}

}

cleardevice();

rectangle(minx, miny, maxx, maxy);

setcolor(2);

drawpoly(counter/2, newpoly);

setcolor(15);

getch();

drawpoly(11,mypoly);

}

void set_intersection(char * clipside, int x1, int y1, int x2, int y2)

{

float slope;

if ( x1 != x2 )

slope = (y2-y1)*1.0/(x2-x1);

if ( strcmp(clipside,"left")==0 )

{ cutx = minx; cuty = y1 + slope * ( minx - x1 ); }

if ( strcmp(clipside,"right")==0 )

{ cutx = maxx; cuty = y1 + slope * ( maxx - x1 ); }

if ( strcmp(clipside,"bottom")==0 )

{

if ( x1 != x2 )

cutx = x1 + ( maxy - y1 ) / slope;

else

cutx = x1;

cuty = maxy;

}

if ( strcmp(clipside,"top")==0 )

{

if ( x1 != x2 )

cutx = x1 + ( miny - y1 ) / slope;

else

cutx = x1;

cuty = miny;

}

}

OUTPUT:

2D LINE CLIPPING:

#include<stdio.h

#include<conio.h

#include<stdlib.h

#include<math.h

#include<graphics.h

float xumax,xumin,yumax,yumin;

float x1,y1,x2,y2,x3,y3,x4,y4,a,b,xmin,ymin,ymax,xmax,p,q;

int i,j,k,code[10];

void rejection(float,float);

void rcode(float,float,int c[10]);

void main()

{

int gd=DETECT,gm;

initgraph(gd,&gm,"c:\\tcc\\bgi");

printf("\n Enter the line co-ordinate");

scanf("%f%f%f%f",&x1,&y1,&x2,&y2);

printf("\n Enter the window Co_ordinate");

scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);

cleardevice();

printf("before clipping in window");

rectangle(xmin,ymin,xmax,ymax);

line(x1,y1,x2,y2);

getch();

cleardevice();

line(x1,y1,x2,y2);

rectangle(xmin,ymin,xmax,ymax);

getch();

cleardevice();

while(1)

{

j=1;

rcode(x1,y1,code);

j=5;

rcode(x2,y2,code);

for(k=1;k<=4;k++)

{

if((code[k]==1)&(code[k+4]==1))

rectangle(xmin,ymin,xmax,ymax);

if((code[k]==0)&(code[k+4]==0))

{

if(k>=3)

goto ch;

}

if(code[k]!=1)

{

if(code[k+4]==1)

{

rejection(x2,y2);

x2=a;

y2=b;

j=4;

rcode(x2,y2,code);

}

}

else

{

rejection(x1,y1);

x1=a;

y1=b;j=1;

rcode(x1,y1,code);

}

}

}

ch:

printf("\n line after clipping the window");

setcolor(12);

line(x1,y1,x2,y2);

setcolor(3);

rectangle(xmin,ymin,xmax,ymax);

getch();

cleardevice();

outtextxy(200,100,"window to view port transformation");

outtextxy(200,100,"view port");

printf("\n Enter the view port co_ordinate");

scanf("%f%f%f%f",&xumin,&yumin,&xumax,&yumax);

x3=xumin+(x1-xmin)*((xumax-xumin)/(xmax-xmin));

y3=yumin+(y1-ymin)*((yumax-yumin)/(ymax-ymin));

x4=xumin+(x2-xmin)*((xumax-xumin)/(xmax-xmin));

y4=yumin+(y2-ymin)*((yumax-yumin)/(ymax-ymin));

cleardevice();

outtextxy(200,100,"window to viewport transform");

setcolor(12);

rectangle(xumin,yumin,xumax,yumax);

line(x3,y3,x4,y4);

getch();

cleardevice();

closegraph();

}

void rcode(float x,float y,int c[10])

{

if(x<xmin)

c[j]=1;

else

c[j]=0;

j++;

if(x>xmax)

c[j]=1;

else

c[j]=0;

j++;

if(y<ymin)

c[j]=1;

else

c[j]=0;

j++;

if(y>ymax)

c[j]=1;

else

c[j]=0;

}

void rejection(float p,float q)

{

float m;

m=(y2-y2)/(x2-x1);

if((code[1]==1)||(code[5]==1))

{

q=q+(xmin-p)*m;

p=xmin;

}

else

{

if((code[2]==1)||(code[6]==1))

{

q=q+(xmax-p)*m;

p=xmax;

}

else

{

if((code[3]==1)||(code[7]==1))

{

p=p+(ymin-q)/m;

q=ymax;

}

else

{

if((code[4]==1)||(code[8]==1))

{

p=p+(ymax-q)/m;

q=ymax;

}

}

}

}

a=p;

b=q;

}

OUTPUT:

Enter the line co ordinate 145

156

234

287

Enter window co ordinate 178

198

298

300

Before clipping in window line after clipping the window

Window to viewport transformation viewport

Enter the viewport coordination 150 160 250 260

Window to viewport transformation

3D TRANSFORMATIONS

Program:

#include<stdio.h
#include<conio.h
#include<graphics.h
#include<math.h
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,ang,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:/TC/BGI");
setfillstyle(3,25);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
outtextxy(100,100,"ORIGINAL OBJECT");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
axis();
outtextxy(100,20,"TRANSLATION");
printf("\n\n Enter the Translation vector: ");
scanf("%d%d",&x,&y);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x+100),midy-(y+20),midx+(x+60),midy-(y+90),20,5);
axis();
outtextxy(100,20,"SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x*100),midy-(y*20),midx+(x*60),midy-(y*90),20*z,5);
axis();
outtextxy(100,20,"ROTATION");
printf("\n Enter the Rotation angle: ");
scanf("%d",&ang);
x1=100*cos(ang*3.14/180)-20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.14/180);
x2=60*cos(ang*3.14/180)-90*sin(ang*3.14/180);
y2=60*sin(ang*3.14/180)+90*sin(ang*3.14/180);
axis();
printf("\n After rotating about z-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,20,5);
axis();
printf("\n After rotating about x-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+100,midy-x1,midx+60,midy-x2,20,5);
axis();
printf("\n After rotating about y-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-20,midx+x2,midy-90,20,5);
axis();
closegraph();
}

OUTPUT: