CS2405 Anna University lab manual Composite 3D transformations - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Monday, June 20, 2011

CS2405 Anna University lab manual Composite 3D transformations


3D- TRANSFORMATION


AIM:
To perform 3D transformations such as translation, rotation and scaling.

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:

Step 1: Create a class cube with function draw cube.
Step 2: Use the function draw cube to draw a cube using eight points by means of
             functions line.
Step 3: Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data type int.
Step 4:Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.
Step 5: Initialize graphics functions.
Step 6: Input the first point in the cube.
Step 7: Input the size of the edge.
Step 8: Create an object to call the function.
Step 9: Using switch operation selects the operation to perform translation, rotation,    scaling.
Step 10: Translation
        a).input the translation vectortx,ty,tz.
        b).calculate points using formula
              x3[i]=x1[i]+tx.
              y3[i]=y1[i]+ty
              z3[i]=z1[i]+tz.
              x4[i]=x3[i]+z3[i]/2
              y4[i]=y3[i]+z3[i]/2
c).using the function line, display the object before and after translation.
Step11: Rotation:
        a). input the rotation angle
        b). using formula theta=(theta*3.14)/180
        c).input the direction in x,y,z axis
       d). if the direction is along x axis,
              x3[i]=x1[i].
            y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
            y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
            if the direction is along yaxis,
              y3[i]=y1[i].
            z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
            x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
             if the direction is along  z axis,
              z3[i]=z1[i].
            x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),
            y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),
       e).calculate the points using the formula
               x4[i]=x3[i]+z3[i]/2
               y4[i]=y3[i]+z3[i]/2
         f). using the function line,display the object before and after rotation.
Step12: Scaling:
       a).input the scaling factor and reference point
      b).calculate coordinates point using formula
           x3[i]=xf+(x1[i]*sx+xf*(1-sx),
           y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
           z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
     c). calculate the points using the formula
             x4[i]=x3[i]+z3[i]/2
             y4[i]=y3[i]+z3[i]/2
    d). using the function line, display the object before and after scaling.
Step13: Stop.

PROGRAM:

#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class cube
{
public:
void drawcube(int x1[],int y1[])
{
int i;
for(i=0;i<4;i++)
{
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[0],y1[0],x1[3],y1[3]);
}
 for(i=4;i<8;i++)
{
if(i<7)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[4],y1[4],x1[7],y1[7]);
}

for(i=0;i<4;i++)
{
line(x1[i],y1[i],x1[i+4],y1[i+4]);
}
}
};
void main()
{
int i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch,tx,ty,tz,sx,sy,sz,xf,yf,zf,x,y,z,size;
int driver=DETECT;
int mode;
initgraph(&driver,&mode,"");
cout<<"enter the points on the cube:";
cin>>x>>y>>z;
cout<<"enter the size of the edge:";
cin>>size;
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
x1[5]=x1[6]=x+size;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+size;
y1[4]=y1[5]=y;
y1[6]=y1[7]=y+size;
z1[1]=z1[2]=z1[3]=z1[0]=z ;
z1[4]=z1[5]=z1[6]=z1[7]=z-size;
for(i=0;i<8;i++)
{
x2[i]=x1[i]+z1[i]/2;
y2[i]=y1[i]+z1[i]/2;
}
cube c;
getch();
cleardevice();
do
{
cout<<"menu"<<endl;
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.exit\n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the translation vector:";
cin>>tx>>ty>>tz;
for(i=0;i<8;i++)
{
x3[i]=x1[i]+tx;
y3[i]=y1[i]+ty;
z3[i]=z1[i]+tz;
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before translation";
c.drawcube(x2,y2);
getch();
cleardevice();
 cout<<"after translation";
 c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle:";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the direction"<<endl;
cout<<"1.rotation about x axis"<<endl<<"2.rotation about y axis"<<endl<<"3.rotation about z axis";
cin>>op;
if(op==1)
{
for(i=0;i<8;i++)

{
x3[i]=x1[i];
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);
z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);
}
}
else
if(op==2)
{
for(i=0;i<8;i++)
{
y3[i]=y1[i];
x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);
x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);
}
}
else
if(op==3)
{
for(i=0;i<8;i++)
{
z3[i]=z1[i];
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);
y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);
}
}
else
cout<<"enter correct option";
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before rotation";
c.drawcube(x2,y2);
getch();
cleardevice();
 cout<<"after rotation";
 c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 3:
cout<<"enter the scaling factor:";
cin>>sx>>sy>>sz;
cout<<"enter the reference point:";
cin>>xf>>yf>>zf;
for(i=0;i<8;i++)
{
x3[i]=xf+(x1[i]*sx)+xf*(1-sx);
y3[i]=yf+(y1[i]*sy)+yf*(1-sy);
z3[i]=zf+(z1[i]*sz)+zf*(1-sz);
 }
 for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before scaling";
c.drawcube(x2,y2);
getch();
cleardevice();
 cout<<"after scaling";
 c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}
while(op!=4);
getch();
}


INPUT& OUTPUT:

Enter the point in the cube: 100 100 100
Enter the size of the edge: 50

Menu
1. translation
2. rotation
3. scaling
4. exit 



Enter the choice:1
Enter the translation vector: 5,10,15

Before translation                                 After translation
                                                           

RESULT:
             Thus the program is executed and verified.

No comments:

Post a Comment