We
define an image in coordinate system, to display that image
Or object. Transformation is refer to transform from one position to another
position depends upon there transformation it is classified
into.Scaling.Shearing.Reflection.Rotation see the source code in C coding
Source code programming 2D TRANSFORMATION Coding
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void draw2d(int,int [],int [],int,int);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],tx=0,ty=0,i,fs;
initgraph(&gd,&gm,"");
printf("No of sides : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
draw2d(fs,x,y,tx,ty);
printf("translation (x,y) : ");
scanf("%d%d",&tx,&ty);
draw2d(fs,x,y,tx,ty);
getch();
}
void draw2d(int fs,int x[20],int y[20],int tx,int ty)
{
int i;
for(i=0;i<fs;i++)
{
if(i!=(fs-1))
line(x[i]+tx,y[i]+ty,x[i+1]+tx,y[i+1]+ty);
else
line(x[i]+tx,y[i]+ty,x[0]+tx,y[0]+ty);
}
}
//# Author: J.Ajai
//#Mail-id- ajay.compiler@gmail.com
//# PH:+91-9790402155
OUTPUT
2D TRANSFORMATION
---------------------------------------------------
1.Translation
2.Scaling
3.Shearing
4.Reflection
5.Rotation
2.Scaling
3.Shearing
4.Reflection
5.Rotation
Enter your Choice :2
SEE THE FOLLOWING OUTPUT SCALING, SHEARING WITH 2
DIGARM(Shearing Y direction with respect to Xref &,Shearing x direction
with respect to yref)
Source coding Output REFLECTION & ROTATION 2D
TRANSFORMATION
#include<dos.h>
#include<math.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
void main()
{
int xmax,ymax,gd=0,gm,tx,ty,k;
initgraph(&gd,&gm,"c:\\tc\\bgi");
xmax=getmaxx();
ymax=getmaxy();
line(xmax/2,0,xmax/2,ymax);
line(0,ymax/2,xmax,ymax/2);
int a[10][10],b[10][10],c[10][10],i,j,n;
cout<<"Enter no of vertices:-";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter co-ordi of "<<i+1<<"point:";
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
setcolor(46);
int ch1,ch2,y=1,sx,sy;
while(y==1)
{
for(i=0;i<n-1;i++)
{
line(xmax/2+a[i][0],ymax/2-a[i][1],xmax/2+a[i+1][0],ymax/2-a[i+1][1]);
}
line(xmax/2+a[i][0],ymax/2-a[i][1],xmax/2+a[0][0],ymax/2-a[0][1]);
setcolor(15);
cout<<"\n1.translation \n2.Rotation\n3.Reflection\n4.Scaling";
cout<<"\nEnter ur choice:-";
cin>>ch1;
switch(ch1)
{
case 1:
b[0][0]=1;b[0][1]=0;b[0][2]=0;
b[1][0]=0;b[1][1]=1;b[1][2]=0;
cout<<"\nEnter tx and ty:-";
cin>>tx>>ty;
b[2][0]=tx;b[2][1]=ty;b[2][2]=1;
break;
case 2:
cout<<"\n1.90 \t2.180\t3.270\t4.360";
cout<<"\nEnter ur choice:-";
cin>>ch2;
switch(ch2)
{
case 1:
b[0][0]=0;b[0][1]=1;b[0][2]=0;
b[1][0]=-1;b[1][1]=0;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
case 2:
b[0][0]=-1;b[0][1]=0;b[0][2]=0;
b[1][0]=0;b[1][1]=-1;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
case 3:
b[0][0]=0;b[0][1]=-1;b[0][2]=0;
b[1][0]=1;b[1][1]=0;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
case 4:
b[0][0]=1;b[0][1]=0;b[0][2]=0;
b[1][0]=0;b[1][1]=1;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
} //end rot switch
break;
case 3:
cout<<"\n1.Ref thru x axis \n2.Ref thru y axis\n3.Ref thru x=y axis\n4.Ref thru x=-y
axis";
cout<<"\nEnter ur choice:-";
cin>>ch2;
switch(ch2)
{
case 1:
b[0][0]=1;b[0][1]=0;b[0][2]=0;
b[1][0]=0;b[1][1]=-1;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
case 2:
b[0][0]=-1;b[0][1]=0;b[0][2]=0;
b[1][0]=0;b[1][1]=1;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
case 3:
b[0][0]=0;b[0][1]=1;b[0][2]=0;
b[1][0]=1;b[1][1]=0;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
case 4:
b[0][0]=0;b[0][1]=-1;b[0][2]=0;
b[1][0]=-1;b[1][1]=0;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
} //end ref switch
break;
case 4:
cout<<"\nEnter scaling factors sx and sy:-";
cin>>sx>>sy;
b[0][0]=sx;b[0][1]=0;b[0][2]=0;
b[1][0]=0;b[1][1]=sy;b[1][2]=0;
b[2][0]=0;b[2][1]=0;b[2][2]=1;
break;
default:
cout<<"\nEnter correct choice:-";
continue;
}//end switch
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
setcolor(4);
for(i=0;i<n-1;i++)
{
line(xmax/2+c[i][0],ymax/2-c[i][1],xmax/2+c[i+1][0],ymax/2-c[i+1][1]);
}
line(xmax/2+c[i][0],ymax/2-c[i][1],xmax/2+c[0][0],ymax/2-c[0][1]);
cout<< "do u want to continue(1=yes/0=no):-";
cin>>y;
cleardevice();
clrscr();
setcolor(15);
line(xmax/2,0,xmax/2,ymax);
line(0,ymax/2,xmax,ymax/2);
}//end while
// getch();
}
cleardevice();
draw();
getch();
tri();
getch();
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void tri()
{
int x,y,a1,a2,a3,b1,b2,b3;
printf("Enter the Transaction coordinates");
scanf("%d%d",&x,&y);
cleardevice();
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void RectAngle(int x,int y,int Height,int Width);
void Translate(int x,int y,int Height,int Width);
void main()
{
int gd=DETECT,gm;
int x,y,Height,Width;
initgraph(&gd,&gm," ");
printf("Enter the First point for the Rectangle:");
scanf("%d%d",&x,&y);
printf("Enter the Height&Width for the Rectangle:");
scanf("%d%d",&Height,&Width);
RectAngle(x,y,Height,Width);
getch();
cleardevice();
Translate(x,y,Height,Width);
RectAngle(x,y,Height,Width);
getch();
}
void RectAngle(int x,int y,int Height,int Width)
{
line(x,y,x+Width,y);
line(x,y,x,y+Height);
line(x+Width,y,x+Width,y+Height);
line(x,y+Height,x+Width,y+Height);
}
void Translate(int x,int y,int Height,int Width)
{
int Newx,Newy,a,b;
printf("Enter the Transaction coordinates");
scanf("%d%d",&Newx,&Newy);
cleardevice();
a=x+Newx;
b=y+Newy;
RectAngle(a,b,Height,Width);
}
http://i.thiyagaraaj.com/tutorials/computer-graphics-programs-using-c-programming/2danimation-computer-graphics-programs/2d-translation-rectangle-program-using-cprogramming
http://i.thiyagaraaj.com/tutorials/computer-graphics-programs-using-c-programming/2danimation-computer-graphics-programs/2d-rotation-program-using-c-programming
http://www.infogoaround.org/JBook/Ch4.html
http://www.google.co.in/url?sa=t&rct=j&q=2d%20transformation%20in%20computer
%20graphics%20source
%20code&source=web&cd=6&cad=rja&ved=0CDwQFjAF&url=http%3A%2F
%2Flabprograms.wordpress.com%2F2009%2F07%2F04%2F2d-transformations
%2F&ei=a-tyUI3oDIzprQeqn4D4Aw&usg=AFQjCNEUlW23pD9yqhpEoqoZ2xiw3kViQ
b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp;
b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm,d:\\turboc~1\\bgi);
printf(\nEnter the number of vertices :
);
scanf(%d,&n);
for(i=0;i<n;i++)
{
printf(\nEnter the co-ordinates of the %d vertex :,i+1);
scanf(%f%f,&c[i][0],&c[i][1]);
c[i][2]=1;
}
do
{
clrscr();
cleardevice();
printf(\n\t\t\t * * * MENU * * *);
printf(\n\t 1) TRANSLATION);
printf(\n\t 2) SCALING );
printf(\n\t 3) ROTATION );
printf(\n\t 4) EXIT);
printf(\n\t ENTER YOUR CHOICE:
);
scanf(%d,&cho);
switch(cho)
{
case 1:
printf(\n\tEnter translation factor for X & Y axis :\t);
scanf(%f%f,&tx,&ty);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
translation(n,c,tx,ty);
setcolor(RED);
disp(n,c);
getch();
break;
case 2:
printf(\n\tEnter scaling factor for X & Y axis :\t);
scanf(%f%f,&sx,&sy);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
scaling(n,c,sx,sy);
getch();
break;
case 3:
printf(\n\n\tEnter the angle of rotation:\t);
scanf(%f,&ra);
clrscr();
cleardevice();
setcolor(BLUE);
disp(n,c);
rotation(n,c,ra);
getch(); break;
case 4:
exit(0);
break;
default:
printf(\n\tInvalid choice !!);
break;
}
}while(cho!=4);
getch();
closegraph();
}
All transformation
To write a C program to implement 2D transformations.
Step By Step Procedural Algorithm
1. Enter the choice for transformation.
2. Perform the translation, rotation, scaling, reflection and shearing of 2D object.
3. Get the needed parameters for the transformation from the user.
4. Incase of rotation, object can be rotated about x or y axis.
5. Display the transmitted object in the screen.
Source Code Programming 2D Transformations
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];
void main()
{
int gm ,gr;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
break;
case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("********Scaling*******\n\n");
printf("Enter the value of scaling factor:\n");
scanf("%f%f",&sx,&sy);
x1=x*sx;
y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(x1,y1,az1,w1);
break;
case 4:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Reflection*********\n");
printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter your choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(90*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 2:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(270*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(180*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
}
break;
case 5:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Shearing******\n\n");
printf("1.x-direction shear\n2.y-direction shear\nEnter your choice:\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the value of shear:\n");
scanf("%f",&x1s);
x1=x+(y*x1s);
y1=y;
az1=az+(w*x1s);
w1=w;
rectangle(x1,y1,az1,w1);
break;
case 2:
printf("Enter the value of shear:\n");
scanf("%f",&y1s);
x1=x;
y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(x1,y1,az1,w1);
break;
}
break;
case 6:
exit(0);
}
}
getch();
}
Example OUTPUT Result compuer Graphics
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(((xmax)/2)+x1,((ymax)/2)+y1,((xmax)/2)+az1,((ymax)/2)+w1);
break;
case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Rotation\n\n");
printf("Enter the two value of fixed point and angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[i+1],
((ymax)/2)+b1[i+1]);
else
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[0],
((ymax)/2)+b1[0]);
}
break;
case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Scaling\n\n");
printf("Enter the two value sx and sy of scaling factor:\n");
scanf("%f%f",&sx,&sy);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
x1=x*sx;
y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(((xmax)/2)+x1,((ymax)/2)+y1,((xmax)/2)+az1,((ymax)/2)+w1);
break;
case 4:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Reflection\n");
line(0,(ymax/2),xmax,(ymax/2));
x1=x;
y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(((xmax)/2)+x1,((xmax)/2)+y1,((xmax)/2)+az1,((xmax)/2)+w1);
break;
}
break;
case 6:
exit(0);
}
}
getch();
}
http://ldrpceit.blogspot.in/2011/07/implement-algorithm-of-2d.html