COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
1. PROGRAM NO. 9
AIM: Write a program for 3d transformation.
SOLUTION:This Program is draw the three dimensional image and used to perform
different graphics function on the input image such as rotation, translation, scaling etc.
SOFTWARE AND TOOLS REQUIRED: A laptop with opengl installed.
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
2. c).input the dir ection 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.
3. Source code:
Aim: #include <math.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
typedef float Matrix4x4 [4][4];
Matrix4x4 theMatrix;
float ptsIni[8][3]={{80,80,-100},{180,80,-100},{180,180,-100},{80,180,-100},
{60,60,0},{160,60,0},{160,160,0},{60,160,0}};
float ptsFin[8][3];
float refptX,refptY,refptZ;
float TransDistX,TransDistY,TransDistZ;
float ScaleX,ScaleY,ScaleZ;
float Alpha,Beta,Gamma,Theta;
float A,B,C;
float aa,bb,cc;
float x1,y11,z1,x2,y2,z2;
int choice,choiceRot,choiceRef;
void matrixSetIdentity(Matrix4x4 m)
{
int i, j;
for (i=0; i<4; i++)
for (j=0; j<4; j++)
m[i][j] = (i == j);
}
void matrixPreMultiply(Matrix4x4 a , Matrix4x4 b)
{
int i,j;
4. Matrix4x4 tmp;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
tmp[i][j]=a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][j]+a[i][3]*b[3][j];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
theMatrix[i][j] = tmp[i][j];
}
void Translate(int tx, int ty, int tz)
{
Matrix4x4 m;
matrixSetIdentity(m);
m[0][3] = tx;
m[1][3] = ty;
m[2][3] = tz;
matrixPreMultiply(m, theMatrix);
}
void Scale(float sx , float sy ,float sz)
{
Matrix4x4 m;
matrixSetIdentity(m);
m[0][0] = sx;
m[0][3] = (1 - sx)*refptX;
m[1][1] = sy;
m[1][3] = (1 - sy)*refptY;
m[2][2] = sz;
m[2][3] = (1 - sy)*refptZ;
matrixPreMultiply(m , theMatrix);
12. }
TransformPoints();
Draw(ptsFin);
glFlush();
}
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 1.0);
glOrtho(-454.0, 454.0, -250.0, 250.0, -250.0, 250.0);
glEnable(GL_DEPTH_TEST);
}
int main (int argc, char *argv)
{
glutInit(&argc, &argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (1362, 750);
glutInitWindowPosition (0, 0);
glutCreateWindow (" Basic Transformations ");
init ();
printf("Enter your choice number:n1.Translationn2.Scalingn3.Rotationn4.Reflectionn=>");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter Translation along X, Y & Zn=>");
scanf("%f%f%f",&TransDistX, &TransDistY , &TransDistZ);
break;
case 2:printf("Enter Scaling ratios along X, Y & Zn=>");
scanf("%f%f%f",&ScaleX , &ScaleY , &ScaleZ);
13. break;
case 3:printf("Enter your choice for Rotation about axis:n1.parallel
to X-axis.(y=B & z=C)n2.parallel to Y-axis.(x=A & z=C)n3.parallel to
Z-axis.(x=A & y=B)n4.Arbitrary line passing through (x1,y1,z1) &
(x2,y2,z2)n =>");
//Realign above line while execution
scanf("%d",&choiceRot);
switch(choiceRot)
{
case 1: printf("Enter B & C: ");
scanf("%f %f",&B,&C);
printf("Enter Rot. Angle Alpha: ");
scanf("%f",&Alpha);
break;
case 2: printf("Enter A & C: ");
scanf("%f %f",&A,&C);
printf("Enter Rot. Angle Beta: ");
scanf("%f",&Beta);
break;
case 3: printf("Enter A & B: ");
scanf("%f %f",&A,&B);
printf("Enter Rot. Angle Gamma: ");
scanf("%f",&Gamma);
break;
case 4: printf("Enter values of x1 ,y1 & z1:n");
scanf("%f %f %f",&x1,&y11,&z1);
printf("Enter values of x2 ,y2 & z2:n");
scanf("%f %f %f",&x2,&y2,&z2);
14. printf("Enter Rot. Angle Theta: ");
scanf("%f",&Theta);
break;
}
break;
case 4: printf("Enter your choice for reflection about plane:n1.X-Yn2.Y-Zn3.X-Zn=>");
scanf("%d",&choiceRef);
break;
default: printf("Please enter a valid choice!!!n");
return 0;
}
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
OUTPUT:
1 . Translation
2 . Rotation
15. 3 . Scaling
4 . Reflection
Program NO.10
AIM: write a program to perform3D projection.
16. SOLUTION:3D projection is any method of mapping three-dimensional points to a two-
dimensional plane. As most current methods for displaying graphical data are based on
planar two-dimensional media, the use of this type of projection is widespread, especially in
computer graphics, engineering and drafting.
SOFTWARE AND TOOLS REQUIRED: A laptop with c compiler installed.
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;
17. 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
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
int ARRAY_SIZE;
int array_max(int *arr, int returnIndex);
int array_min(int *arr, int returnIndex);
void draw3d(int sides, int *x, int *y, int depth);
void drawFrontView(int sides, int *x, int *y);
void drawSideView(int sides, int *x, int *y, int depth);
18. void drawTopView(int sides,int *x,int *y, int depth);
void drawAxis(int *x, int *y, int depth);
int array_max(int *arr, int returnIndex){
int i, max = arr[0], index=0;
for(i=1;i<ARRAY_SIZE;i++){
if(arr[i]>max)
{
max=arr[i];
index=i;
}
}
return returnIndex?index:max;
}
int array_min(int *arr, int returnIndex){
int i, min = arr[0], index=0;
for(i=1;i<ARRAY_SIZE;i++){
if(arr[i]<min)
{
min=arr[i];
index=i;
}
}
return returnIndex?index:min;
}
void drawAxis(int *x, int *y, int depth){
int maxy, minx;
maxy = array_max(y,0);