Professional Documents
Culture Documents
Unit 3 Overview of Transformation Sayli
Unit 3 Overview of Transformation Sayli
2D Translation
- Moves points to new locations by adding translation amounts to the coordinates of the
points.
• Initial Position point P (x, y)
• The new point P’ (x’, y’) Where,
x’ = x + tx
y’ = y + ty
tx and ty is the displacement in x and y respectively.
The translation pair (tx, ty) is called a translation vector or shift vector.
Matrix Representation
OR P’ = P + T
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
1
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
void draw();
void tri();
void main()
{
int gd=DETECT,gm; int c;
initgraph(&gd,&gm,"c:\\turboc3\\bgi ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
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;
2
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
Rotation
Default rotation center: Origin (0,0)
o Rotation angle θ
o Pivot point ( xr , yr)
Positive values of θ for counterclockwise rotation
x = r cos (φ)
y = r sin (φ)
x’ = r cos (φ + θ)
y’ = r sin (φ + θ)
x’ = r cos (φ + θ)= r cos(φ) cos(θ) – r sin(φ) sin(θ)
= x cos(θ) – y sin(θ)
y’ = r sin (φ + θ) = r sin(φ) cos(θ) + r cos(φ)sin(θ)
= y cos(θ) + x sin(θ)
P’ = P. R
3
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
For positive rotation angle, we can use the above rotation matrix. However, for negative angle
rotation, the matrix will change as shown below –
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void triangle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
triangle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
triangle(x1,y1,x2,y2,x3,y3);
getch();
}
void triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
4
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
triangle(a1,b1,a2,b2,a3,b3);
}
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.
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. Scaling matrix
form as below –
OR
P’ = P. S
Where S is the scaling matrix. The scaling process is shown in the following figure.
5
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
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.
Program: Write a program for 2D Scaling of a Triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
6
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
{
int x,y,a1,a2,a3,b1,b2,b3; int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line (a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
Homogeneous coordinates:
In design and picture formation process, many times require to perform translation, rotations
and scaling to fit the picture components into their proper positions.
General matrix form:
For Translation:
For Rotation:
7
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
For Scaling:
(To produce sequence of transformation with above equations, such as translation followed
by rotation and then scaling, the calculation of transformed coordinates one step at a time. To
combine sequence of transformations into one transformation so that final coordinate
positions are obtained directly from initial coordinates. This eliminates the calculation of
intermediate coordinate values.)
In this system, we can represent all the transformation equations in matrix multiplication.
Any Cartesian point P(X, Y) can be converted to homogenous coordinates by P’ (Xh, Yh, h).
So
8
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
So,
So,
Reflection:
9
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
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
examples of some common reflections are:
#include<stdio.h>
#include<process.h>
#include<conio.h>
10
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
#include<graphics.h>
#include<math.h>
11
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
int i=0,ch,j;
cleardevice();
12
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
b[1][1]=0;
b[0][1]=-1;
b[1][0]=-1;
break;
case 6:
break;
default:
printf("“\n\tINVALID CHOICE ! ");
break;
}
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,"c:\\turboc3\\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;
}
disp(n,c);
reflection(n,c);
getch();
closegraph();
}
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 shift X coordinate’s values and
other shifts Y coordinate values. However in both the cases only one coordinate changes its
coordinates and other preserves its values.
13
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
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.
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
14
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>
void mul(int mat[3][3],int vertex[10][3],int n);
void shear(int vertex[10][3],int n);
void init(int vertex[10][3],int n);
int main()
{
int i,x,y,xsh,ysh,xref,yref;
15
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
for(i=0;i<n-1;i++)
{
line(320+vertex[i][0],240-vertex[i][1],320+vertex[i+1][0],240- vertex[i+1][1]);
}
line(320+vertex[n-1][0],240-vertex[n-1][1],320+vertex[0][0],240-vertex[0][1]);
16
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
res[i][j]=0;
for(k=0;k<3;k++)
{
res[i][j] = res[i][j] + vertex[i][k]*mat[k][j];
}
}
}
setcolor(15);
for(i=0;i<n-1;i++)
{
line(320+res[i][0],240-res[i][1],320+res[i+1][0],240-res[i+1][1]);
}
line(320+res[n-1][0],240-res[n-1][1],320+res[0][0],240-res[0][1]);
17
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
shear_array[0][2]=0;
shear_array[1][2]=0;
shear_array[2][2]=1;
init(vertex,n);
mul(shear_array,vertex,n);
break;
case 2:
printf("\nEnter the y shear : ");
scanf("%d",&ysh);
shear_array[0][0]=1;
shear_array[1][0]=0;
shear_array[2][0]=0;
shear_array[0][1]=ysh;
shear_array[1][1]=1;
shear_array[2][1]=0;
shear_array[0][2]=0;
shear_array[1][2]=0;
shear_array[2][2]=1;
init(vertex,n);
mul(shear_array,vertex,n);
break;
case 3:
printf("\nEnter the y shear : ");
scanf("%d",&ysh);
printf("\nEnter the x ref : ");
scanf("%d",&xref);
shear_array[0][0]=1;
shear_array[1][0]=0;
shear_array[2][0]=0;
shear_array[0][1]=ysh;
shear_array[1][1]=1;
shear_array[2][1]=-ysh*xref;
shear_array[0][2]=0;
shear_array[1][2]=0;
shear_array[2][2]=1;
init(vertex,n);
mul(shear_array,vertex,n);
break;
case 4:
printf("\nEnter the x shear : ");
18
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
scanf("%d",&xsh);
printf("\nEnter the y ref: ");
scanf("%d",&yref);
shear_array[0][0]=1;
shear_array[1][0]=xsh;
shear_array[2][0]=-xsh*yref;
shear_array[0][1]=0;
shear_array[1][1]=1;
shear_array[2][1]=0;
shear_array[0][2]=0;
shear_array[1][2]=0;
shear_array[2][2]=1;
init(vertex,n);
mul(shear_array,vertex,n);
break;
}
printf("\n Do you want to continue");
ans=getche();
}
while(ans=='y'||ans=='Y');
}
Composite transformations
The basic purpose of composing transformations is to gain efficiency by applying a single
composed transformation to a point, rather than applying a series of transformation, one after
another.
If a transformation of the plane T1 is followed by a second plane transformation T2, then the
result itself may be represented by a single transformation T which is the composition of T1
and T2 taken in that order. This is written as T = T1∙T2.
For example, to rotate an object about an arbitrary point (Xp, Yp), we have to carry out three
steps – (T*R*T)
19
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
Assume that to rotate a point P1 with respect to (Xm, Ym) then perform three steps.
I) Translation: First we have to translate the (Xm, Ym) to origin as shown in figure
Translation matrix (T1) will become
20
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
3D Transformation:
3D Translation:
Three dimensional transformation matrix for translation with homogeneous coordinates is
given below. It specifies three coordinates with their own translation factor.
1 0 0 0
0 1 0 0
0]
T=[0 0 1
�� �� �� 1
P’= P. T
1 0 0 0
0 1 0 0
[�′ � ′ �′ 1] = [� � � 1] * [ 0 0 1 ]
0
�� �� �� 1
= [� + �� � + �� � + �� 1]
3D Scaling:
Three dimensional transformation matrix for scaling with homogeneous coordinates is given
below. It specifies three coordinates with their own scaling factor.
21
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
3D Rotation:
Three dimensional transformation matrix for rotation with homogeneous coordinates is given
below. It specifies three coordinates with their own rotation factor.
22
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void trans();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Translation\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
trans();
getch();
23
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void trans()
{
int a1,a2,b1,b2,dep,x,y;
printf("\n Enter the Translation Distances:");
scanf("%d%d",&x,&y);
a1=x1+x;
a2=x2+x;
b1=y1+y;
b2=y2+y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void scale();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Scaling\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
24
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
my=(y1+y2)/2;
draw();
getch();
cleardevice();
scale();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void scale()
{
int x,y,a1,a2,b1,b2,dep;
printf("\n\n Enter scaling Factors:");
scanf("%d%d",&x,&y);
a1=mx+(x1-mx)*x;
a2=mx+(x2-mx)*x;
b1=my+(y1-my)*y;
b2=my+(y2-my)*y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
25
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
{
int x,y,z,o,x1,x2,y1,y2;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter rotating angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotation about z axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
printf("After rotation about x axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
printf("After rotation about yaxis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
closegraph();
}
Types of projections
26
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
• In parallel projection, the distance from the center of projection to project plane is
infinite.
• In this type of projection, we connect the projected vertices by line segments which
correspond to connections on the original object.
• Parallel projections are less realistic, but they are good for exact measurements.
• In this type of projections, parallel lines remain parallel and angles are not preserved.
• Various types of parallel projections are shown in the following hierarchy.
Orthographic Projection
• In orthographic projection the direction of projection is normal to the projection of the
plane.
27
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
Oblique Projection
• In orthographic projection, the direction of projection is not normal to the projection
of plane.
• In oblique projection, we can view the object better than orthographic projection.
• There are two types of oblique projections − Cavalier and Cabinet.
• The Cavalier projection makes 45° angle with the projection plane.
• The projection of a line perpendicular to the view plane has the same length as the
line itself in Cavalier projection.
• In a cavalier projection, the foreshortening factors for all three principal directions are
equal.
• The Cabinet projection makes 63.4° angle with the projection plane.
• In Cabinet projection, lines perpendicular to the viewing surface are projected at ½
their actual length.
• Both the projections are shown in the following figure –
Perspective Projection
• In perspective projection, the distance from the center of projection to project plane is
finite and the size of the object varies inversely with distance which looks more
realistic.
• The distance and angles are not preserved and parallel lines do not remain parallel.
• Instead, they all converge at a single point called center of projection or projection
28
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
reference point.
There are 3 types of perspective projections which are shown in the following chart.
• One point perspective projection
is simple to draw.
• Two point perspective projection
gives better impression of depth.
• Three point perspective projection
is most difficult to draw.
29
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION
30
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune