## Monday, June 20, 2011

### CS2405 Anna University lab manual Three dimensional transformations - Translation, Rotation, Scaling

PARALLEL PROJECTION

AIM:
To write program to visualize the Parallel Projection of 3D images

FUNCTIONS USED:

Line()
The function line () is used to draw a line from(x1,y1)to (x2,y2)

Syntax:
line (x1,y1,x2,y2)

initgraph().
This function takes thee arguments and they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.

Syntax:
Initgraph(gd,gm,path)

ALGORITHM:

1. Create a class parallel.
2. Create a constructor parallel
i).initialize graphics
3. Create a function initialize
i).draw the x,y,z axis
4. Create a function projection
i).get the reference angle alpha as j.
ii).assign the value of k as 45.
iii).compute the following
fi=(3.14/180)*k;
a1=(3.14/180)*j;
z=pz1[1];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
iv).Calculate
px3[1]=px1[1]+i1;
py3[1]=py1[1]+i2;
px3[2]=px1[2]+i1;
py3[2]=py1[2]+i2;
px3[3]=px1[3]+i1;
py3[3]=py1[3]+i2;
px3[4]=px1[4]+i1;
py3[4]=py1[4]+i2;
v).compute the following
z=pz1[5];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
vi). calculate
px3[5]=px1[5]+i1;
py3[5]=py1[5]+i2;
px3[6]=px1[6]+i1;
py3[6]=py1[6]+i2;
px3[7]=px1[7]+i1;
py3[7]=py1[7]+i2;
px3[8]=px1[8]+i1;
py3[8]=py1[8]+i2;
vii).compute the values to screen coordinate value.
viii).join the coordinate using line function
ix).display the projected object.

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
class parallel
{
public:
int a,k;
int gd,gm;
int px[8],py[8],pz[8],px1[8],py1[8],pz1[8],px3[8],py3[8];
parallel();
void initialize();
void drawobj();
void proj(int);
};
parallel::parallel()
{
gd=DETECT;
initgraph(&gd,&gm,"");
}
void parallel::initialize()
{
px1[1]=100,py1[1]=100,pz1[1]=0;
px1[2]=200,py1[2]=100,pz1[2]=0;
px1[3]=200,py1[3]=200,pz1[3]=0;
px1[4]=100,py1[4]=200,pz1[4]=0;
px1[5]=100,py1[5]=100,pz1[5]=100;
px1[6]=200,py1[6]=100,pz1[6]=100;
px1[7]=200,py1[7]=200,pz1[7]=100;
px1[8]=100,py1[8]=200,pz1[8]=100;
}
void parallel::drawobj()
{
setcolor(WHITE);
line(px1[1],py1[1],px1[2],py1[2]);
line(px1[2],py1[2],px1[3],py1[3]);
line(px1[3],py1[3],px1[4],py1[4]);
line(px1[4],py1[4],px1[8]+50,py1[8]+50);
line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);
line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);
line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);
line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);
line(px1[1],py1[1],px1[5]+50,py1[5]+50);
line(px1[2],py1[2],px1[6]+50,py1[6]+50);
line(px1[3],py1[3],px1[7]+50,py1[7]+50);
line(px1[4],py1[4],px1[1],py1[1]);
getch();
}
void parallel::proj(int j)
{
cleardevice();
int z,xp,yp;
int dx,dy;
float a1,fi,i,i1,i2;
k=45;
fi=(3.14/180)*k;
a1=(3.14/180)*j;
z=pz1[1];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
px3[1]=px1[1]+i1;
py3[1]=py1[1]+i2;
px3[2]=px1[2]+i1;
py3[2]=py1[2]+i2;
px3[3]=px1[3]+i1;
py3[3]=py1[3]+i2;
px3[4]=px1[4]+i1;
py3[4]=py1[4]+i2;
z=pz1[5];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
px3[5]=px1[5]+i1;
py3[5]=py1[5]+i2;
px3[6]=px1[6]+i1;
py3[6]=py1[6]+i2;
px3[7]=px1[7]+i1;
py3[7]=py1[7]+i2;
px3[8]=px1[8]+i1;
py3[8]=py1[8]+i2;
cout<<"enter the projected object";
line(px3[1],py3[1],px3[2],py3[2]);
line(px3[2],py3[2],px3[3],py3[3]);
line(px3[3],py3[3],px3[4],py3[4]);
line(px3[4],py3[4],px3[1],py3[1]);
line(px3[5],py3[5],px3[6],py3[6]);
line(px3[6],py3[6],px3[7],py3[7]);
line(px3[7],py3[7],px3[8],py3[8]);
line(px3[8],py3[8],px3[5],py3[5]);
line(px3[1],py3[1],px3[5],py3[5]);
line(px3[2],py3[2],px3[6],py3[6]);
line(px3[3],py3[3],px3[7],py3[7]);
line(px3[4],py3[4],px3[8],py3[8]);
}
void main()
{
parallel p;
int a;
char c='y';
clrscr();
cleardevice();
while(c=='y'||c=='y')
{
cleardevice();
p.initialize();
p.drawobj();
cout<<"enter the reference angle\ny";
cin>>a;
p.proj(a);
cout<<"do u want to continue(y/n)";
cin>>c;
}
cleardevice();

OUTPUT:

Enter the reference angle:   180

Enter the projected object: do u want to continue(y/n)n

RESULT:
Thus the program is executed and verified.