Reflection And Shearing In Computer Graphics Program

If user choose reflection then rotate the triangle in 1800 at (x2, y2) and draw the rotated triangle (which is reflected triangle). If user choose shearing then get the shear value and draw the sheared triangle in the following coordinates (x1, y1, x2+x, y2, x3, y3). To write a C program to perform composite 2D transformations such as translation, rotation, scaling, reflection and shearing. Algorithm: Step 1: Start the program.

/* C PROGRAM TO DEMONSTRATE SHEARING TRANSFORMATION*/

/* AUTHOR : DEEPAK MAHAKALE 3RD YEAR IT SRCOEM, NAGPUR*/
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int poly[30],a[9][3],b[3][3],c[9][3],poly2[30];
int x=0,y=0,p,i,j,k,xc,yc,ch;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,'C:/TC/BGI');
xc=getmaxx()/2;
yc=getmaxy()/2;

setcolor(1);
setbkcolor(15);
setfillstyle(6,3);

printf('n Enter number of points : ');
scanf('%d',&p);
j=0;
for(i=0;i<p*2;i+=2)
{
printf('n Enter cordinate point x%d and y%d : ',j+1,j+1);

scanf('%d',&poly[i]);
scanf('%d',&poly[i+1]);
j++;
}
poly[p*2]=poly[0];
poly[p*2+1]=poly[1];

for(i=0;i<p*2;i+=2)
{
poly2[i]=xc+poly[i];
poly2[i+1]=yc-poly[i+1];

}
poly2[p*2]=poly2[0];
poly2[p*2+1]=poly2[1];

fillpoly(p+1,poly2);
line(0,yc,xc*2,yc);
line(xc,0,xc,yc*2);

printf('n Shearing of : n 1. x n 2. y n 3. Bothn enter choice : ');
scanf('%d',&ch);
if(ch1)
{
printf('n Enter x shear value : ');
scanf('%d',&x);
}
if(ch2)
{
printf('n Enter y shear value : ');
scanf('%d',&y);
}
if(ch3)
{
printf('n Enter x shear value : ');
scanf('%d',&x);
printf('n Enter y shear value : ');
scanf('%d',&y);
}

j=0;
for(i=0;i<p;i++)
{
a[i][0]=poly[j];
a[i][1]=poly[++j];
a[i][2]=1;
++j;
}

if(ch1)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(ij)
{
b[i][j]=1;
}
}
}
b[1][0]=x;

}
else if(ch2)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(ij)
{
b[i][j]=1;
}
}
}
b[0][1]=y;

}
else if(ch3)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(ij)
{
b[i][j]=1;
}
}
}

b[1][0]=x;
b[0][1]=y;

}
for(i=0;i<p;i++)
{

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

}
printf('nnnnnt After Shearing : ');

for(i=0,j=0;i<p;i++,j+=2)
{
poly[j] =xc+c[i][0];
poly[j+1]=yc-c[i][1];
}
poly[j] =poly[0];
poly[j+1]=poly[1];

setfillstyle(9,2);
fillpoly(p+1,poly);

getch();
closegraph();
}

2D Transformations in Computer Graphics-

We have discussed-

  • Transformation is a process of modifying and re-positioning the existing graphics.
  • 2D Transformations take place in a two dimensional plane.

In computer graphics, various transformation techniques are-

In this article, we will discuss about 2D Shearing in Computer Graphics.

2D Shearing in Computer Graphics-

In Computer graphics,

2D Shearing is an ideal technique to change the shape of an existing object in a two dimensional plane.

In a two dimensional plane, the object size can be changed along X direction as well as Y direction.

So, there are two versions of shearing-

  1. Shearing in X direction
  2. Shearing in Y direction

Consider a point object O has to be sheared in a 2D plane.

Let-

  • Initial coordinates of the object O = (Xold, Yold)
  • Shearing parameter towards X direction = Shx
  • Shearing parameter towards Y direction = Shy
  • New coordinates of the object O after shearing = (Xnew, Ynew)

Shearing in X Axis-

Shearing in X axis is achieved by using the following shearing equations-

  • Xnew = Xold + Shx x Yold
  • Ynew = Yold

In Matrix form, the above shearing equations may be represented as-

For homogeneous coordinates, the above shearing matrix may be represented as a 3 x 3 matrix as-

Shearing in Y Axis-

Shearing in Y axis is achieved by using the following shearing equations-

  • Xnew = Xold
  • Ynew = Yold + Shy x Xold

In Matrix form, the above shearing equations may be represented as-

For homogeneous coordinates, the above shearing matrix may be represented as a 3 x 3 matrix as-

PRACTICE PROBLEMS BASED ON 2D SHEARING IN COMPUTER GRAPHICS-

Problem-01:

Given a triangle with points (1, 1), (0, 0) and (1, 0). Apply shear parameter 2 on X axis and 2 on Y axis and find out the new coordinates of the object.

Reflection And Shearing In Computer Graphics Program Free

Solution-

Given-

  • Old corner coordinates of the triangle = A (1, 1), B(0, 0), C(1, 0)
  • Shearing parameter towards X direction (Shx) = 2
  • Shearing parameter towards Y direction (Shy) = 2

Shearing in X Axis-

For Coordinates A(1, 1)

Let the new coordinates of corner A after shearing = (Xnew, Ynew).

Applying the shearing equations, we have-

  • Xnew = Xold + Shx x Yold = 1 + 2 x 1 = 3
  • Ynew = Yold = 1

Thus, New coordinates of corner A after shearing = (3, 1).

For Coordinates B(0, 0)

Reflection And Shearing In Computer Graphics ProgramReflection and shearing in computer graphics programmer

Let the new coordinates of corner B after shearing = (Xnew, Ynew).

Applying the shearing equations, we have-

  • Xnew = Xold + Shx x Yold = 0 + 2 x 0 = 0
  • Ynew = Yold = 0

Thus, New coordinates of corner B after shearing = (0, 0).

For Coordinates C(1, 0)

Let the new coordinates of corner C after shearing = (Xnew, Ynew).

Applying the shearing equations, we have-

  • Xnew = Xold + Shx x Yold = 1 + 2 x 0 = 1
  • Ynew = Yold = 0

Thus, New coordinates of corner C after shearing = (1, 0).

Thus, New coordinates of the triangle after shearing in X axis = A (3, 1), B(0, 0), C(1, 0).

Shearing in Y Axis-

For Coordinates A(1, 1)

Let the new coordinates of corner A after shearing = (Xnew, Ynew).

Applying the shearing equations, we have-

  • Xnew = Xold = 1
  • Ynew = Yold + Shy x Xold = 1 + 2 x 1 = 3

Thus, New coordinates of corner A after shearing = (1, 3).

For Coordinates B(0, 0)

Let the new coordinates of corner B after shearing = (Xnew, Ynew).

Reflection and shearing in computer graphics programming

Applying the shearing equations, we have-

  • Xnew = Xold = 0
  • Ynew = Yold + Shy x Xold = 0 + 2 x 0 = 0

Thus, New coordinates of corner B after shearing = (0, 0).

For Coordinates C(1, 0)

Let the new coordinates of corner C after shearing = (Xnew, Ynew).

Reflection And Shearing In Computer Graphics Programmer

Applying the shearing equations, we have-

  • Xnew = Xold = 1
  • Ynew = Yold + Shy x Xold = 0 + 2 x 1 = 2

Thus, New coordinates of corner C after shearing = (1, 2).

Reflection And Shearing In Computer Graphics Programming

Thus, New coordinates of the triangle after shearing in Y axis = A (1, 3), B(0, 0), C(1, 2).

Reflection And Shearing In Computer Graphics C Program

To gain better understanding about 2D Shearing in Computer Graphics,

Get more notes and other study material of Computer Graphics.

Watch video lectures by visiting our YouTube channel LearnVidFun.

2D Shearing in Computer Graphics | Definition | Examples
Description
2D Shearing in Computer Graphics is a process of modifying the shape of an object in 2D plane. Shearing Transformation in Computer Graphics Definition, Solved Examples and Problems.
Author
Gate Vidyalay
Publisher Logo

Comments are closed.