You are on page 1of 30

UNIT 3: OVERVIEW OF TRANSFORMATION

CO3 : Develop programs for 2D and 3D transformations

CO4 : Use projections to visualize objects on view plane

Two dimensional transformation


In many applications, changes in orientations, size, and shape are accomplished with geometric
transformations that alter the coordinate descriptions of objects.
Basic geometric transformations are: These are most useful and most commonly used.
Translation (Position)
Rotation (Orientation) Scaling (Size)
Other transformations: These transformations are useful in certain applications.
Reflection (Mirror)
Shear (Size)

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

Program: Write a program for 2D Translation 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;

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)

 Rotation is applied to an object by repositioning it along a circular path in the xy plane. To

generate a rotation, we specify

o Rotation angle θ
o Pivot point ( xr , yr)
 Positive values of θ for counterclockwise rotation

 Negative values of θ for clockwise rotation.

(x,y) ---> Rotate about the origin by θ" --->(x’, y’)

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(θ)

So, x’ = x cos(θ) – y sin(θ) and y’ = y cos(θ) + x sin(θ)


Representing the above equation in matrix form,

Where R is the rotation matrix

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 –

Program: Write a program for 2D Rotation of a Triangle

#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.)

To perform a sequence of transformation such as translation followed by rotation and scaling,


we need to follow a sequential process −

• Translate the coordinates,


• Rotate the translated coordinates, and then
• Scale the rotated coordinates to complete the composite transformation.
To shorten this process, we have to use 3×3 transformation matrix instead of 2×2
transformation matrix. To convert a 2×2 matrix to 3×3 matrix, we have to add an extra
dummy coordinate W.
In this way, we can represent the point by 3 numbers instead of 2 numbers, which is called
Homogenous Coordinate system.

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).

Homogeneous coordinates for translation:

So

8
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION

Homogeneous coordinates for Rotation:

So,

Homogeneous Coordinates for Scaling:

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:

Program: To Perform 2d Transformations (Reflection)

#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>

void disp(int n,float c[][3])


{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(GREEN);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}

void mul(int n,float b[][3],float c[][3],float a[][3])


{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
a[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j] = a[i][j] + (c[i][k] * b[k][j]);
}
}

void reflection(int n,float c[][3])


{
float b[10][3],a[10][3];

11
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION

int i=0,ch,j;
cleardevice();

printf("\n\t* * MENU * *");


printf("\n\t1) ABOUT X-AXIS");
printf("\n\t2) ABOUT Y-AXIS");
printf("\n\t3) ABOUT ORIGIN");
printf("\n\t4) ABOUT Y=X");
printf("\n\t5) ABOUT Y=-X");
printf("\n\t6) EXIT”");
printf("\n\tENTER YOUR CHOICE :");
scanf("%d",&ch);
clrscr();
cleardevice();
disp(n,c);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
b[i][j]=1;
}
switch(ch)
{
case 1:
b[1][1]=-1;
break;
case 2:
b[0][0]=-1;
break;
case 3:
b[0][0]=-1;
b[1][1]=-1;
break;
case 4:
b[0][0]=0;
b[1][1]=0;
b[0][1]=1;
b[1][0]=1;
break;
case 5:
b[0][0]=0;

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.

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

14
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION

The Y-Shear can be represented in matrix from as –

Shearing relative to other reference line:


In x shear transformation use y reference line and in y shear use x reference line. X shear with
y reference line:

y shear with x reference line:

Program: Shearing transformation in C graphics

#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

int vertex[10][3],n; clrscr();


printf("\nEnter the no. of vertex : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the points (x,y): ");
scanf("%d%d",&x,&y);
vertex[i][0]=x;
vertex[i][1]=y;
vertex[i][2]=1;
}
shear(vertex,n);
getch();
return 0;
}
void init(int vertex[10][3],int n)
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(10);
line(0,240,640,240); //drawing X axis
line(320,0,320,480); //drawing Y axis setcolor(3);
line(450,20,490,20);
setcolor(15);
line(450,50,490,50);
setcolor(6);
outtextxy(500,20,"Original");
outtextxy(500,50,"Transformed");
setcolor(3);

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]);

void mul(int mat[3][3],int vertex[10][3],int n)


{
int i,j,k; // loop variables
int res[10][3];

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]);

void shear(int vertex[10][3],int n)


{
int opt,xref,yref,xsh,ysh;
int shear_array[3][3];
char ans;
do
{
printf("\n1.x-shear\n2.y-shear\n3.x shear with yref\n4.y shear with xref\nYour
Choice: ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the x shear : ");
scanf("%d",&xsh);
shear_array[0][0]=1;
shear_array[1][0]=xsh;
shear_array[2][0]=0;
shear_array[0][1]=0;
shear_array[1][1]=1;
shear_array[2][1]=0;

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)

• Translate point (Xp, Yp) to the origin.


• Rotate it about the origin.
• Finally, translate the center of rotation back where it belonged.

19
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune
UNIT 3: OVERVIEW OF TRANSFORMATION

Rotation about arbitrary point:


Suppose the reference point of rotation is other than origin, then in that case follow series 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

Program for 3D Translation

#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();
}

Program for 3D Scaling

#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();
}

Program for 3D Rotation

#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

• It is the process of converting a 3D object into a 2D object.


• It is also defined as mapping or transformation of the object in projection plane or
view plane.
• Types of projection
– Parallel Projection
– Perspective Projection
Parallel Projection
• Parallel projection discards z-coordinate and parallel lines from each vertex on the
object are extended until they intersect the view plane.
• In parallel projection, we specify a direction of projection instead of center of
projection.

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

• There are three types of orthographic projections −


– Front Projection
– Top Projection
– Side Projection

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

• Perspective projection is located as a finite point whereas Parallel projection is located


at infinite points.
• Perspective projection form a realistic picture of object whereas Parallel projection do
not form realistic view of object.
• Perspective projection cannot preserve the relative proportion of an object whereas
Parallel projection can preserve the relative proportion of an object.
• Projector in perspective projection is not parallel whereas Projector in parallel
projection is parallel.
• Perspective projection represents the object in three dimensional way whereas Parallel
projection represents the object in a different way like telescope.
• The lines of perspective projection are not parallel whereas The lines of parallel
projection are parallel.
• Perspective projection cannot give the accurate view of object whereas Parallel
projection can give the accurate view of object.

30
Course Coordinator: Mrs. Deshmukh A.P. M.M.Polytechnic, Thergaon , Pune

You might also like