Two-Dimensional and Three-Dimensional Transformation: Compiled by Khem Nath Khatiwada
Two-Dimensional and Three-Dimensional Transformation: Compiled by Khem Nath Khatiwada
dimensional
Transformation
compiled by
Khem Nath Khatiwada
Unit Two: Two-Dimensional and Three dimensionalTransformation (7
Hours)
1. 2-Dimensional Transformations (4 hours)
• 2-D Translation, Rotation, Scaling (1.5hours):
Explanation of translation, rotation, and scaling operations in a 2D
space. Formulas and algorithms for performing 2D translation,
rotation, and scaling
• Transformation refers to the mathematical operations or rules that
are applied on a graphical image consisting of the number of lines,
circles, and ellipses to change its size, shape, or orientation.
• It can also reposition the image on the screen. Transformations play a
very crucial role in computer graphics.
• In Computer graphics,Transformation is a process of modifying and re-
positioning the existing graphics.
• 2D Transformations take place in a two dimensional plane.
• Transformations are helpful in changing the position, size, orientation,
shape etc of the object.
• Transformation Techniques-
• In computer graphics, various transformation techniques are
2D Translation in Computer Graphics-
• A translation process moves every point a constant distance in a specified direction.
• It can be described as a rigid motion.
• A translation can also be interpreted as the addition of a constant vector to every point, or as
shifting the origin of the coordinate system.
• 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,Yto get the new coordinate X
′,Y′
• .
X’ = tx + X
Y’ = ty + Y
or P' = T + P where
P' = (X', Y'),
T = (tx, ty ),
P = (X, Y)
Here, P(X, Y) is the original point. T (t x, ty ),is the translation factor, i.e. the amount by which the
point will be translated. P'(X’, Y’) is the coordinates of point P after translation.
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. The above equations can also be
represented using the column vectors.
Translation for (i = 0; i < n; i++) { return 0;
#include <conio.h> cin >> xs[i] >> ys[i]; }
#include <graphics.h> } void draw()
#include <iostream> cout << "Enter distances for {
#include <stdio.h> translation (in x and y " for (i = 0; i < n; i++) {
int gd = DETECT, gm; "directions): "; line(xs[i], ys[i], xs[(i + 1) %
int n, xs[100], ys[100], i, ty, tx; cin >> tx >> ty; n],
void draw(); initgraph(&gd, &gm, ys[(i + 1) % n]);
(char*)""); }
void translate();
cleardevice(); }
using namespace std;
setcolor(RED); void translate()
int main()
draw(); {
{
translate(); for (i = 0; i < n; i++) {
cout << "Enter no. of sides in
polygon: "; setcolor(YELLOW); xs[i] += tx;
cin >> n; draw(); ys[i] += ty;
cout << "Enter coordinates x, y getch(); }
for each vertex: "; closegraph(); }
• Given a square with coordinate points A(0, 3), B(3, 3), C(3, 0), D(0, 0).
Apply the translation with distance 1 towards X axis and 1 towards Y
axis. Obtain the new coordinates of the square.
• Given-
• Old coordinates of the square = A (0, 3), B(3, 3), C(3, 0), D(0, 0)
• Translation vector = (Tx, Ty) = (1, 1)
• For Coordinates A(0, 3)
• Let the new coordinates of corner A = (Xnew, Ynew).
• Applying the translation equations, we have-
• Xnew = Xold + Tx = 0 + 1 = 1
• Ynew = Yold + Ty = 3 + 1 = 4
• Thus, New coordinates of corner A = (1, 4).
• Computer Graphics Rotation
• Rotation is one of the part of computer graphic’s transformation,
• 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′.
Using standard trigonometric the original coordinate of point
PX,Y can be represented as −
• Rotate a line CD whose endpoints are (3, 4) and (12, 15) about origin
through a 45° anticlockwise direction.
• Rotate line AB whose endpoints are A (2, 5) and B (6, 12) about origin
through a 30° clockwise direction.
• Solution: For rotation in the clockwise direction. The matrix is
Rotation
• Step1: Rotation of point A (2, 5). Take angle 30°
Step2: Rotation of point B (6, 12)
//Program to rotate a Triangle: line(x2,y2, x3,y3);
//For Rotation line(x3, y3, x1, y1);
#include<stdio.h> cout<<"Enter rotation angle: ";
#include<graphics.h> cin>>angle;
#include<math.h> c = cos(angle *M_PI/180);
#include<iostream> s = sin(angle *M_PI/180);
using namespace std; x1 = floor(x1 * c + y1 * s);
int main() y1 = floor(-x1 * s + y1 * c);
{ x2 = floor(x2 * c + y2 * s);
int gd=0,gm,x1,y1,x2,y2,x3,y3; y2 = floor(-x2 * s + y2 * c);
double s,c, angle; x3 = floor(x3 * c + y3 * s);
initgraph(&gd, &gm, (char*)""); y3 = floor(-x3 * s + y3 * c);
setcolor(RED); setcolor(GREEN);
cout<<"Enter coordinates of triangle: "; line(x1, y1 ,x2, y2);
cin>>x1>>y1; line(x2,y2, x3,y3);
cin>>x2>>y2; line(x3, y3, x1, y1);
cin>>x3>>y3; getch();
setbkcolor(WHITE); closegraph();
cleardevice(); return 0;
line(x1,y1,x2,y2); }
• 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.
The above equations can also be represented in matrix form as below −
• continue
• If scaling factors are less than one, then the size of the object will be
reduced. If scaling factors are higher than one, then the size of the
object will be enlarged.
• If Sxand Syare equal it is also called as Uniform Scaling. If not equal
then called as Differential Scaling.
• If scaling factors with values less than one will move the object closer
to coordinate origin, while a value higher than one will move
coordinate position farther from origin.
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.
• Note: If the scaling factor S is less than 1, then we reduce the size of the
object. If the scaling factor S is greater than 1, then we increase size of
the object. Algorithm:
1. Make a 2x2 scaling matrix S as:
Sx 0
0 Sy
2. For each point of the polygon.
(i) Make a 2x1 matrix P, where P[0][0] equals
to x coordinate of the point and P[1][0]
equals to y coordinate of the point.
(ii) Multiply scaling matrix S with point
matrix P to get the new coordinate.
3. Draw the polygon using new coordinates.
#include <conio.h> for each vertex: "; void draw()
#include <graphics.h> for (i = 0; i < n; i++) { {
#include <iostream> cin >> x[i] >> y[i]; for (i = 0; i < n; i++) {
#include <math.h> } line(x[i], y[i], x[(i + 1) % n], y[(i
#include <stdio.h> cout << "Enter scale factors: sfx + 1) % n]);
int gd = DETECT, gm; and sfy : "; }
int n, x[100], y[100], i; cin >> sfx >> sfy; }
float sfx, sfy; initgraph(&gd, &gm, (char*)""); void scale()
void draw(); cleardevice(); {
void scale(); setcolor(RED); for (i = 0; i < n; i++) {
using namespace std; draw(); x[i] = x[0] + (int)((float)(x[i] -
scale(); x[0]) * sfx);
setcolor(YELLOW); y[i] = y[0] + (int)((float)(y[i] -
int main() y[0]) * sfx);
{ draw();
}
cout << "Enter no. of sides in getch();
}
polygon: "; closegraph();
scaling.cpp
cin >> n; return 0;
cout << "Enter coordinates x, y }
• Enlargement: If T1=Scaling,If (x1 y1)is original position and T1 is
translation vector then (x2 y2) are coordinated after scaling
• Reduction: If T1=Scaling. If (x1 y1) is original position and T1 is translation
vector, then (x2 y2) are coordinates after scaling
• Reduction: If T1=Scaling. If (x1 y1) is original position and T1 is translation
vector, then (x2 y2) are coordinates after scaling
• If (x,y) are the coordinates of an object and (x’,y’) are the coordinates of
transofrmed object then x’ and y’ after scaling is defined as follows.
x’=x.sx and y’=y*sy
• The above two equations can be represented with matrix as:
• Perform a scaling transformation on an object in square shape which
is at coordinates A(0, 3), B(3, 3), C(3, 0), D(0, 0) with the scaling
parameter as 2 towards X axis and 3 towards Y axis. also find new
coordinates of the object.
• S-1: In this case we have Point A with x1=0, y1=3, Point B with x2 =3 ,
y2 =3, Point C with x3=3 and y3=0, The last point or vertices D with
x4=0 and y4=0. Scaling Parameters are given as Sx=2 and Sy=3.
• S-3: After multiplication of above matrices the new coordinates after
performing scaling transformation. A’=(0,6) B(6,9), C(6,0) and D(0,0)
• 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 mirror image can be either about x-axis or y-axis. The object is
rotated by180°.
• The following figures show reflections with respect to X and Y axes,
and about the origin respectively.
• Types of Reflection:
• Reflection about the x-axis
• Reflection about the y-axis
• Reflection about an axis perpendicular to xy plane and passing
through the origin
• Reflection about line y=x
• 1. Reflection about x-axis: The object can be reflected about x-axis
with the help of the following matrix
• Reflection
• In this transformation value of x will remain same whereas the value
of y will become negative. Following figures shows the reflection of
the object axis. The object will lie another side of the x-axis.
• 2. Reflection about y-axis: The object can be reflected about y-axis
with the help of following transformation matrix
• Reflection
• Here the values of x will be reversed, whereas the value of y will
remain the same. The object will lie another side of the y-axis.
In this value of x and y both will be reversed. This is also called as half revolution about the origin.
• 4. Reflection about line y=x: The object may be reflected about line y
= x with the help of following transformation matrix
First of all, the object is rotated at 45°. The direction of rotation is clockwise. After it reflection is done concerning
x-axis. The last step is the rotation of y=x back to its original position that is counterclockwise at 45°.
• A triangle ABC is given. The coordinates of A, B, C are given as A (3 4)
B (6 4), C (4 8)
• Find reflected position of triangle i.e., to the x-axis.
Homogeneous Coordinates (0.5 hours):
• Introduction to homogeneous coordinates in 2D
transformations.
• Understanding the advantages and application of
homogeneous coordinates in transformations.
• Shearing:
• It is transformation which changes the shape of object. The sliding of
layers of object occur.
• 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.
• Shearing in the X-direction: In this horizontal shearing sliding of layers
occur. The homogeneous matrix for shearing in the x-direction is
shown below:
• 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 transformation matrix for X-Shear can be
represented as −
• 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
• Homogeneous Coordinates
• The rotation of a point, straight line or an entire image on the screen, about a point other
than origin, is achieved by first moving the image until the point of rotation occupies the
origin, then performing rotation, then finally moving the image to its original position.
• The moving of an image from one place to another in a straight line is called a translation.
A translation may be done by adding or subtracting to each point, the amount, by which
picture is required to be shifted.
Example: a(2,2), b(10,2), c(5,5) translate the triangle with dx=5 dy=6.
a’=a+ t
• Rotation:
• Rotating of an object will be always respective with an angle in the
plane. Suppose initial coordinates p(x,y,z), initial angle= sie and
rotation angle = theta, then we can perform three types of rotations
i.e x-axis, y-axis, & z-axis.
x-axis
• x'=x
• y'=ycos(theta) - zsin(theta)
• z'=ysin(theta) + zcos(theta)
y-axis
• x'= zsin(theta) + xcos(theta)
• y' = y
• z' = zcos(theta) - xsin(theta)
z-axis
• x'= xcos(theta) - ysin(theta)
• y' = xsin(theta) + ycos(theta)
• z'= z
• Example : p(1,2,3) theta=90
• x'=x=1
• y'=ycos(theta)-zsin(theta)
• => 2cos90-3sin90
• => -3
• z' = ysin(theta)+zcos(theta)
• => 2sin90+3cos90 => 2
• Scaling:
• Scaling is the transformation that will change the object size, based on
scaling factors sx, sy, sz along the x-axis, y-axis, and z-axis
• By this simple formula, we can achieve a variety of useful transformations, depending on what we put in the entries of
the matrix. For our purposes, consider moving along the x-axis a horizontal move and along the y-axis, a vertical move.
• A scaling transformation alters size of an object. In the scaling process, we either compress or expand the dimension of
the object. Scaling operation can be achieved by multiplying each vertex coordinate (x, y) of the polygon by scaling
factor sx and sy to produce the transformed coordinates as (x’, y’). So, x’ = x * sx and y’ = y * sy. The scaling factor sx, sy
scales the object in X and Y direction respectively. So, the above equation can be represented in matrix form: \
begin{bmatrix} X'\\ Y' \end{bmatrix}=\begin{bmatrix} Sx & 0 \\ 0 & Sy \end{bmatrix}\begin{bmatrix} X\\ Y \end{bmatrix}
Or P’ = S . P Scaling process:
• Note: If the scaling factor S is less than 1, then we reduce the size of the object. If the scaling factor S is greater than 1,
then we increase size of the object. Algorithm:
• 2D Transformation in Computer Graphics | Set 1 (Scaling of Objects) - GeeksforGeeks
cin >> n; return 0;
#include <conio.h> cout << "Enter coordinates x, y for }
#include <graphics.h> each vertex: "; void draw()
#include <iostream> for (i = 0; i < n; i++) { {
#include <math.h> cin >> x[i] >> y[i]; for (i = 0; i < n; i++) {
#include <stdio.h> } line(x[i], y[i], x[(i + 1) % n], y[(i +
cout << "Enter scale factors: sfx 1) % n]);
and sfy : "; }
int gd = DETECT, gm;
cin >> sfx >> sfy; }
int n, x[100], y[100], i;
initgraph(&gd, &gm, (char*)""); void scale()
float sfx, sfy;
cleardevice(); {
void draw();
setcolor(RED); for (i = 0; i < n; i++) {
void scale();
draw(); x[i] = x[0] + (int)((float)(x[i] -
using namespace std;
scale(); x[0]) * sfx);
setcolor(YELLOW); y[i] = y[0] + (int)((float)(y[i] -
int main() y[0]) * sfx);
draw();
{ }
getch();
cout << "Enter no. of sides in }
polygon: "; closegraph();
//for shearing cin>>n; closegraph();
cout<<"Enter coordinates x, y return 0;
#include<stdio.h> for each vertex: "; }
#include<graphics.h> for(i=0;i<n;i++){ void draw(){
#include<conio.h> cin>>x[i]>>y[i];} for(i=0; i<n; i++){
#include<iostream> cout<<"Enter scale factors: sfx line(x[i],y[i],x[(i+1)%n],y[(i+1)%n
and sfy : "; ]);}
#include<math.h>
cin>>sfx>>sfy; }
int gd= DETECT, gm;
initgraph(&gd, &gm, (char*)""); void scale(){
int n,x[100],y[100],i;
cleardevice(); for(i=0; i<n; i++){
float sfx, sfy;
setcolor(RED); x[i]=x[0]+(int)((float)(x[i]-
void draw();
draw(); x[0])*sfx);
void scale();
scale(); y[i]=y[0]+(int)((float)(y[i]-
using namespace std; y[0])*sfx);
setcolor(YELLOW);
int main(){ }
draw();
cout<<"Enter no. of sides in }
polygon: "; getch();
For Rotation triangle: "; x1 = floor(x1 * c + y1 * s);
#include<stdio.h> cin>>x1>>y1; y1 = floor(-x1 * s + y1 * c);
#include<graphics.h> cin>>x2>>y2; x2 = floor(x2 * c + y2 * s);
#include<math.h> cin>>x3>>y3; y2 = floor(-x2 * s + y2 * c);
#include<iostream> setbkcolor(WHITE); x3 = floor(x3 * c + y3 * s);
using namespace std; cleardevice(); y3 = floor(-x3 * s + y3 * c);
int main() line(x1,y1,x2,y2); setcolor(GREEN);
{ line(x2,y2, x3,y3); line(x1, y1 ,x2, y2);
int line(x3, y3, x1, y1); line(x2,y2, x3,y3);
gd=0,gm,x1,y1,x2,y2,x3,y3; cout<<"Enter rotation angle: line(x3, y3, x1, y1);
double s,c, angle; "; getch();
initgraph(&gd, &gm, cin>>angle; closegraph();
(char*)""); c = cos(angle *M_PI/180); return 0;
setcolor(RED); s = sin(angle *M_PI/180); }
cout<<"Enter coordinates of
#include<iostream> cout<<"Rectangle rectangle(x1,y1,xr,yr); cout<<"triangle line(x1,y1,x3,y3);
#include<graphics.h> after translation"<<endl; getch(); before reflection"<<endl; line(x3,y3,x4,y4);
#include<math.h> break; setcolor(3); line(x2,y2,x4,y4);
rectangle(x1+tx,y1+ty,x2+tx,y2 line(x1,y1,x2,y2);
using namespace std; +ty); } cout<<"After shearing
int main() case 3: line(x1,y1,x3,y3); of rectangle"<<endl;
getch();
{ { line(x2,y2,x3,y3); x1=x1+shx*y1;
break;
int gd=DETECT,gm,s; int cout<<"triangle after x2=x2+shx*y2;
}
initgraph(&gd,&gm, x1=30,y1=30,x2=70,y2=70,y=2 reflection"<<endl; x3=x3+shx*y3;
case 2: ,x=2; setcolor(5);
(char*)""); x4=x4+shx*y4;
{ long cout<<"Before line(x1,-y1+500,x2,-
cout<<"1.Translation\ x1=200,y1=200,x2=300,y2=30 scaling"<<endl; setcolor(13);
n2.Rotation\n3.Scaling\ y2+500);
0; line(x1,y1,x2,y2);
n4.Reflection\n5.Shearing setcolor(3); line(x1,-y1+500,x3,-
"<<endl; double a; y3+500); line(x1,y1,x3,y3);
cout<<"Selection:"; cout<<"Rectangle rectangle(x1,y1,x2,y2); line(x2,-y2+500,x3,- line(x3,y3,x4,y4);
with rotation"<<endl; y3+500); line(x2,y2,x4,y4);
cin>>s; cout<<"After
setcolor(3); scaling"<<endl; getch(); getch();
switch(s)
{ setcolor(10); break; }
rectangle(x1,y1,x2,y2);
case 1: } default:
cout<<"Angle of rectangle(x1*x,y1*y,x2*x,y2*y
{ int rotation:"; case 5: {
);
x1=200,y1=150,x2=300,y2=25 cin>>a; { cout<<"Invalid
0; getch(); Selection"<<endl;
a=(a*3.14)/180; int
int tx=50,ty=50; break; x1=400,y1=100,x2=600,y2=10 break;
long xr=x1+((x2- } 0,x3=400,y3=200,x4=600,y4=2
cout<<"Rectangle x1)*cos(a)-(y2-y1)*sin(a)); }
before translation"<<endl; case 4: 00,shx=2;
long yr=y1+((x2- }
setcolor(3); { cout<<"Before
x1)*sin(a)+(y2-y1)*cos(a)); shearing of rectangle"<<endl; closegraph();
setcolor(2); int return 0;
rectangle(x1,y1,x2,y2); x1=200,y1=300,x2=500,y2=30 setcolor(3);
0,x3=350,y3=400; line(x1,y1,x2,y2); }
setcolor(4);
• 3D Transformation [Translation, Rotation and Scaling] in C/C++ • x’ = x
• BY PROGRAMMING TECHNIQUES · PUBLISHED MARCH 23, 2012 · • About y – axis
UPDATED JANUARY 31, 2019
• z’ = z cosθ – x sinθ
• Translation • x’ = z sinθ + x cosθ
• y’ = y
• In a three-dimensional homogeneous coordinates representation, a • About z – axis
point is translated from position P = (x, y, z) to position P’ = (x’, y’, z’)
with the following equations.
• x’ = x + tx • x’ = x cosθ – y sinθ
• y’ = y + ty • y’ = x sinθ + y cosθ
• z’ = z + tz • z’ = z
• Rotation about axes • Scaling
• Scaling with respect a selected fixed position (xf, yf, zf) can be
represented with the following transformation sequence:
• To generate a rotation transformation for an object, we must
designate an axis of rotation (about which the the object is to be
rotated) and the amount of angular rotation. Unlike 2D applications, • 1. Translate the fixed point to the origin
where all transformations are carried out in the xy plane, a three- • 2. Scale the object relative to the coordinate origin
dimensional rotation can be specified around any line in space. I will
cover rotation about arbitrary axis in another post, here the • 3. Translate the fixed point back to its original position
discussion is restricted to rotation about coordinate axes. • The equations for this sequence of transformation is (where s is
• About x – axis scaling factor)