0% found this document useful (0 votes)
13 views8 pages

Practical 10

cgr

Uploaded by

mopopim241
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Practical 10

cgr

Uploaded by

mopopim241
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical No 10

Program for three dimensional transformations


(Translation & Scaling)

 Algorithm:
Step 1: Start the program.
Step 2: Input the object coordinates
Step 3: For Translation

a) Enter the translation factors tx,ty and tz.


b) Move the original coordinate position (x,y,z) to a new position
(x1,y1,z1).ie. x=x+x1, y=y+y1 and z=z+z1
c) Display the object after translation

Step 4: For Scaling


a) Input the scaled factors sx,sy and sz.
b) The transformed coordinates (x1,y1,z1) , x1=x.sx , y1=y.sy and
z1=z.sx.
c) Display the object after scaling

Step 5: Stop the Program.

 Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int x1=50,x2=200,y1=75,y2=125,tx,ty,n;
float sy,sx;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
bar3d(x1,y1,x2,y2,10,5);
printf("1)Translation\n2)Scaling\n");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter translation factor\n.");
scanf("%d%d",&tx,&ty);
x1=x1+tx;
x2=x2+tx;
y1=y1+ty;
y2=y2+ty;
outtextxy(x1-20,y1-20,"After Translation:");
bar3d(x1,y1,x2,y2,10,5);
break;
case 2:
printf("Enter scaling factor\n");
scanf("%f %f",&sx,&sy);
x1=x1*sx;
x2=x2*sx;
y1=y1*sy;
y2=y2*sy;
outtextxy(x1-20,y1-20,"After Scaling:");
bar3d(x1,y1,x2,y2,10,5);
break;
default:
printf("Invalid option");
break;
};
getch();
closegraph();
}
Outputs:
1) Translation:

2) Scaling:
 Practical related questions:

1) Write transformation matrix for 3D Translation.


Ans:

2) Write transformation matrix for 3D Scaling.


Ans:
3) What Z coordinate indicates in 3D transformations.
Ans:
In three dimensions, you need three numbers to specify a point. The third
coordinate is often called z. The z-axis is perpendicular to both the x-axis and the
y-axis.

4) Explain Homogeneous coordinates.


Ans :
Homogeneous coordinates will have some neutral applications in computer
graphics, they form the basis for geometry which is used extensively to display
three-dimensional objects on two-dimensional image planes. Homogeneous
coordinate provides a standard to perform certain standard operations on points in
euclidean space means matrix multiplication.

 Exercise :

1. Write a program to draw a cube in C by using 'bar3d' function. Translate the


cube by 75 units in X, 75 units in Y and 75 units in Z direction.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int gd,gm;
int tx=75,ty=75;
int x,y,x1,y1;
int depth=20;
int tx1,tx2,ty1,ty2;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
printf("Enter the coordinates of rectangle:");
scanf("%d %d %d %d",&x,&y,&x1,&y1);
printf("Enter the depth of rectangle to form cuboid\n");
scanf("%d",&depth);
setcolor(CYAN);
bar3d(x,y,x1,y1,depth,1);
tx1=x+tx;
tx2=x1+tx;
ty1=y+ty;
ty2=y1+ty;
setcolor(RED);
bar3d(tx1,ty1,tx2,ty2,depth,1);
getch();
closegraph();
return 0;
}

2. Write a program to draw a cube in C by using 'bar3d' function. Scale it to


double of its original size.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main()
{
int gd,gm;
int sx=2,sy=2;
int x,y,x1,y1;
int depth=20;
int sx1,sx2,sy1,sy2;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
printf("Enter the coordinates of rectangle:");
scanf("%d %d %d %d",&x,&y,&x1,&y1);
printf("Enter the depth of rectangle to form cuboid\n");
scanf("%d",&depth);
setcolor(CYAN);
bar3d(x,y,x1,y1,depth,1);
sx1=x*sx;
sx2=x1*sx;
sy1=y*sy;
sy2=y1*sy;
setcolor(RED);
bar3d(sx1,sy1,sx2,sy2,depth,1);
getch();
closegraph();
return 0;
}

You might also like