All rights are reserved by Niteesh Kumar.. Theme images by Storman. Powered by Blogger.

Followers

Total Pageviews

Blog Archive

Follow by Email

Translate

Sunday, 9 April 2017

Program to implement 3D transformation-Translation

We require three dimensional geometric and co-ordinate transformation to change the view of a three dimensional object.Three dimensional transformations are extended from two dimensional transformations by considering the 'z' co-ordinate also in the plane.Just like two dimensional transformation in three dimensional transformations are formed by composing three basic transformations of translation, scaling and rotation.

Here we are going to discuss about the translation.

What is translation?

A translation transform simply moves every point by a certain amount horizontally and a certain amount vertically. If (x,y) is the original point and (x1,y1) is the transformed point, then the formula for a translation is-
                          x1=x+e
                          y1=y+f
e and f are translation factors.

In three dimensional co-ordinate system each vertex have (x,y,z). So, each vertex have three translation factors.But if we use this process we need to translate all the co-ordinates of object.For practical purpose we use the technique below.

So, first of all we need to draw a 3D object, here we are going to use bar3d function that is defined in graphics.h library of c.
  
       void bar3d(left,top,right,bottom,depth,topflag);

bar3d draws a three-dimensional rectangular bar by taking these parameters.
for translation of a rectangular box first we draw it using bar3d function and then will take the translation factor as input and again use bar3d function with the new left,top,right,bottom. 

C program for 3D translation is given below-


 
#include stdio .h
#include graphics .h
#include math .h

void trans();


//these are left,top,right,bottom parameters for bar3d function
int maxx,maxy,midx,midy;

void main()

{

            int ch;

            int gd=DETECT,gm;

            detectgraph(&gd,&gm);

            initgraph(&gd,&gm," ");
           
            trans();
return 0;
}

//function for translation of a 3d object
void trans()

{

            int x,y,z,o,x1,x2,y1,y2;

            midx=200;

            midy=200;

            //function to draw 3D rectangular box  
            bar3d(midx+50,midy-100,midx+100,midy-50,20,1);
  delay(1000);
            printf("Enter translation factor");

            scanf("%d%d",&x,&y);

            printf("After translation:");

            bar3d(midx+x+50,midy-(y+100),midx+x+100,midy-(y+50),20,1);

}
 
 
 OUTPUT- 
translation factors-(20,20)


0 on: "Program to implement 3D transformation-Translation"