Two Dimensional Transformations

1 year ago
Computer Graphics

Write a C-program for performing the basic 2D transformations such as translation, Scaling, Rotation, shearing, and reflection for a given 2D object.

Aim: To apply the basic 2D transformations such as translation, Scaling, Rotation, shearing, and reflection for a given 2D object.

Description: We have to perform 2D transformations on 2D objects. Here we perform transformations on a line segment.

The 2D transformations are:

  1. Translation
  2. Scaling
  3. Rotation
  4. Reflection
  5. Shear

1.Translation: Translation is defined as moving the object from one position to another position along a straight line path.

We can move the objects based on translation distances along x and y axis. tx denotes translation distance along the x-axis and ty denotes translation distance along y axis.

Translation Distance: It is nothing but by how much units we should shift the object from one location to another along x, y-axis.

Consider (x,y) are old coordinates of a point. Then the new coordinates of that same point (x’,y’) can be

obtained as follows:

X’=x+tx

Y’=y+ty

We denote translation transformation as P. we express above equations in matrix form as:

x,y---old coordinates x’,y’—new coordinates after translation

tx,ty—translation distances, T is

2.Scaling: scaling refers to changing the size of the object either by increasing or decreasing. We will increase or decrease the size of the object based on scaling factors along x and y-axis.

If (x, y) are old coordinates of object, then new coordinates of object after applying scaling transformation are obtained as:

x’=x*sx

y’=y*sy.

sx and sy are scaling factors along x-axis and y-axis. we express the above equations in matrix form.

3 Rotation: A rotation repositions all points in an object along a circular path in the plane centered at the pivot We rotate an object by an angle theta.

New coordinates after rotation depend on both x and y

  • x’ = xcosθ -y sinθ
  • y’ = xsinθ+ ycosθ

•      or in matrix form:

 

P' = R • P,

R-rotation matrix.

4.Reflection: Reflection is nothing but producing mirror image of an Reflection can be done just by rotating the object about given axis of reflection with an angle of 180 degrees.

5.Shear:

  1. Shear is the translation along an axis by an amount that increases linearly with another axis (Y). It produces shape distortions as if objects were composed of layers that are caused to slide over each
  2. Shear transformations are very useful in creating italic letters and slanted letters from regular
  3. Shear transformation changes the shape of the object to a slant

  1. Shear transformation is of 2 types:                                                                                                    X-shear: changing x-coordinate value and keeping y constant

x’=x+shx*y

y’=y

b.Y-shear: changing y coordinates value and keeping x constant

x’=x

y’=y+shy*x

shx and shy are shear factors along x and y-axis.

 

1.      Program for translation:

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int gd=DETECT,gm;

int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter the starting point of line segment:"); scanf("%d %d",&x1,&y1);

printf("Enter the ending point of line segment:"); scanf("%d %d",&x2,&y2);

printf("Enter translation distances tx,ty:\n"); scanf("%d%d",&tx,&ty);

setcolor(5); line(x1,y1,x2,y2);

outtextxy(x2+2,y2+2,"Original line"); x3=x1+tx;

y3=y1+ty; x4=x2+tx; y4=y2+ty; setcolor(7); line(x3,y3,x4,y4);

outtextxy(x4+2,y4+2,"Line after translation"); getch();

}

 

Program for scaling:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int gd=DETECT,gm;

float x1,y1,x2,y2,sx,sy,x3,y3,x4,y4;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter the starting point coordinates:"); scanf("%f %f",&x1,&y1);

printf("Enter the ending point coordinates:"); scanf("%f %f",&x2,&y2);

printf("Enter scaling factors sx,sy:\n"); scanf("%f%f",&sx,&sy);

setcolor(5); line(x1,y1,x2,y2);

outtextxy(x2+2,y2+2,"Original line"); x3=x1*sx;

y3=y1*sy; x4=x2*sx; y4=y2*sy; setcolor(7);

}

Output:

 

   Program for Rotation:

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h> void main()

{

int gd=DETECT,gm;

float x1,y1,x2,y2,x3,y3,x4,y4,a,t; initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); printf("Enter coordinates of starting point:\n"); scanf("%f%f",&x1,&y1);

printf("Enter coordinates of ending point\n"); scanf("%f%f",&x2,&y2);

printf("Enter angle for rotation\n"); scanf("%f",&a);

setcolor(5); line(x1,y1,x2,y2);

outtextxy(x2+2,y2+2,"Original line"); t=a*(3.14/180);

x3=(x1*cos(t))-(y1*sin(t));

y3=(x1*sin(t))+(y1*cos(t));

x4=(x2*cos(t))-(y2*sin(t));

y4=(x2*sin(t))+(y2*cos(t)); setcolor(7); line(x3,y3,x4,y4);

outtextxy(x3+2,y3+2,"Line after rotation"); getch();

}

Output:

  Program for reflection along x-axis:

# include <stdio.h>

# include <conio.h>

# include <graphics.h>

# include <math.h>

char IncFlag;

int PolygonPoints[3][2] ={{10,100},{110,100},{110,200}};

void PolyLine()

{

int iCnt; cleardevice(); line(0,240,640,240); line(320,0,320,480);

for (iCnt=0; iCnt<3; iCnt++)

{

line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1], PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);

}

}

void Reflect()

{

float Angle; int iCnt;

int Tx,Ty; printf("endl");;

for (iCnt=0; iCnt<3; iCnt++)

PolygonPoints[iCnt][1] = (480 - PolygonPoints[iCnt][1]);

}

void main()

{

int gDriver = DETECT, gMode; int iCnt;

initgraph(&gDriver, &gMode, "C:\\TurboC3\\BGI"); for (iCnt=0; iCnt<3; iCnt++)

{

PolygonPoints[iCnt][0] += 320;

PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];

}

PolyLine(); getch(); Reflect(); PolyLine(); getch()

}

Output:

Object before reflection about x-axis

  Program for Reflection about y-axis:

# include <stdio.h>

# include <conio.h>

# include <graphics.h>

# include <math.h>

char IncFlag;

int PolygonPoints[3][2] =

{{10,100},{110,100},{110,200}};

void PolyLine()

{

int iCnt; cleardevice(); line(0,240,640,240); line(320,0,320,480);

for (iCnt=0; iCnt<3; iCnt++)

{

line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1], PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);

}

}

void Reflect()

{

float Angle; int iCnt;

int Tx,Ty;

for (iCnt=0; iCnt<3; iCnt++)

PolygonPoints[iCnt][0] = (640 - PolygonPoints[iCnt][0]);

}

void main()

{

int gd = DETECT, gm; int iCnt;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI"); for (iCnt=0; iCnt<3; iCnt++)

{

PolygonPoints[iCnt][0] += 320;

PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];

}

PolyLine(); getch(); Reflect(); PolyLine(); getch();

}

Output:

Object before reflection:

 

Object after Reflection about y-axis:

 

Program for X-shear:

 

#include<stdio.h>

#include<conio.h>

#include<dos.h>

#include<graphics.h>

void main()

{

int gd=DETECT,gm; float shx,shy;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); printf("Enter shear factor shx along x-axis :"); scanf("%f",&shx);

line(100,0,200,0); line(200,0,200,200); line(200,200,100,200); line(100,200,100,0);

printf("X-shear"); setcolor(12);

line((100+(0*shx)),0,(200+(0*shx)),0);

line((200+(0*shx)),0,(200+(200*shx)),200);

line((200+(200*shx)),200,(100+(200*shx)),200);

line((100+(200*shx)),200,(100+(0*shx)),0);

getch();

}

Output:

In above output, red lined rectangle denotes object after x-shear transformation.

 

  Program for Y-shear:

 

#include<stdio.h>

#include<conio.h>

#include<dos.h>

#include<graphics.h>

void main()

{

int gd=DETECT,gm;

float shx,shy;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

printf("Enter shear factor shy along y-axis :"); scanf("%f",&shy);

line(100,10,200,10);

line(200,10,200,200);

line(200,200,100,200); line(100,200,100,10);

printf("Y-shear"); setcolor(12);

line(100,10+(shy*100),200,10+(shy*200)); line(200,10+(shy*200),200,200+(shy*200)); line(200,200+(shy*200),100,200+(shy*100)); line(100,200+(shy*100),100,10+(shy*100));

getch();

closegraph();

0
Rusma Khadka
Jan 4, 2023
More related questions

Questions Bank

View all Questions