#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int xp[2],yp[2],z;
void display();
void translate();
void scaling();
void rotation();
void matrixmul(int [4][4]);
void main()
{
int gd=DETECT,gm;
int ch,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<2;i++)
{
printf("\nEnter X-coordinate of vertex %d : ",i+1);
scanf("%d",&xp[i]);
printf("\nEnter Y-coordinate of vertex %d : ",i+1);
scanf("%d",&yp[i]);
}
printf("\nEnter The Z-axis For 3d Figure : ");
scanf("%d",&z);
clrscr();
cleardevice();
display(xp,yp);
getche();
do
{
printf("----- MENU -----");
printf("\n1.TRANSLATION.");
printf("\t2.SCALING.");
printf("\n3.ROTATION.");
printf("\t4.EXIT.");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
clrscr();
cleardevice();
display(xp,yp);
switch(ch)
{
case 1 : translate();
break;
case 2 : scaling();
break;
case 3 : rotation();
break;
case 4 : exit(0);
default:
outtextxy(1,66,"-PLEASE SELECT THE RIGHT OPTION-");
}
}
while(ch!=4);
getch();
closegraph();
}
void translate()
{
int p[4][4];
int tx,ty,tz,i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Translating Factor tx : ");
scanf("%d",&tx);
printf("\nEnter The Translating Factor ty : ");
scanf("%d",&ty);
printf("\nEnter The Translating Factor tz : ");
scanf("%d",&tz);
clrscr();
cleardevice();
display();
p[0][3]=tx;
p[1][3]=ty;
p[2][3]=tz;
matrixmul(p);
}
void scaling()
{
int p[4][4];
int sx,sy,sz,i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Scaling Factor sx : ");
scanf("%d",&sx);
printf("\nEnter The Scaling Factor sy : ");
scanf("%d",&sy);
printf("\nEnter The Scaling Factor sz : ");
scanf("%d",&sz);
if(sx==0)
sx=1;
if(sy==0)
sy=1;
if(sz==0)
sz=1;
clrscr();
cleardevice();
p[0][0]=sx;
p[1][1]=sy;
p[2][2]=sz;
p[3][3]=1;
matrixmul(p);
}
void rotation()
{
float res[4][1],p[4][4],t[4][1];
int ang,i,j,k,l,rch;
float rad;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Rotating Angle : ");
scanf("%d",&ang);
rad=ang*0.0174;
printf("\nChoose the axis of roration ");
printf("\n1.X-axis");
printf("\n2.Y-axis");
printf("\n3.Z-axis");
printf("\nEnter Your Choice : ");
scanf("%d",&rch);
switch(rch)
{
case 1 : p[1][1]=cos(rad);
p[1][2]=(-1)*sin(rad);
p[2][1]=sin(rad);
p[2][2]=cos(rad);
break;
case 2 :
p[0][0]=cos(rad);
p[2][0]=(-1)*sin(rad);
p[0][2]=sin(rad);
p[2][2]=cos(rad);
break;
case 3 :
p[0][0]=cos(rad);
p[0][1]=(-1)*sin(rad);
p[1][0]=sin(rad);
p[1][1]=cos(rad);
break;
default :
printf("\nInvalid Choice !");
}
clrscr();
cleardevice();
for(i=0;i<2;i++)
{
t[0][0]=xp[i];
t[1][0]=yp[i];
t[2][0]=z;
t[3][0]=1;
for(j=0;j<4;j++)
{
for(k=0;k<1;k++)
{
res[j][k]=0;
for(l=0;l<4;l++)
{
res[j][k]=res[j][k]+(p[j][l]*t[l][k]);
}
}
}
xp[i]=res[0][0];
yp[i]=res[1][0];
z=res[2][0];
}
display(xp,yp);
}
void display(int xp[2],int yp[2])
{
int x3,y3,x4,y4;
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
outtextxy(getmaxx()/2+5,getmaxy()/2+5,"(0,0)");
outtextxy(getmaxx()-50,getmaxy()/2+10,"X-Axis");
outtextxy(getmaxx()/2+10,20,"Y-Axis");
outtextxy(10,getmaxy()/2+10,"X'-Axis");
outtextxy(getmaxx()/2+10,getmaxy()-20,"Y'-Axis");
rectangle(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+xp[1],getmaxy()/2-yp[1]);
if(z>=xp[0])
{
x3=z+xp[0];
y3=z+yp[0];
x4=z+xp[1];
y4=z+yp[1];
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
else
{
x3=xp[0]-z;
y3=yp[0]-z;
x4=xp[1]-z;
y4=yp[1]-z;
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
}
void matrixmul(int a[4][4])
{
float res[4][1],b[4][1];
int i,j,k,l;
for(i=0;i<2;i++)
{
b[0][0]=xp[i];
b[1][0]=yp[i];
b[2][0]=z;
b[3][0]=1;
for(j=0;j<4;j++)
{
for(k=0;k<1;k++)
{
res[j][k]=0;
for(l=0;l<4;l++)
{
res[j][k]=res[j][k]+(a[j][l]*b[l][k]);
}
}
}
xp[i]=res[0][0];
yp[i]=res[1][0];
}
z=res[2][0];
display(xp,yp);
}
Monday, May 11, 2009
C Program for 3-D transformations
Labels: Rotation, Scaling, Translation
Posted by Angad at 12:01 AM 2 comments
Saturday, May 9, 2009
C program for 2D Transformations
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate();
void scale();
void rotate();
void main()
{
int ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(6);
outtextxy (100,88,"Object.");
rectangle(100,150,150,100);
printf("---MENU---");
printf("\n 1)Translate\n 2)Scale\n 3)Rotate");
printf("\nEnter your choice: ");
scanf("%d",&ch);
cleardevice();
switch(ch)
{
case 1: translate();
break;
case 2: scale();
break;
case 3: rotate();
break;
default: printf("you have enterd wrong choice");
break;
}
getch();
closegraph();
}
void translate()
{
int tx,ty;
setcolor(2);
outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
printf("\nEnter tx: ");
scanf("%d",&tx);
printf("\nEnter ty: ");
scanf("%d",&ty);
cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Translation");
rectangle(100+tx,150+ty,150+tx,100+ty);
}
void scale()
{
int sx,sy;
setcolor(2);
outtextxy(240,10,"SCALING");
outtextxy(238,20,"--------");
printf("\nEnter sx: ");
scanf("%d",&sx);
printf("\nEnter sy: ");
scanf("%d",&sy);
cleardevice();
rectangle(100,150,150,100);
printf("\nAfter Scaling");
rectangle(100*sx,150*sy,150*sx,100*sy);
}
void rotate()
{
float theta;
int x1,x2,x3,x4;
int y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
int refx,refy;
printf("\nEnter the angle for rotation: ");
scanf("%f",&theta);
theta=theta*(3.14/180);
cleardevice();
setcolor(2);
outtextxy(240,10,"ROTATE");
outtextxy(238,20,"-------");
refx=100;
refy=100;
x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);
ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);
}
Labels: Rotation, Scaling, Translation
Posted by Angad at 2:07 AM 15 comments
