Professional Documents
Culture Documents
Graphics Lab File - After Mid Sem
Graphics Lab File - After Mid Sem
Mid-semester
Examinations
1
Table of Contents
2
Program 1: Three-Dimensional Transformations
/*********************************************************************
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: 3D-Transformations
*********************************************************************/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int xp[2],yp[2],z;
void display();
void translate();
void scaling();
void rotation();
void matrixmul(int [4][4]);
void main()
{
int gd=DETECT,gm;
int ch,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<2;i++)
{
printf("\nEnter X-coordinate of vertex %d : ",i+1);
scanf("%d",&xp[i]);
printf("\nEnter Y-coordinate of vertex %d : ",i+1);
scanf("%d",&yp[i]);
}
printf("\nEnter The Z-axis For 3d Figure : ");
scanf("%d",&z);
clrscr();
cleardevice();
display(xp,yp);
getche();
do
{
printf("----- MENU -----");
printf("\n1.TRANSLATION.");
printf("\t2.SCALING.");
printf("\n3.ROTATION.");
printf("\t4.EXIT.");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
3
clrscr();
cleardevice();
display(xp,yp);
switch(ch)
{
case 1 : translate();
break;
case 2 : scaling();
break;
case 3 : rotation();
break;
case 4 : exit(0);
default:
outtextxy(1,66,"-PLEASE SELECT THE RIGHT OPTION-");
}
}
while(ch!=4);
getch();
closegraph();
}
void translate()
{
int p[4][4];
int tx,ty,tz,i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Translating Factor tx : ");
scanf("%d",&tx);
printf("\nEnter The Translating Factor ty : ");
scanf("%d",&ty);
printf("\nEnter The Translating Factor tz : ");
scanf("%d",&tz);
clrscr();
cleardevice();
display();
p[0][3]=tx;
p[1][3]=ty;
p[2][3]=tz;
matrixmul(p);
}
void scaling()
{
int p[4][4];
int sx,sy,sz,i,j;
4
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Scaling Factor sx : ");
scanf("%d",&sx);
printf("\nEnter The Scaling Factor sy : ");
scanf("%d",&sy);
printf("\nEnter The Scaling Factor sz : ");
scanf("%d",&sz);
if(sx==0)
sx=1;
if(sy==0)
sy=1;
if(sz==0)
sz=1;
clrscr();
cleardevice();
p[0][0]=sx;
p[1][1]=sy;
p[2][2]=sz;
p[3][3]=1;
matrixmul(p);
}
void rotation()
{
float res[4][1],p[4][4],t[4][1];
int ang,i,j,k,l,rch;
float rad;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Rotating Angle : ");
scanf("%d",?);
rad=ang*0.0174;
printf("\nChoose the axis of roration ");
printf("\n1.X-axis");
printf("\n2.Y-axis");
printf("\n3.Z-axis");
printf("\nEnter Your Choice : ");
scanf("%d",&rch);
switch(rch)
{
case 1 : p[1][1]=cos(rad);
p[1][2]=(-1)*sin(rad);
p[2][1]=sin(rad);
p[2][2]=cos(rad);
break;
case 2 :
p[0][0]=cos(rad);
5
p[2][0]=(-1)*sin(rad);
p[0][2]=sin(rad);
p[2][2]=cos(rad);
break;
case 3 :
p[0][0]=cos(rad);
p[0][1]=(-1)*sin(rad);
p[1][0]=sin(rad);
p[1][1]=cos(rad);
break;
default :
printf("\nInvalid Choice !");
}
clrscr();
cleardevice();
for(i=0;i<2;i++)
{
t[0][0]=xp[i];
t[1][0]=yp[i];
t[2][0]=z;
t[3][0]=1;
for(j=0;j<4;j++)
{
for(k=0;k<1;k++)
{
res[j][k]=0;
for(l=0;l<4;l++)
{
res[j][k]=res[j][k]+(p[j][l]*t[l][k]);
}
}
}
xp[i]=res[0][0];
yp[i]=res[1][0];
z=res[2][0];
}
display(xp,yp);
}
6
rectangle(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+xp[1],getmaxy()/2-yp[1]);
if(z>=xp[0])
{
x3=z+xp[0];
y3=z+yp[0];
x4=z+xp[1];
y4=z+yp[1];
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
else
{
x3=xp[0]-z;
y3=yp[0]-z;
x4=xp[1]-z;
y4=yp[1]-z;
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
}
7
z=res[2][0];
display(xp,yp);
}
8
rectangle(x[1],y[1],x[2],y[2]);
for(int i=1;i<=n;i++)
if((a[i]<=x[2]&&a[i]>=x[i]&&b[i]<=y[2]&&b[i]>=y[i]))
putpixel(a[i],b[i],15);
getch();
closegraph();
}
void main()
{
clip.drawrect();
clip.getpixeldata();
clip.result();
}
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do{
if(!(code0 | code1))
{ accept =1 ; done =1; }
else
if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
9
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
} while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
10
return(code);
}
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
11
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) || ( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) || ( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
12
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
13
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: Sutherland Hodgeman
*********************************************************************/
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#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;
void initialize()
{
int gDriver = DETECT,gMode;
initgraph(&gDriver,&gMode,"C:\\tc\\bgi");
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++)
14
corArray[i] = polygon_vertices[i];
}
15
case top:
y = wy_max;
if(x1 != x2)
x = x1 + (float)(1/m) * (y - y1);
else
x = x1;
break;
}
}
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;
}
}
16
addNewPoint(corArray2[0],corArray2[1]);
void main()
{
clrscr();
initialize();
inputPolygon();
samplePolygon();
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();
}
main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
17
int xp, yp, zp, x, y, z;
detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:\\tc\\bgi");
ymax = getmaxy();
xp = 300; yp = 320; zp = 100;
setcolor(7);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
18
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1, ymax-y1, x2, ymax-y2);
}
getch();
}
main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
double L1,phi;
par[0][0] = 1; par[0][1] = 0;
par[0][2] = L1*cos(phi); par[0][3] = 0;
par[1][0] = 0; par[1][1] = 1;
par[1][2] = L1*sin(phi); par[1][3] = 0;
par[2][0] = 0; par[2][1] = 0;
par[2][2] = 0; par[2][3] = 0;
par[3][0] = 0; par[3][1] = 0;
par[3][2] = 0; par[3][3] = 1;
19
m=4; n=4; p=8;
detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:\\tc\\bgi");
ymax = getmaxy();
for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1,ymax-y1,x2,ymax-y2);
setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1,ymax-y1,x2,ymax-y2);
setcolor(13);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1,ymax-y1,x2,ymax-y2);
}
getch();
}
20