SlideShare a Scribd company logo
1 of 23
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
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.
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;
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);
}
void RotateX(float angle)
{
Matrix4x4 m;
matrixSetIdentity(m);
angle = angle*22/1260;
m[1][1] = cos(angle);
m[1][2] = -sin(angle);
m[2][1] = sin(angle);
m[2][2] = cos(angle);
matrixPreMultiply(m , theMatrix);
}
void RotateY(float angle)
{
Matrix4x4 m;
matrixSetIdentity(m);
angle = angle*22/1260;
m[0][0] = cos(angle);
m[0][2] = sin(angle);
m[2][0] = -sin(angle);
m[2][2] = cos(angle);
matrixPreMultiply(m , theMatrix);
}
void RotateZ(float angle)
{
Matrix4x4 m;
matrixSetIdentity(m);
angle = angle*22/1260;
m[0][0] = cos(angle);
m[0][1] = -sin(angle);
m[1][0] = sin(angle);
m[1][1] = cos(angle);
matrixPreMultiply(m , theMatrix);
}
void Reflect(void)
{
Matrix4x4 m;
matrixSetIdentity(m);
switch(choiceRef)
{
case 1: m[2][2] = -1;
break;
case 2: m[0][0] = -1;
break;
case 3: m[1][1] = -1;
break;
}
matrixPreMultiply(m , theMatrix);
}
void DrawRotLine(void)
{
switch(choiceRot)
{
case 1: glBegin(GL_LINES);
glVertex3s(-1000 ,B,C);
glVertex3s( 1000 ,B,C);
glEnd();
break;
case 2: glBegin(GL_LINES);
glVertex3s(A ,-1000 ,C);
glVertex3s(A ,1000 ,C);
glEnd();
break;
case 3: glBegin(GL_LINES);
glVertex3s(A ,B ,-1000);
glVertex3s(A ,B ,1000);
glEnd();
break;
case 4: glBegin(GL_LINES);
glVertex3s(x1-aa*500 ,y11-bb*500 , z1-cc*500);
glVertex3s(x2+aa*500 ,y2+bb*500 , z2+cc*500);
glEnd();
break;
}
}
void TransformPoints(void)
{
int i,k;
float tmp ;
for(k=0 ; k<8 ; k++)
for (i=0 ; i<3 ; i++)
ptsFin[k][i] = theMatrix[i][0]*ptsIni[k][0] + theMatrix[i][1]*ptsIni[k][1]
+ theMatrix[i][2]*ptsIni[k][2] + theMatrix[i][3];
}
void Axes(void)
{
glColor3f (0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2s(-1000 ,0);
glVertex2s( 1000 ,0);
glEnd();
glBegin(GL_LINES);
glVertex2s(0 ,-1000);
glVertex2s(0 , 1000);
glEnd();
}
void Draw(float a[8][3])
{
int i;
glColor3f (0.7, 0.4, 0.7);
glBegin(GL_POLYGON);
glVertex3f(a[0][0],a[0][1],a[0][2]);
glVertex3f(a[1][0],a[1][1],a[1][2]);
glVertex3f(a[2][0],a[2][1],a[2][2]);
glVertex3f(a[3][0],a[3][1],a[3][2]);
glEnd();
i=0;
glColor3f (0.8, 0.6, 0.5);
glBegin(GL_POLYGON);
glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]);
glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]);
glEnd();
glColor3f (0.2, 0.4, 0.7);
glBegin(GL_POLYGON);
glVertex3f(a[0][0],a[0][1],a[0][2]);
glVertex3f(a[3][0],a[3][1],a[3][2]);
glVertex3f(a[7][0],a[7][1],a[7][2]);
glVertex3f(a[4][0],a[4][1],a[4][2]);
glEnd();
i=1;
glColor3f (0.5, 0.4, 0.3);
glBegin(GL_POLYGON);
glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]);
glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]);
glEnd();
i=2;
glColor3f (0.5, 0.6, 0.2);
glBegin(GL_POLYGON);
glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]);
glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]);
glEnd();
i=4;
glColor3f (0.7, 0.3, 0.4);
glBegin(GL_POLYGON);
glVertex3f(a[0+i][0],a[0+i][1],a[0+i][2]);
glVertex3f(a[1+i][0],a[1+i][1],a[1+i][2]);
glVertex3f(a[2+i][0],a[2+i][1],a[2+i][2]);
glVertex3f(a[3+i][0],a[3+i][1],a[3+i][2]);
glEnd();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Axes();
glColor3f (1.0, 0.0, 0.0);
Draw(ptsIni);
matrixSetIdentity(theMatrix);
switch(choice)
{
case 1: Translate(TransDistX, TransDistY ,TransDistZ);
break;
case 2: Scale(ScaleX, ScaleY, ScaleZ);
break;
case 3: switch(choiceRot)
{
case 1: DrawRotLine();
Translate(0,-B,-C);
RotateX(Alpha);
Translate(0,B,C);
break;
case 2: DrawRotLine();
Translate(-A,0,-C);
RotateY(Beta);
Translate(A,0,C);
break;
case 3: DrawRotLine();
Translate(-A,-B,0);
RotateZ(Gamma);
Translate(A,B,0);
break;
case 4: DrawRotLine();
float MOD =sqrt((x2-x1)*(x2-x1) + (y2-y11)*(y2-y11) + (z2-z1)*(z2-z1));
aa = (x2-x1)/MOD;
bb = (y2-y11)/MOD;
cc = (z2-z1)/MOD;
Translate(-x1,-y11,-z1);
float ThetaDash;
ThetaDash = 1260*atan(bb/cc)/22;
RotateX(ThetaDash);
RotateY(1260*asin(-aa)/22);
RotateZ(Theta);
RotateY(1260*asin(aa)/22);
RotateX(-ThetaDash);
Translate(x1,y11,z1);
break;
}
break;
case 4: Reflect();
break;
}
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);
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);
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
3 . Scaling
4 . Reflection
Program NO.10
AIM: write a program to perform3D projection.
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;
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);
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);
minx = array_min(x,0);
setcolor(BLUE);
line(minx+depth, maxy-depth, -depth, getmaxy());
line(minx+depth, maxy-depth, getmaxx(), maxy-depth);
line(minx+depth, maxy-depth, minx+depth, 0);
setlinestyle(0,1,2);
setfillstyle(CLOSE_DOT_FILL, WHITE);
floodfill(0,0,BLUE);
setfillstyle(CLOSE_DOT_FILL, BROWN);
floodfill(getmaxx(),0,BLUE);
setfillstyle(CLOSE_DOT_FILL, DARKGRAY);
floodfill(getmaxx(),getmaxy(),BLUE);
}
void draw3d(int sides,int *x,int *y,int depth)
{
int i,j,k=0;
setlinestyle(0,1,1);
drawAxis(x, y, depth);
setcolor(YELLOW);
for(j=0;j<2;j++)
{
for(i=0;i<sides;i++)
{
if(i!=sides-1)
line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
else
line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
if(j==0)
line(x[i],y[i],x[i]+depth,y[i]-depth);
}
k=depth;
}
}
void drawFrontView(int sides,int *x,int *y)
{
int i;
setlinestyle(0,1,1);
setcolor(LIGHTGREEN);
for(i=0;i<sides;i++)
{
if(i!=sides-1)
line(x[i],y[i],x[i+1],y[i+1]);
else
line(x[i],y[i],x[0],y[0]);
}
}
void drawSideView(int sides,int *x,int *y, int depth)
{
int i, maxyIndex, minyIndex, maxxIndex;
maxxIndex=array_max(x,1);
maxyIndex=array_max(y,1);
minyIndex=array_min(y,1);
setcolor(LIGHTGREEN);
for(i=0;i<sides-1;i++)
{
if(y[i]<y[maxyIndex] && y[i]>y[minyIndex] && x[i]<x[maxxIndex])
setlinestyle(3,1,1);
else
setlinestyle(0,1,1);
line(x[0],y[i],x[0],y[i+1]);
line(x[0]+depth*2,y[i],x[0]+depth*2,y[i+1]);
line(x[0],y[i],x[0]+depth*2,y[i]);
line(x[0],y[i+1],x[0]+depth*2,y[i+1]);
}
}
void drawTopView(int sides, int *x, int *y, int depth)
{
int i, minyIndex, maxxIndex, minxIndex;
maxxIndex=array_max(x,1);
minxIndex=array_min(x,1);
minyIndex=array_min(y,1);
setcolor(LIGHTGREEN);
for(i=0;i<sides-1;i++)
{
if(x[i]<x[maxxIndex] && x[i]>x[minxIndex] && y[i]>y[minyIndex])
setlinestyle(3,1,1);
else
setlinestyle(0,1,1);
line(x[i],y[0],x[i+1],y[0]);
line(x[i],y[0]+depth * 2,x[i+1],y[0]+depth*2);
line(x[i],y[0],x[i],y[0]+depth*2);
line(x[i+1],y[0],x[i+1],y[0]+depth*2);
}
}
void main(){
int gd=0,gm;
int x[20],y[20],i,sides,depth,key;
initgraph(&gd,&gm,"");
printf("No of sides(front view only) : ");
scanf("%d",&sides);
printf("Co-ordinates : ");
for(i=0;i<sides;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
printf("Depth :");
scanf("%d",&depth);
ARRAY_SIZE=sides;
while(1){
key=getche();
clrscr();
cleardevice();
printf("LEFT ARROW -> 3D ViewnUP ARROW - > Front ViewnDOWN ARROW -> Top
ViewnRIGHT ARROW -> Side ViewnESC -> Exit");
switch(key){
case 75: //left arrow
draw3d(sides,x,y,depth);
break;
case 72: //up arrow
drawFrontView(sides,x,y);
break;
case 77: //right arrow
drawSideView(sides,x,y,depth);
break;
case 80: //down arrow
drawTopView(sides,x,y,depth);
break;
case 27: //esc
exit(0);
break;
}
}
OUTPUT:

More Related Content

Similar to COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx

Mobile Game and Application with J2ME - Collision Detection
Mobile Gameand Application withJ2ME  - Collision DetectionMobile Gameand Application withJ2ME  - Collision Detection
Mobile Game and Application with J2ME - Collision DetectionJenchoke Tachagomain
 
Mobile Game and Application with J2ME
Mobile Gameand Application with J2MEMobile Gameand Application with J2ME
Mobile Game and Application with J2MEJenchoke Tachagomain
 
Cocos2d Performance Tips
Cocos2d Performance TipsCocos2d Performance Tips
Cocos2d Performance TipsKeisuke Hata
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?jonbodner
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Jay Coskey
 
Robotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsRobotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsTravis Heidrich
 
Coscup2021 - useful abstractions at rust and it's practical usage
Coscup2021 - useful abstractions at rust and it's practical usageCoscup2021 - useful abstractions at rust and it's practical usage
Coscup2021 - useful abstractions at rust and it's practical usageWayne Tsai
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscationguest9006ab
 
Psimd open64 workshop_2012-new
Psimd open64 workshop_2012-newPsimd open64 workshop_2012-new
Psimd open64 workshop_2012-newdibyendu_das0708
 
Halide tutorial 2019
Halide tutorial 2019Halide tutorial 2019
Halide tutorial 2019Champ Yen
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderEduardo Lundgren
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov
 
Idea for ineractive programming language
Idea for ineractive programming languageIdea for ineractive programming language
Idea for ineractive programming languageLincoln Hannah
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptxMouDhara1
 
Coscup2021-rust-toturial
Coscup2021-rust-toturialCoscup2021-rust-toturial
Coscup2021-rust-toturialWayne Tsai
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graphkinan keshkeh
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docxjosies1
 

Similar to COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx (20)

Mobile Game and Application with J2ME - Collision Detection
Mobile Gameand Application withJ2ME  - Collision DetectionMobile Gameand Application withJ2ME  - Collision Detection
Mobile Game and Application with J2ME - Collision Detection
 
Mobile Game and Application with J2ME
Mobile Gameand Application with J2MEMobile Gameand Application with J2ME
Mobile Game and Application with J2ME
 
Cocos2d Performance Tips
Cocos2d Performance TipsCocos2d Performance Tips
Cocos2d Performance Tips
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
 
Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3Intro to Python (High School) Unit #3
Intro to Python (High School) Unit #3
 
Robotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsRobotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic Joints
 
Coscup2021 - useful abstractions at rust and it's practical usage
Coscup2021 - useful abstractions at rust and it's practical usageCoscup2021 - useful abstractions at rust and it's practical usage
Coscup2021 - useful abstractions at rust and it's practical usage
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Psimd open64 workshop_2012-new
Psimd open64 workshop_2012-newPsimd open64 workshop_2012-new
Psimd open64 workshop_2012-new
 
Halide tutorial 2019
Halide tutorial 2019Halide tutorial 2019
Halide tutorial 2019
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
 
CS.3.Arrays.pdf
CS.3.Arrays.pdfCS.3.Arrays.pdf
CS.3.Arrays.pdf
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
 
Idea for ineractive programming language
Idea for ineractive programming languageIdea for ineractive programming language
Idea for ineractive programming language
 
06 Recursion in C.pptx
06 Recursion in C.pptx06 Recursion in C.pptx
06 Recursion in C.pptx
 
Coscup2021-rust-toturial
Coscup2021-rust-toturialCoscup2021-rust-toturial
Coscup2021-rust-toturial
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

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);
  • 5. } void RotateX(float angle) { Matrix4x4 m; matrixSetIdentity(m); angle = angle*22/1260; m[1][1] = cos(angle); m[1][2] = -sin(angle); m[2][1] = sin(angle); m[2][2] = cos(angle); matrixPreMultiply(m , theMatrix); } void RotateY(float angle) { Matrix4x4 m; matrixSetIdentity(m); angle = angle*22/1260; m[0][0] = cos(angle); m[0][2] = sin(angle); m[2][0] = -sin(angle); m[2][2] = cos(angle); matrixPreMultiply(m , theMatrix); } void RotateZ(float angle) { Matrix4x4 m; matrixSetIdentity(m); angle = angle*22/1260;
  • 6. m[0][0] = cos(angle); m[0][1] = -sin(angle); m[1][0] = sin(angle); m[1][1] = cos(angle); matrixPreMultiply(m , theMatrix); } void Reflect(void) { Matrix4x4 m; matrixSetIdentity(m); switch(choiceRef) { case 1: m[2][2] = -1; break; case 2: m[0][0] = -1; break; case 3: m[1][1] = -1; break; } matrixPreMultiply(m , theMatrix); } void DrawRotLine(void) { switch(choiceRot) { case 1: glBegin(GL_LINES); glVertex3s(-1000 ,B,C); glVertex3s( 1000 ,B,C);
  • 7. glEnd(); break; case 2: glBegin(GL_LINES); glVertex3s(A ,-1000 ,C); glVertex3s(A ,1000 ,C); glEnd(); break; case 3: glBegin(GL_LINES); glVertex3s(A ,B ,-1000); glVertex3s(A ,B ,1000); glEnd(); break; case 4: glBegin(GL_LINES); glVertex3s(x1-aa*500 ,y11-bb*500 , z1-cc*500); glVertex3s(x2+aa*500 ,y2+bb*500 , z2+cc*500); glEnd(); break; } } void TransformPoints(void) { int i,k; float tmp ; for(k=0 ; k<8 ; k++) for (i=0 ; i<3 ; i++) ptsFin[k][i] = theMatrix[i][0]*ptsIni[k][0] + theMatrix[i][1]*ptsIni[k][1] + theMatrix[i][2]*ptsIni[k][2] + theMatrix[i][3]; }
  • 8. void Axes(void) { glColor3f (0.0, 0.0, 0.0); glBegin(GL_LINES); glVertex2s(-1000 ,0); glVertex2s( 1000 ,0); glEnd(); glBegin(GL_LINES); glVertex2s(0 ,-1000); glVertex2s(0 , 1000); glEnd(); } void Draw(float a[8][3]) { int i; glColor3f (0.7, 0.4, 0.7); glBegin(GL_POLYGON); glVertex3f(a[0][0],a[0][1],a[0][2]); glVertex3f(a[1][0],a[1][1],a[1][2]); glVertex3f(a[2][0],a[2][1],a[2][2]); glVertex3f(a[3][0],a[3][1],a[3][2]); glEnd(); i=0; glColor3f (0.8, 0.6, 0.5); glBegin(GL_POLYGON); glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]); glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]); glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]);
  • 9. glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]); glEnd(); glColor3f (0.2, 0.4, 0.7); glBegin(GL_POLYGON); glVertex3f(a[0][0],a[0][1],a[0][2]); glVertex3f(a[3][0],a[3][1],a[3][2]); glVertex3f(a[7][0],a[7][1],a[7][2]); glVertex3f(a[4][0],a[4][1],a[4][2]); glEnd(); i=1; glColor3f (0.5, 0.4, 0.3); glBegin(GL_POLYGON); glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]); glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]); glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]); glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]); glEnd(); i=2; glColor3f (0.5, 0.6, 0.2); glBegin(GL_POLYGON); glVertex3s(a[0+i][0],a[0+i][1],a[0+i][2]); glVertex3s(a[1+i][0],a[1+i][1],a[1+i][2]); glVertex3s(a[5+i][0],a[5+i][1],a[5+i][2]); glVertex3s(a[4+i][0],a[4+i][1],a[4+i][2]); glEnd(); i=4; glColor3f (0.7, 0.3, 0.4); glBegin(GL_POLYGON);
  • 10. glVertex3f(a[0+i][0],a[0+i][1],a[0+i][2]); glVertex3f(a[1+i][0],a[1+i][1],a[1+i][2]); glVertex3f(a[2+i][0],a[2+i][1],a[2+i][2]); glVertex3f(a[3+i][0],a[3+i][1],a[3+i][2]); glEnd(); } void display(void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Axes(); glColor3f (1.0, 0.0, 0.0); Draw(ptsIni); matrixSetIdentity(theMatrix); switch(choice) { case 1: Translate(TransDistX, TransDistY ,TransDistZ); break; case 2: Scale(ScaleX, ScaleY, ScaleZ); break; case 3: switch(choiceRot) { case 1: DrawRotLine(); Translate(0,-B,-C); RotateX(Alpha); Translate(0,B,C); break; case 2: DrawRotLine();
  • 11. Translate(-A,0,-C); RotateY(Beta); Translate(A,0,C); break; case 3: DrawRotLine(); Translate(-A,-B,0); RotateZ(Gamma); Translate(A,B,0); break; case 4: DrawRotLine(); float MOD =sqrt((x2-x1)*(x2-x1) + (y2-y11)*(y2-y11) + (z2-z1)*(z2-z1)); aa = (x2-x1)/MOD; bb = (y2-y11)/MOD; cc = (z2-z1)/MOD; Translate(-x1,-y11,-z1); float ThetaDash; ThetaDash = 1260*atan(bb/cc)/22; RotateX(ThetaDash); RotateY(1260*asin(-aa)/22); RotateZ(Theta); RotateY(1260*asin(aa)/22); RotateX(-ThetaDash); Translate(x1,y11,z1); break; } break; case 4: Reflect(); break;
  • 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);
  • 19. minx = array_min(x,0); setcolor(BLUE); line(minx+depth, maxy-depth, -depth, getmaxy()); line(minx+depth, maxy-depth, getmaxx(), maxy-depth); line(minx+depth, maxy-depth, minx+depth, 0); setlinestyle(0,1,2); setfillstyle(CLOSE_DOT_FILL, WHITE); floodfill(0,0,BLUE); setfillstyle(CLOSE_DOT_FILL, BROWN); floodfill(getmaxx(),0,BLUE); setfillstyle(CLOSE_DOT_FILL, DARKGRAY); floodfill(getmaxx(),getmaxy(),BLUE); } void draw3d(int sides,int *x,int *y,int depth) { int i,j,k=0; setlinestyle(0,1,1); drawAxis(x, y, depth); setcolor(YELLOW); for(j=0;j<2;j++) { for(i=0;i<sides;i++) { if(i!=sides-1) line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k); else line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
  • 20. if(j==0) line(x[i],y[i],x[i]+depth,y[i]-depth); } k=depth; } } void drawFrontView(int sides,int *x,int *y) { int i; setlinestyle(0,1,1); setcolor(LIGHTGREEN); for(i=0;i<sides;i++) { if(i!=sides-1) line(x[i],y[i],x[i+1],y[i+1]); else line(x[i],y[i],x[0],y[0]); } } void drawSideView(int sides,int *x,int *y, int depth) { int i, maxyIndex, minyIndex, maxxIndex; maxxIndex=array_max(x,1); maxyIndex=array_max(y,1); minyIndex=array_min(y,1); setcolor(LIGHTGREEN); for(i=0;i<sides-1;i++)
  • 21. { if(y[i]<y[maxyIndex] && y[i]>y[minyIndex] && x[i]<x[maxxIndex]) setlinestyle(3,1,1); else setlinestyle(0,1,1); line(x[0],y[i],x[0],y[i+1]); line(x[0]+depth*2,y[i],x[0]+depth*2,y[i+1]); line(x[0],y[i],x[0]+depth*2,y[i]); line(x[0],y[i+1],x[0]+depth*2,y[i+1]); } } void drawTopView(int sides, int *x, int *y, int depth) { int i, minyIndex, maxxIndex, minxIndex; maxxIndex=array_max(x,1); minxIndex=array_min(x,1); minyIndex=array_min(y,1); setcolor(LIGHTGREEN); for(i=0;i<sides-1;i++) { if(x[i]<x[maxxIndex] && x[i]>x[minxIndex] && y[i]>y[minyIndex]) setlinestyle(3,1,1); else setlinestyle(0,1,1); line(x[i],y[0],x[i+1],y[0]);
  • 22. line(x[i],y[0]+depth * 2,x[i+1],y[0]+depth*2); line(x[i],y[0],x[i],y[0]+depth*2); line(x[i+1],y[0],x[i+1],y[0]+depth*2); } } void main(){ int gd=0,gm; int x[20],y[20],i,sides,depth,key; initgraph(&gd,&gm,""); printf("No of sides(front view only) : "); scanf("%d",&sides); printf("Co-ordinates : "); for(i=0;i<sides;i++) { printf("(x%d,y%d)",i,i); scanf("%d%d",&x[i],&y[i]); } printf("Depth :"); scanf("%d",&depth); ARRAY_SIZE=sides; while(1){ key=getche(); clrscr(); cleardevice(); printf("LEFT ARROW -> 3D ViewnUP ARROW - > Front ViewnDOWN ARROW -> Top ViewnRIGHT ARROW -> Side ViewnESC -> Exit"); switch(key){ case 75: //left arrow
  • 23. draw3d(sides,x,y,depth); break; case 72: //up arrow drawFrontView(sides,x,y); break; case 77: //right arrow drawSideView(sides,x,y,depth); break; case 80: //down arrow drawTopView(sides,x,y,depth); break; case 27: //esc exit(0); break; } } OUTPUT: