COMPUTER GRAPHICS- Implement 2D Transformation : Translation, Scaling, Rotation, Reflection

Experiment No.05
Aim:

Implement 2D Transformation : Translation, Scaling, Rotation, Reflection
and Shear.


Prerequisite:
1. C Language.
2. Geometric Concepts.
2. Concept of 2D basic Transformations.

Outcome:
After successful completion of this experiment students will be able to,
Apply the transformations and clipping algorithms on graphical objects.

Theory:
1. Translation
A translation moves an object to a different position on the screen. You can translate a
point in 2D by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to
get the new coordinate (X’, Y’).

A translation moves an object to a different position on the screen. You can translate a point in 2D by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to get the new coordinate (X’, Y’).














From the above figure, you can write that −
X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector.
P’ = P + T

2. Rotation
In rotation, we rotate the object at particular angle θ (theta) from its origin. From the
following figure, we can see that the point P(X, Y) is located at angle φ from the
horizontal X coordinate with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new location,
you will get a new point P’ (X’, Y’).

In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate with distance r from the origin.













Using standard trigonometric the original coordinate of point P(X, Y) can be represented as −
X=rcosϕ......(1)X=rcosϕ......(1)
Y=rsinϕ......(2)Y=rsinϕ......(2)
Same way we can represent the point P’ (X’, Y’) as −
x′=rcos(Φ+θ)=rcoscosθ−rsin Φ sinθ.......(3)x′=rcos(Φ +θ)=rcos Φ Cosθ−rsin Φ sinθ.......(3)
y′=rsin(Φ +θ)=rcos Φ sinθ+rsin Φ cosθ.......(4)y′=rsin(Φ +θ)=rcos Φ sinθ+rsin Φ cosθ.......(4)
Substituting equation (1) & (2) in (3) & (4) respectively, we will get
x′=xcosθ−ysinθx′=xcosθ−ysinθ
y′=xsinθ+ycosθy′=xsinθ+ycosθ
Representing the above equation in matrix form,
[X′Y′]=[XY][cosθ−sinθsinθcosθ]OR[X′Y′]=[XY][cosθsinθ−sinθcosθ]OR
P’ = P . R
Where R is the rotation matrix
R=[cosθ−sinθsinθcosθ]R=[cosθsinθ−sinθcosθ]
The rotation angle can be positive and negative.
For positive rotation angle, we can use the above rotation matrix. However, for negative angle
rotation, the matrix will change as shown below −
R=[cos(−θ)−sin(−θ)sin(−θ)cos(−θ)]R=[cos(−θ)sin(−θ)−sin(−θ)cos(−θ)]
=[cosθsinθ−sinθcosθ](∵cos(−θ)=cosθandsin(−θ)=−sinθ)=[cosθ−sinθsinθcosθ](​∵​cos(−θ)
=cosθandsin(−θ)=−sinθ)

3. Scaling
To change the size of an object, scaling transformation is used. In the scaling process, you either
expand or compress the dimensions of the object. Scaling can be achieved by multiplying the
original coordinates of the object with the scaling factor to get the desired result.
Let us assume that the original coordinates are (X, Y), the scaling factors are (SX, SY), and the
produced coordinates are (X’, Y’). This can be mathematically represented as shown below −
X' = X . SX and Y' = Y . SY
The scaling factor SX, SY scales the object in X and Y direction respectively.
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.
To change the size of an object, scaling transformation is used. In the scaling process, you either expand or compress the dimensions of the object. Scaling can be achieved by multiplying the original coordinates of the object with the scaling factor to get the desired result.












If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object.
If we provide values greater than 1, then we can increase the size of the object.

4. Reflection
Reflection is the mirror image of original object. In other words, we can say that it is a
rotation operation with 180°. In reflection transformation, the size of the object does not
change.
The following figures show reflections with respect to X and Y axes, and about the
origin respectively.

Reflection is the mirror image of original object. In other words, we can say that it is a rotation operation with 180°. In reflection transformation, the size of the object does not change.
























5. Shear
A transformation that slants the shape of an object is called the shear transformation.
There are two shear transformations X-Shear and Y-Shear. One shifts X coordinates
values and other shifts Y coordinate values. However; in both the cases only one
coordinate changes its coordinates and other preserves its values. Shearing is also
termed as Skewing.
X-Shear
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which
causes the vertical lines to tilt right or left as shown in below figure.
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which causes the vertical lines to tilt right or left as shown in below figure.












X' = X + Shx . Y 
Y’ = Y
Y-Shear
The Y-Shear preserves the X coordinates and changes the Y coordinates which causes
the horizontal lines to transform into lines which slopes up or down as shown in the
following figure.

The Y-Shear preserves the X coordinates and changes the Y coordinates which causes the horizontal lines to transform into lines which slopes up or down













Y’ = Y + Shy . X 
X’ = X

Procedure:
Algorithm
1) Start
2) Enter the number of edges of polygon(n)
3) Enter x and y co-ordinates of vertex.
4) Determine process to be performed
a) Translation
b) Rotation
c) Scaling

5) Translation
i) Enter translation value for x and y and z(tx,ty,tz)
ii) Add translation vertex tx & ty & tz to original co-
ordinate position (x,y,z) to move the point to new
position(x’,y’,z’)
x’ = x + tx
y’= y + ty
z’= z + tz
iii) goto step 8

6) Rotation
i) Enter the angle of rotation
ii) For rotating co-ordinate of vertex about Z axis we get transformation equation
through angle as
x’ = xcos0-ysin0
y’ = xsin0 + ysin0
z’ = z
iii) goto step 8

7) Scaling
i) Enter scaling factor sx, sy ,sz
ii) For scaling polygon ,multiply co-ordinate values (x,y,z) at each vertex by scaling
factor sx, sy, sz to produce transformed co-ordinate(x,y)as
x’ = x * sx
y’ = y * sy
z’ = z * sz
iii) Goto step 8
8) Stop.
Similarly, Perform Reflection and Shearing Transformation.

Code : 
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.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,"c:\\TURBOC3\\BGI");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d",&az,&w);
rectangle(x,y,az,w);
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da;
b[1]=y;
a[2]=x+da;
b[2]=y+db;
a[3]=x;b[3]=y+db;
while(1)
{
printf("******2D Transformations*******\n");
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n6.Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Translation*******\n\n");
printf("Enter the value of shift vector:\n");
scanf("%f%f",&dx,&dy);
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(x1,y1,az1,w1);
break;
case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Rotation*******\n\n");
printf("Enter the value of fixed point and angle of rotation:Enter the value of fixed point and angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
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(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
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,"c:\\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();
}

Outputs : 
1) Translation

2) Rotation

3) Scaling

4)X-Axis Reflection

5)Y-Axis Reflection

6)Both Axis Reflection

7)X-Direction Shearing

8)Y-Direction Shearing

Observations and learning:
In this experiment we Implemented 2D Transformation such as Translation, Scaling, Rotation, Reflection and Shearing.

Conclusion:
It becomes easy to perform more than one transformation on a single object by implementing various transformation to get the desired output.

Question of Curiosity
Q.1] Explain Composite Transformation.
Ans : A composite transformation (or composition of transformations) is two or more transformations performed one after the other. Sometimes, a composition of transformations is equivalent to a single transformation. The following is an example of a translation followed by a reflection. The original triangle is the brown triangle and the image is the blue striped triangle. The brown striped triangle shows the intermediate step after the translation has taken place.

Q.2] Write a short note on Homogeneous Co-ordinates.
Ans : Any point in the projective plane is represented by a triple (X, Y, Z), called the homogeneous coordinates or projective coordinates of the point, where X, Y and Z are not all 0. The point represented by a given set of homogeneous coordinates is unchanged if the coordinates are multiplied by a common factor.

Q.3] What happens to the size of object when
         Case a. Sx=Sy<=1
         Case b. Sx=Sy>1 

Comments